原 Linux环境下,shell脚本一键获取操作系统主机配置信息
脚本
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 | cat > /tmp/get_os_info.sh <<"EOF" #!/bin/bash export LANG=en_US.UTF-8 # 如果 cat /proc/1/cgroup | grep docker | wc -l 大于0 或 systemd-detect-virt 返回 docker,则为 docker容器, # 如果 virt-what 返回 kvm或vmware或hyperv或xen、xen-hvm、lxc 或 systemd-detect-virt 返回 vmware、kvm 则为虚拟机 # 如果 cat /sys/class/dmi/id/product_name 返回 Bochs 、OpenStack Nova则为虚拟机 若返回Alibaba Cloud ECS,则代表阿里云主机 # 如果 systemd-detect-virt 返回 none 则表示物理机 # 判断当前主机类型 if [ "$(cat /proc/1/cgroup | grep -c docker)" -gt 0 ] || systemd-detect-virt | grep -q docker; then host_type="Docker容器" elif [[ $(cat /sys/class/dmi/id/product_name) == "Alibaba Cloud ECS" ]]; then host_type="阿里云主机" elif [[ $(virt-what) =~ (kvm|vmware|hyperv|xen|xen-hvm|lxc) ]] || [[ $(systemd-detect-virt) =~ (vmware|kvm) ]]; then host_type="虚拟机" elif [[ $(cat /sys/class/dmi/id/product_name) =~ (Bochs|OpenStack Nova) ]]; then host_type="虚拟机" else host_type="物理机" fi echo "- **当前主机类型**: $host_type" # 获取并显示系统制造商、产品名称和版本 echo " - 当前主机系统硬件信息:" dmidecode -t 1 | awk -F: '/Manufacturer/{printf " - 制造商: %s\n", $2} /Product Name/{printf " - 产品名称: %s\n", $2}' # 获取操作系统信息 os_info=$(cat /etc/os-release | grep 'PRETTY_NAME' | cut -d '"' -f 2) # 如果操作系统是 CentOS Linux 7 (Core),查询 /etc/redhat-release if [[ "$os_info" == "CentOS Linux 7 (Core)" ]]; then os_info=$(cat /etc/redhat-release) fi echo "- **操作系统信息**: $os_info" # 显示当前内核版本 kernel_version=$(uname -r) echo "- **内核版本**: $kernel_version" echo "- **硬件配置**:" # 获取CPU信息 cpu_model=$(lscpu | grep "Model name" | awk -F: '{print $2}' | xargs) cpu_cores=$(lscpu | grep "^CPU(s):" | awk '{print $2}') # 获取每核的线程数 threads_per_core=$(lscpu | grep -i "Thread(s) per core" | awk '{print $4}') # 如果每核线程数大于1,则说明开启了超线程 if [[ $threads_per_core -gt 1 ]]; then HyperThreading="超线程已开启 (每核线程数: $threads_per_core)" else HyperThreading="超线程未开启 (每核线程数: $threads_per_core)" fi echo " - **CPU**: $cpu_model ,**共 $cpu_cores 核** ,$HyperThreading " # 获取内存信息 memory=$(free -h | awk '/^Mem:/{print $2}') echo " - **内存**: $memory" # 获取指定磁盘信息 echo " - **磁盘**: $disk_info" lsblk -d -o NAME,ROTA | grep -E '^(sd|vd)' | awk '{print " -", $1, ":", ($2 == 1 ? "HDD机械硬盘" : "SSD固态硬盘")}' echo ' ```' if command -v lshw &> /dev/null; then lshw -short | grep disk | grep -v "cdrom" | awk '{$1=""; print " "$0}' fi echo " " lsscsi | grep -v "cd/dvd" | grep "disk" | awk '{$1=""; print " "$0}' echo ' ```' # 显示文件系统类型 echo "- **文件系统**:" # df -Th 2>/dev/null | grep -v "loop" | grep -v "tmpfs" | awk '{print " -", $1, "->", $2}' echo ' ```' df -Th 2>/dev/null | grep -v "loop" | grep -v "tmpfs" | grep -v "iso" | grep -v "/var/lib/docker/overlay2" | grep -v "/boot" | awk '{print " "$0}' echo ' ```' # 1. 对闪存(SSD)等存储介质,优先使用noop或none调度算法 # 2. 对IO压力比较重且非SSD存储设备,且功能比较单一的场景,例如数据库服务器,使用deadline或mq-deadline调度算法 # for dev in $(lsblk -dno NAME | grep -E '^(sd|vd)'); do echo "$dev: $(cat /sys/block/$dev/queue/scheduler)"; done # echo noop | sudo tee /sys/block/sda/queue/scheduler # for dev in $(lsblk -dno NAME | grep -E '^(sd|vd)'); do echo "echo noop | sudo tee /sys/block/$dev/queue/scheduler"; done # 获取I/O调度器信息 echo "- I/O 调度器:" for dev in $(lsblk -dno NAME | grep -E '^(sd|vd)'); do # 提取中括号中的当前调度器 scheduler=$(cat /sys/block/$dev/queue/scheduler 2>/dev/null | grep -o '\[[^]]*\]' | tr -d '[]') echo " - $dev: $scheduler" done # 查看时区 timezone=$(timedatectl | grep "Time zone:" | awk '{print $3,$4,$5}') echo "- 当前主机时区信息:$(echo $timezone)" # 查看IP地址 ip_add=`hostname -I` hostname1=`hostname` echo "- 当前主机IP地址:$ip_add" echo "- 当前主机IP地址:$hostname1" EOF sleep 1 sh /tmp/get_os_info.sh |
结果
可直接粘贴到markdown的文档中进行解析即可: