干貨:Linux查詢 OS、CPU、內存、硬盤信息
一.前言
當我們接手了一臺或者幾臺服務器的時候,首先我們有必要對服務器的基本配置有所認識,這樣才可以對癥下藥,對以后的軟件部署,系統運維會有事半功倍的效果。
二.關于服務器基本配置
查詢服務器的基本配置一般查詢操作系統,CPU,內存,硬盤,下面進行逐一講解。
2.1 操作系統基本配置查詢
查看操作系統版本
- #cat /etc/redhat-release這個命令主要是查看紅帽發行的操作系統的版本號
- [root@node5 ~]# cat /etc/redhat-release
- CentOS Linux release 7.4.1708 (Core)
- #cat /etc/issue這個命令適用于大多數linux發行版
- [root@node5 ~]# cat /etc/issue
- \S
- Kernel \r on an \m
查看操作系統內核版本
- [root@node5 ~]# uname -r
- 3.10.0-693.el7.x86_64
查看操作系統詳細信息
- [root@node5 ~]# uname -a
- Linux node5 3.10.0-693.el7.x86_64 #1 SMP Tue Aug 22 21:09:27 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux
- #從上面這段輸出可以看出,該服務器主機名是node5,linux內核版本是3.10.0-693.el7.x86_64,CPU是x86架構
- #該命令可以查看更多信息
- [root@node5 ~]# more /etc/*release
- ::::::::::::::
- /etc/centos-release
- ::::::::::::::
- CentOS Linux release 7.4.1708 (Core)
- ::::::::::::::
- /etc/os-release
- ::::::::::::::
- NAME="CentOS Linux"
- VERSION="7 (Core)"
- ID="centos"
- ID_LIKE="rhel fedora"
- VERSION_ID="7"
- PRETTY_NAME="CentOS Linux 7 (Core)"
- ANSI_COLOR="0;31"
- CPE_NAME="cpe:/o:centos:centos:7"
- HOME_URL="https://www.centos.org/"
- BUG_REPORT_URL="https://bugs.centos.org/"
- CENTOS_MANTISBT_PROJECT="CentOS-7"
- CENTOS_MANTISBT_PROJECT_VERSION="7"
- REDHAT_SUPPORT_PRODUCT="centos"
- REDHAT_SUPPORT_PRODUCT_VERSION="7"
- ::::::::::::::
- /etc/redhat-release
- ::::::::::::::
- CentOS Linux release 7.4.1708 (Core)
- ::::::::::::::
- /etc/system-release
- ::::::::::::::
- CentOS Linux release 7.4.1708 (Core)
2.2 CPU基本配置查詢
名詞解釋
名詞 | 含義 |
---|---|
CPU物理個數 | 主板上實際插入的cpu數量 |
CPU核心數 | 單塊CPU上面能處理數據的芯片組的數量,如雙核、四核等 (cpu cores) |
邏輯CPU數/線程數 | 一般情況下,邏輯cpu=物理CPU個數×每顆核數,如果不相等的話,則表示服務器的CPU支持超線程技術 |
查看 CPU 物理個數
- [root@node5 ~]# grep 'physical id' /proc/cpuinfo | sort -u | wc -l
- 1
查看 CPU 核心數量
- [root@node5 ~]# grep 'core id' /proc/cpuinfo | sort -u | wc -l
- 4
查看 CPU 線程數
- #邏輯cpu數:一般情況下,邏輯cpu=物理CPU個數×每顆核數,如果不相等的話,則表示服務器的CPU支持超線程技術(HT:簡單來說,它可使處理#器中的1 顆內核如2 顆內核那樣在操作系統中發揮作用。這樣一來,操作系統可使用的執行資源擴大了一倍,大幅提高了系統的整體性能,此時邏#輯cpu=物理CPU個數×每顆核數x2)
- [root@node5 ~]# cat /proc/cpuinfo| grep "processor"|wc -l
- 4
- [root@node5 ~]# grep 'processor' /proc/cpuinfo | sort -u | wc -l
- 4
查看 CPU 型號
- [root@node5 ~]# cat /proc/cpuinfo | grep name | sort | uniq
- model name : Intel(R) Core(TM) i7-8550U CPU @ 1.80GHz
- [root@node5 ~]# dmidecode -s processor-version | uniq #使用uniq進行去重
- Intel(R) Core(TM) i7-8550U CPU @ 1.80GHz
查看 CPU 的詳細信息
- #CPU有幾個核,就會輸出幾個重復的信息
- [root@node5 ~]# cat /proc/cpuinfo
- processor : 0
- vendor_id : GenuineIntel
- cpu family : 6
- model : 142
- model name : Intel(R) Core(TM) i7-8550U CPU @ 1.80GHz
- stepping : 10
- microcode : 0x96
- cpu MHz : 2000.921
- cache size : 8192 KB
- physical id : 0
- siblings : 4
- core id : 0
- cpu cores : 4
- apicid : 0
- initial apicid : 0
- fpu : yes
- fpu_exception : yes
- cpuid level : 22
- wp : yes
- flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ss ht syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon nopl xtopology tsc_reliable nonstop_tsc eagerfpu pni pclmulqdq vmx ssse3 fma cx16 pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand hypervisor lahf_lm abm 3dnowprefetch tpr_shadow vnmi ept vpid fsgsbase tsc_adjust bmi1 avx2 smep bmi2 invpcid mpx rdseed adx smap clflushopt xsaveopt xsavec arat
- bogomips : 4002.00
- clflush size : 64
- cache_alignment : 64
- address sizes : 43 bits physical, 48 bits virtual
- power management:
查看CPU的詳細信息
- [root@node5 ~]# lscpu
- Architecture: x86_64
- CPU op-mode(s): 32-bit, 64-bit
- Byte Order: Little Endian
- CPU(s): 4
- On-line CPU(s) list: 0-3
- Thread(s) per core: 1
- Core(s) per socket: 4
- Socket(s): 1
- NUMA node(s): 1
- Vendor ID: GenuineIntel
- CPU family: 6
- Model: 142
- Model name: Intel(R) Core(TM) i7-8550U CPU @ 1.80GHz
- Stepping: 10
- CPU MHz: 2000.921
- BogoMIPS: 4002.00
- Virtualization: VT-x
- Hypervisor vendor: VMware
- Virtualization type: full
- L1d cache: 32K
- L1i cache: 32K
- L2 cache: 256K
- L3 cache: 8192K
- NUMA node0 CPU(s): 0-3
- Flags: fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ss ht syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon nopl xtopology tsc_reliable nonstop_tsc eagerfpu pni pclmulqdq vmx ssse3 fma cx16 pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand hypervisor lahf_lm abm 3dnowprefetch tpr_shadow vnmi ept vpid fsgsbase tsc_adjust bmi1 avx2 smep bmi2 invpcid mpx rdseed adx smap clflushopt xsaveopt xsavec arat
CPU配置總結
通過以上的查詢,我們可以知道該服務器是1路4核的CPU ,CPU型號是Intel(R) Core(TM) i7-8550U CPU @ 1.80GHz,該CPU沒有超線程。
2.3 內存基本配置查詢
名詞解釋
名詞 | 含義 |
---|---|
Mem | 內存的使用情況總覽表 |
Swap | 虛擬內存。即可以把數據存放在硬盤上的數據,當物理內存不足時,拿出部分硬盤空間當SWAP分區(虛擬成內存)使用,從而解決內存容量不足的情況。SWAP意思是交換,顧名思義,當某進程向OS請求內存發現不足時,OS會把內存中暫時不用的數據交換出去,放在SWAP分區中,這個過程稱為SWAP OUT。當某進程又需要這些數據且OS發現還有空閑物理內存時,又會把SWAP分區中的數據交換回物理內存中,這個過程稱為SWAP IN。當然,swap大小是有上限的,一旦swap使用完,操作系統會觸發OOM-Killer機制,把消耗內存最多的進程kill掉以釋放內存。 |
shared | 共享內存,即和普通用戶共享的物理內存值, 主要用于進程間通信 |
buffers | 用于存放要輸出到disk(塊設備)的數據的 |
cached | 存放從disk上讀出的數據 |
total | 總的物理內存,total=used+free |
used | 使用掉的內存 |
free | 空閑的內存 |
查詢服務器內存
- [root@node5 ~]# free -m
- total used free shared buff/cache available
- Mem: 3941 286 3446 19 208 3407
- Swap: 2047 0 2047
- #注釋
- #linux的內存管理機制的思想包括(不敢說就是)內存利用率最大化。內核會把剩余的內存申請為cached,而cached不屬于free范疇。當系統運#行時間較久,會發現cached很大,對于有頻繁文件讀寫操作的系統,這種現象會更加明顯。直觀的看,此時free的內存會非常小,但并不代表可##用的內存小,當一個程序需要申請較大的內存時,如果free的內存不夠,內核會把部分cached的內存回收,回收的內存再分配給應用程序。所以#對于linux系統,可用于分配的內存不只是free的內存,還包括cached的內存(其實還包括buffers)。
- #對于操作系統:
- #MemFree=total-used
- #MemUsed = MemTotal - MemFree
- #對于應用程序:
- #MemFree=buffers+cached+free
每隔3秒查詢一下內存
- [root@node5 ~]# free -s 3
- total used free shared buff/cache available
- Mem: 4036316 361144 3458272 19536 216900 3419776
- Swap: 2097148 0 2097148
- total used free shared buff/cache available
- Mem: 4036316 361144 3458272 19536 216900 3419776
- Swap: 2097148 0 2097148
- total used free shared buff/cache available
- Mem: 4036316 361144 3458272 19536 216900 3419776
- Swap: 2097148 0 2097148
2.4 硬盤基本配置查詢
查詢磁盤整體使用情況
- [root@node5 ~]# df -h
- Filesystem Size Used Avail Use% Mounted on
- /dev/mapper/centos-root 17G 4.1G 13G 24% /
- devtmpfs 2.0G 0 2.0G 0% /dev
- tmpfs 2.0G 8.0K 2.0G 1% /dev/shm
- tmpfs 2.0G 8.7M 2.0G 1% /run
- tmpfs 2.0G 0 2.0G 0% /sys/fs/cgroup
- /dev/sda1 1014M 125M 890M 13% /boot
- tmpfs 395M 0 395M 0% /run/user/0
- #命令拓展
- #df -a 顯示全部的文件系統的使用情況
- #df -i顯示inode信息
- #df -k 已字節數顯示區塊占用情況
- #df -T 顯示文件系統的類型
查詢某個目錄磁盤占用情況
- #命令拓展
- #du -s 指定目錄大小匯總
- #du -h帶計量單位
- #du -a 含文件
- #du --max-depth=1 子目錄深度
- #du -c 列出明細的同時,增加匯總值
- [root@node5 ~]# du -sh /home/
- 1.7G /home/
- [root@node5 ~]# du -ach --max-depth=2 /home/
- 4.0K /home/www/.bash_logout
- 4.0K /home/www/.bash_profile
- 4.0K /home/www/.bashrc
- 4.0K /home/www/web
- 16K /home/www
- 4.0K /home/nginx/.bash_logout
- 4.0K /home/nginx/.bash_profile
- 4.0K /home/nginx/.bashrc
- 12K /home/nginx
- 4.0K /home/esnode/.bash_logout
- 4.0K /home/esnode/.bash_profile
- 4.0K /home/esnode/.bashrc
- 4.0K /home/esnode/.oracle_jre_usage
- 4.3M /home/esnode/elasticsearch-analysis-ik-6.2.2.zip
- 80M /home/esnode/kibana-6.2.2-linux-x86_64.tar.gz
- 300M /home/esnode/x-pack-6.2.2.zip
- 28M /home/esnode/elasticsearch-6.2.2.tar.gz
- 4.0K /home/esnode/.bash_history
- 294M /home/esnode/elasticsearch-6.2.2
- 4.0K /home/esnode/.ssh
- 4.0K /home/esnode/x-pack生成的秘鑰.txt
- 1014M /home/esnode/kibana-6.2.2-linux-x86_64
- 8.0K /home/esnode/.viminfo
- 1.7G /home/esnode
- 1.7G /home/
- 1.7G total
查看目錄結構
- #tree命令默認沒有安裝,需要手動安裝一下
- [root@node5 ~]# yum -y install tree
- #-L指定目錄深度
- [root@node5 ~]# tree -L 2 /home/
- /home/
- ├── esnode
- │ ├── elasticsearch-6.2.2
- │ ├── elasticsearch-6.2.2.tar.gz
- │ ├── elasticsearch-analysis-ik-6.2.2.zip
- │ ├── kibana-6.2.2-linux-x86_64
- │ ├── kibana-6.2.2-linux-x86_64.tar.gz
- │ ├── x-pack-6.2.2.zip
- │ └── x-pack\347\224\237\346\210\220\347\232\204\347\247\230\351\222\245.txt
- ├── nginx
- └── www
- └── web
- 6 directories, 5 files
以樹狀的格式顯示所有可用的塊設備信息
- [root@node5 ~]# lsblk
- NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
- sda 8:0 0 20G 0 disk
- ├─sda1 8:1 0 1G 0 part /boot
- └─sda2 8:2 0 19G 0 part
- ├─centos-root 253:0 0 17G 0 lvm /
- └─centos-swap 253:1 0 2G 0 lvm [SWAP]
- sdb 8:16 0 1G 0 disk
- └─sdb1 8:17 0 200M 0 part
- sr0 11:0 1 1024M 0 rom
- #注釋
- #NAME —— 設備的名稱
- #MAJ:MIN —— Linux 操作系統中的每個設備都以一個文件表示,對塊(磁盤)設備來說,這里用主次設備編號來描述設備。
- #RM —— 可移動設備。如果這是一個可移動設備將顯示 1,否則顯示 0。
- #TYPE —— 設備的類型
- #MOUNTPOINT —— 設備掛載的位置
- #RO —— 對于只讀文件系統,這里會顯示 1,否則顯示 0。
- #SIZE —— 設備的容量
列出所有可用的設備、通用唯一識別碼(UUID)、文件系統類型以及卷標
- [root@node5 ~]# blkid
- /dev/sda1: UUID="6503b4ad-2975-4152-a824-feb7bea1b622" TYPE="xfs"
- /dev/sda2: UUID="nqZ4uJ-ksnN-KzYS-N42b-00m3-Ohc2-BJXunP" TYPE="LVM2_member"
- /dev/sdb1: UUID="94396e17-4821-4957-aa76-d41f33958ff5" TYPE="xfs"
- /dev/mapper/centos-root: UUID="c1d38b37-821d-48e7-8727-3937ccc657a4" TYPE="xfs"
- /dev/mapper/centos-swap: UUID="c2fcaf11-42d8-4e4c-bf9e-6464f0777198" TYPE="swap"
【編輯推薦】