導讀:Linux 高效但細節繁多,以下依「版本、記憶體、CPU、硬碟」四類整理最常翻用的查看指令。
uname 用來看核心版本,實務上直接加 -a 一次看全部:
[root@linuxprobe ~]# uname -a
Linux linuxprobe.com 2.6.32-358.el6.x86_64 #1 SMP Tue Jan 29 11:47:41 EST 2013 x86_64 x86_64 x86_64 GNU/Linux
但它看不到發行版名稱與版本號,這時改用下面兩個:
[root@linuxprobe ~]# lsb_release -a
LSB Version: :base-4.0-amd64:base-4.0-noarch:core-4.0-amd64:core-4.0-noarch
Distributor ID: RedHatEnterpriseServer
Description: Red Hat Enterprise Linux Server release 6.4 (Santiago)
Release: 6.4
Codename: Santiago
[root@linuxprobe ~]# cat /etc/issue
Red Hat Enterprise Linux Server release 6.4 (Santiago)
Kernel \r on an \m
free 是最直覺的工具:
[root@linuxprobe ~]# free
total used free shared buffers cached
Mem: 5228872 669360 4559512 0 28060 266936
-/+ buffers/cache: 374364 4854508
Swap: 4194296 0 4194296
[root@linuxprobe ~]# free -m # 以 MB 為單位
[root@linuxprobe ~]# free -g # 以 GB 為單位
總量、已用、可用、buffers 與 cached、以及 swap 狀況一目了然。要注意真正可用的量是 -/+ buffers/cache 那一行的 free 欄位,而非 Mem 列的 free。
uptime 看負載:
[root@linuxprobe ~]# uptime
20:09:48 up 43 min, 3 users, load average: 0.35, 0.25, 0.11
三個數字分別是 1、5、15 分鐘的平均負載,超過 CPU 核心數就代表已經吃緊。
top 即時監控 CPU 與記憶體,預設每 3 秒刷新:
[root@linuxprobe ~]# top
top - 20:15:41 up 49 min, 3 users, load average: 0.41, 0.38, 0.21
Tasks: 236 total, 1 running, 235 sleeping, 0 stopped, 0 zombie
Cpu(s): 0.2%us, 0.5%sy, 0.0%ni, 98.8%id, 0.5%wa, 0.0%hi, 0.0%si, 0.0%st
Mem: 5228872k total, 671136k used, 4557736k free, 28324k buffers
Swap: 4194296k total, 0k used, 4194296k free, 267360k cached
CPU 欄位中 us 是使用者程式、sy 是系統、id 是閒置、wa 是 IO 等待;wa 偏高通常代表磁碟是瓶頸。
CPU 硬體參數則看 /proc/cpuinfo:
cat /proc/cpuinfo | grep "physical id" | uniq | wc -l # 實體 CPU 數
cat /proc/cpuinfo | grep "cpu cores" | uniq # 每顆核心數
cat /proc/cpuinfo | grep 'model name' | uniq # CPU 型號
df 看分割區使用率,加 -h 更易讀:
[root@linuxprobe ~]# df -h
檔案系統 容量 已用 可用 已用% 掛載點
/dev/sda2 40G 459M 37G 2% /
tmpfs 2.5G 224K 2.5G 1% /dev/shm
/dev/sda1 97M 32M 61M 35% /boot
/dev/sda6 504M 18M 461M 4% /home
/dev/sda3 4.0G 2.5G 1.4G 65% /usr
fdisk -l 看分割區結構:
[root@linuxprobe ~]# fdisk -l
Disk /dev/sda: 107.4 GB, 107374182400 bytes
Device Boot Start End Blocks Id System
/dev/sda1 * 1 13 102400 83 Linux
/dev/sda2 13 5235 41943040 83 Linux
/dev/sda3 5235 5757 4194304 83 Linux
/dev/sda4 5757 13055 58616832 5 Extended
/dev/sda5 5757 6280 4194304 82 Linux swap / Solaris
/dev/sda6 6280 6345 524288 83 Linux
iostat 看讀寫速度,需先安裝 sysstat:
[root@linuxprobe ~]# iostat -t 1 3 # 每 1 秒一次,共 3 次
輸出中 Device 列的 tps 是每秒傳輸次數,Blk_read/s 與 Blk_wrtn/s 分別是每秒讀寫的區塊數,可據此判斷磁碟是否成為瓶頸。