linux擷取系統資訊常用命令 1.磁碟資訊[wang@localhost 案頭]$ du -sh ./mm392K ./mm[wang@localhost 案頭]$ du -ak ./mm376 ./mm/boot_nontrust.bin4 ./mm/bt.sh0 ./mm/check.md54 ./mm/rtk8723_bt_config4 ./mm/startup.x392 ./mm[wang@localhost 案頭]$ du -ah ./mm376K ./mm/boot_nontrust.bin4.0K ./mm/bt.sh0 ./mm/check.md54.0K ./mm/rtk8723_bt_config4.0K ./mm/startup.x392K ./mm[wang@localhost 案頭]$ du -ac ./mm376 ./mm/boot_nontrust.bin4 ./mm/bt.sh0 ./mm/check.md54 ./mm/rtk8723_bt_config4 ./mm/startup.x392 ./mm392 總計[wang@localhost 案頭]$ du -m ./mm1 ./mm-a 所有檔案情況;-s 合計資料;-h 友好輸出;-c 總和;-b -k -m -B 位元組 Kb Mb 塊。注意:可以用於檢測SD卡之類的是否掛載成功。 [wang@localhost 案頭]$ df -h檔案系統 容量 已用 可用 已用% 掛載點/dev/mapper/vg_localhost-lv_root 28G 12G 15G 46% //dev/sda1 194M 14M 171M 8% /boottmpfs 502M 15M 488M 3% /dev/shm.host:/ 139G 35G 105G 25% /mnt/hgfs可以不用加-h,這是習慣問題。 2.命令執行時間[wang@localhost 案頭]$ time lsa.txt b.txt b.txt.orig c.patch mm mm~ PC1 shell.sh test1.sh real 0m0.025s //從開始到結束的時間user 0m0.001s //使用者模式CPU的時間sys 0m0.001s //核心模式CPU的時間[wang@localhost 案頭]$ time -o out.o lsbash: -o: command not found real 0m0.004s user 0m0.000ssys 0m0.000s[wang@localhost 案頭]$ /usr/bin/time -o out.o ls //使用time的其他功能就要用/usr/bin/timea.txt b.txt b.txt.orig c.patch mm mm~ out.o PC1shell.sh test1.sh因為系統有兩個time,一個shell的內建命令,一個是/usr/bin/time。 [wang@localhost 案頭]$ /usr/bin/time -f "Pagesize: %Z bytes" ls> /dev/null Pagesize: 4096 bytes[wang@localhost 案頭]$ /usr/bin/time -f "Pagesize: %Z bytes" -o out.o ls> /dev/null 3.使用者資訊[wang@localhost 案頭]$ whowang tty1 2013-03-05 14:44 (:0)wang pts/0 2013-03-05 14:44 (:0.0)wang pts/1 2013-03-25 17:02 (:0.0)wang pts/2 2013-03-11 10:10 (:0.0)wang pts/3 2013-03-12 10:19 (:0.0)[wang@localhost 案頭]$ w 10:10:03 up 5 days, 21:54, 5 users, load average: 0.21, 0.06, 0.02USER TTY FROM LOGIN@ IDLE JCPU PCPU WHATwang tty1 :0 05Mar13 21days 9:48 0.03s pam: gdm-passwowang pts/0 :0.0 05Mar13 12days 0.46s 50.76s gnome-terminalwang pts/1 :0.0 Mon17 1.00s 0.27s 0.12s wwang pts/2 :0.0 11Mar13 22:56m 0.21s 0.21s bashwang pts/3 :0.0 12Mar13 6days 0.18s 50.76s gnome-terminal[wang@localhost 案頭]$ userswang wang wang wang wang[wang@localhost 案頭]$ last //登入工作階段資訊wang pts/1 :0.0 Mon Mar 25 17:02 still logged in wang pts/4 :0.0 Fri Mar 22 17:09 - 17:02 (2+23:52) ……[wang@localhost 案頭]$ last reboot //擷取重啟會話資訊reboot system boot 2.6.29.4-167.fc1 Wed Mar 6 14:43 (21+19:31) reboot system boot 2.6.29.4-167.fc1 Sun Jan 27 09:07 (60+01:07) reboot system boot 2.6.29.4-167.fc1 Sat Jan 26 18:13 (60+16:02) reboot system boot 2.6.29.4-167.fc1 Sat Jan 26 18:08 (60+16:06) reboot system boot 2.6.29.4-167.fc1 Thu Jan 10 20:50 (76+13:24) reboot system boot 2.6.29.4-167.fc1 Thu Jan 10 04:31 (77+05:44) wtmp begins Thu Jan 10 04:31:04 2013[wang@localhost 案頭]$ last USER //獲得單個使用者登入工作階段資訊 wtmp begins Thu Jan 10 04:31:04 2013[wang@localhost 案頭]$ lastb //列出登入系統失敗的使用者相關資訊lastb: /var/log/btmp: Permission denied[wang@localhost 案頭]$ su密碼:[root@localhost 案頭]# lastbwang tty1 :0 Tue Mar 5 14:44 - 14:44 (00:00) btmp begins Tue Mar 5 14:44:04 2013 4.進程資訊[wang@localhost 案頭]$ ps PID TTY TIME CMD 7128 pts/1 00:00:00 bash 7943 pts/1 00:00:00 bash32625 pts/1 00:00:00 bash32651 pts/1 00:00:00 ps[wang@localhost 案頭]$ ps -fUID PID PPID C STIME TTY TIME CMDwang 7128 3023 0 Mar28 pts/1 00:00:00 bashwang 7943 7942 0 Mar28 pts/1 00:00:00 bashwang 32625 32623 0 13:08 pts/1 00:00:00 bashwang 32654 32625 0 13:08 pts/1 00:00:00 ps -f[wang@localhost 案頭]$ ps -eo comm,pcpu | head //comm表示命令名,pupc表示cpu使用率 ,還有其他參數COMMAND %CPUinit 0.0kthreadd 0.0migration/0 0.0ksoftirqd/0 0.0watchdog/0 0.0cpuset 0.0events/0 0.0work_on_cpu/0 0.0khelper 0.0參數列表:pcpu cpu佔用率pid 進程IDppid 父進程IDpmem 記憶體使用量率comm 可執行檔cmd 簡單命令user 啟動進程的使用者nice 優先順序time 累計的cpu時間etime 進程啟動後度過的時間tty 所關聯的TTY裝置euid 有效使用者IDstat 進程狀態 [wang@localhost 案頭]$ ps -eo comm,pcpu --sort -pcpu | head //按pcpu降序排列COMMAND %CPUXorg 0.1gedit 0.0ata/0 0.0pulseaudio 0.0nautilus 0.0python 0.0vmtoolsd 0.0gnome-terminal 0.0ibus-daemon 0.0[wang@localhost 案頭]$ ps -C bash PID TTY TIME CMD 3025 pts/0 00:00:00 bash 3123 pts/0 00:00:00 bash……[wang@localhost 案頭]$ pgrep bash30253123……[wang@localhost 案頭]$ ps -eo pid,cmd e | tail -n 3 //顯示進程的環境變數 [wang@localhost 案頭]$ kill 進程號[wang@localhost 案頭]$ kill -9 進程號 //強制殺死進程[wang@localhost 案頭]$ killall 進程名字 [wang@localhost 案頭]$ killall -9 進程名字 [wang@localhost 案頭]$ kill %進程號 //殺死後台運行進程 [wang@localhost 案頭]$ top //動態查看cpu使用率[wang@localhost 案頭]$ watch -n 1 -d 'ps' //每1秒更新一次 -d 會顯示出前後的差異 5.日誌資訊/var/log/boot.log 系統啟動資訊/var/log/httpd Apache Web伺服器日誌/var/log/messages 發布核心啟動資訊/var/log/auth.log 使用者認證日誌/var/log/dmesg 系統開機記錄var/log/mail.log 郵件伺服器日誌var/log/Xorg.0.log X伺服器日誌var/log/wtmp 使用者登入工作階段記錄 [wang@localhost 案頭]$ logger This is a test[wang@localhost 案頭]$ tail -n -1 /var/log/messagestail: 無法開啟 “/var/log/messages” 讀取資料: 許可權不夠[wang@localhost 案頭]$ su密碼:[root@localhost 案頭]# tail -n -1 /var/log/messagesMar 29 09:39:47 localhost dhclient: bound to 192.168.126.148 -- renewal in 890 seconds.[root@localhost 案頭]# logger This is a test[root@localhost 案頭]# tail -n -1 /var/log/messagesMar 29 09:40:11 localhost wang: This is a test [wang@localhost 案頭]$ cat a.txt | wall //向所有當前登入使用者的終端寫入訊息 Broadcast message from wang (Fri Mar 29 13:48:43 2013): abcdefg 6.系統資訊[wang@localhost 案頭]$ hostname //當前系統主機名稱localhost.wang[wang@localhost 案頭]$ uname -n //linux核心版本和硬體架構資訊localhost.wang[wang@localhost 案頭]$ uname -a //核心發布版本Linux localhost.wang 2.6.29.4-167.fc11.i686.PAE #1 SMP Wed May 27 17:28:22 EDT 2009 i686 athlon i386 GNU/Linux[wang@localhost 案頭]$ uname -m //主控件類型i686[wang@localhost 案頭]$ cat /proc/cpuinfo //處理器資訊[wang@localhost 案頭]$ cat /proc/meminfo //記憶體資訊[wang@localhost 案頭]$ cat /proc/partitions //系統磁碟分割資訊 7.檔案查詢[wang@localhost 案頭]$ which ls //查看可執行檔的位置alias ls='ls --color=auto'/bin/ls[wang@localhost 案頭]$ whereis ls //查看檔案的位置ls: /bin/ls /usr/share/man/man1/ls.1.gz /usr/share/man/man1p/ls.1p.gz-b : 只找二進位檔案 -m: 只找在說明檔案manual路徑下的檔案 -s : 只找source源檔案 -u : 沒有說明文檔的檔案[wang@localhost 案頭]$ locate passwd //是在資料庫裡尋找,資料庫大至每天更新一次 locate: can not stat () `/var/lib/mlocate/mlocate.db': 沒有那個檔案或目錄locate 是在資料庫裡尋找,資料庫大至每天更新一次 [wang@localhost 案頭]$ whatis lsls [] (1) - list directory contentsls [] (1p) - list directory contents[wang@localhost 案頭]$ uptime 13:43:00 up 6 days, 5:25, 5 users, load average: 0.00, 0.00, 0.00