1. 程式人生 > 程式設計 >linux 檢視系統環境資訊命令彙總

linux 檢視系統環境資訊命令彙總

前言

本文所有 shell 命令均在阿里雲ECS伺服器上測試過,以確保每行程式碼都是百分百可用的。測試使用的伺服器配置資訊如下:

  • 2核8G記憶體 40G SSD雲盤
  • CentOS 7.6 64位

為了精簡篇幅,同時又方便直觀的瞭解各命令執行的效果。本文對命令輸出的處理方式分為以下三種:

  1. 在提供必要的輸出資訊的命令列前加上 $,代表該行為要執行的命令,同時也意味著緊接著的是該命令執行後的輸出資訊,直到遇到下一個以 $ 開始的行,用於區分命令和命令的輸出,實際執行不用加 $

  2. 未加 $ 的整個程式碼塊,都不提供輸出資訊;

  3. 輸出資訊量大,但又有一些影響我們瞭解重要資訊的輸出,通過管道,使用實用程式 grep

    過濾重要資訊,格式:$ command | grep xxx

1. 檢視伺服器配置

1.1 檢視系統版本

方式 1:

$ cat /etc/redhat-release
CentOS Linux release 7.6.1810 (Core) 
複製程式碼

方式2:

lsb_release -a
複製程式碼

1.2 檢視CPU和記憶體

cpu:

$ cat /proc/cpuinfo | grep name
model name      : Intel(R) Xeon(R) Platinum 8163 CPU @ 2.50GHz
model name      : Intel(R) Xeon(R) Platinum 8163 CPU @ 2.50GHz
複製程式碼

記憶體:

$ cat /proc/meminfo | grep Mem
MemTotal:        8009180 kB
MemFree:         7633240 kB
MemAvailable:    7657060 kB
複製程式碼

1.3 檢視磁碟和分割槽

磁碟:

$ fdisk -l | grep -E '.+/dev/'
磁碟 /dev/vda:42.9 GB,42949672960 位元組,83886080 個扇區
複製程式碼

分割槽:

$ df -TH | grep ^/dev
/dev/vda1      ext4       43G  1.8G   39G    5% /
複製程式碼

1.4 檢視網路卡和IP地址

網路卡:

$ lspci | grep -i eth
00:03.0 Ethernet controller: Red Hat,Inc. Virtio network device
複製程式碼

IP:

$ ip addr | grep -E "^[1-9]+|inet"
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    inet 127.0.0.1/8 scope host lo
2: eth0: <BROADCAST,MULTICAST,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
    inet 172.18.24.41/20 brd 172.18.31.255 scope global dynamic eth0
複製程式碼

1.5 檢視主機板製造商

$ dmidecode | grep -V -A2 "System Information"
System Information
        Manufacturer: Alibaba Cloud
        Product Name: Alibaba Cloud ECS
複製程式碼

2. 檢視防火牆

2.1 檢視防火牆服務

$ systemctl list-unit-files | grep firewalld
firewalld.service                             enabled
複製程式碼

啟用防火牆服務(開機啟動):

systemctl enable firewalld.service
複製程式碼

禁用防火牆服務:

systemctl disable firewalld.service
複製程式碼

2.2 檢視防火牆執行狀態

$ firewall-cmd --state
running
複製程式碼

開啟防火牆:

systemctl start firewalld.service
複製程式碼

關閉防火牆:

systemctl stop firewalld.service
複製程式碼

3. 檢視系統使用者和使用者組資訊

3.1 檢視系統使用者

$ cat /etc/passwd | grep bash
root:x:0:0:root:/root:/bin/bash
# 使用者名稱 :密碼 :使用者ID  :分組ID :註釋性描述   :使用者目錄    :登入Shell
# -------------------------------------------------------------
# root  :x   :0       :0     :root       :/root      :/bin/bash
複製程式碼

3.2 檢視使用者組

$ cat /etc/group | grep root
root:x:0:
# 組名  :密碼  :分組ID  :組內使用者列表
# root :x     :0      :
複製程式碼

3.3 檢視系統使用者密碼(加密)

$ cat /etc/shadow | grep root
root:*::0:99999:7:::
# 使用者名稱 :加密口令(密碼) :最後一次修改時間 :最小時間間隔 :最大時間間隔 :警告時間 :不活動時間 :失效時間 :標誌
# root  :*            :              :0          :99999      :7       :         :        :
複製程式碼

4. 檢視系統執行狀態

4.1 檢視當前執行的程式列表

ps aux
# 加 f 以樹狀顯示父子程式
ps aufx
複製程式碼

4.2 檢視實時程式資源佔用(CPU和記憶體)

top
複製程式碼

ctrl+c 退出。

4.3 系統執行狀態監控(CPU和IO)

安裝監控程式 dstat:

yum install dstat
複製程式碼

實時監控系統執行:

dstat
複製程式碼

ctrl+c 退出。