常用Linux系統資源查看命令
[[參考文檔]](https://segmentfault.com/a/1190000014789482?utm_source=index-hottest)
[TOC]
## 查看系統版本
方法1:
~~~
[root@linuxidc ~]# lsb_release -a
LSB Version: :core-4.1-amd64:core-4.1-noarch:cxx-4.1-amd64:cxx-4.1-noarch:desktop-4.1-amd64:desktop-4.1-noarch:languages-4.1-amd64:languages-4.1-noarch:printing-4.1-amd64:printing-4.1-noarch
Distributor ID: CentOS
Description: CentOS Linux release 7.2.1511 (Core)
Release: 7.2.1511
Codename: Core
~~~
方法2:
~~~
[root@linuxidc ~]# cat /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"
~~~
## `vmstat` 命令(監控系統資源)
參數:
`-a`:使用 inactive/active(活躍與否)替代 buffer/cache 的內存輸出信息;
`-f`:開機到目前為止系統復制(fork)的進程數;
`-s`:將一些事件(開機至目前為止) 導致的內存變化情況列表說明;
`-S`:后面可以接單位,讓顯示的數據有單位.例如K/M 取代 bytes 的容量;
`-d`:列出磁盤的讀寫總量統計表;
`-p`:后面列出分區,可顯示該分區的讀寫總量統計表。
~~~bash
[vagrant~] ]$vmstat 1 3
procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
r b swpd free buff cache si so bi bo in cs us sy id wa st
0 0 0 259312 28320 108272 0 0 30 4 41 202 0 0 99 0 0
0 0 0 259304 28320 108272 0 0 0 0 50 93 0 1 99 0 0
0 0 0 259300 28320 108272 0 0 0 0 33 73 0 0 100 0 0
~~~
## `dmesg` 命令(開機時內核檢查信息)
1. 輸出所有內核開機時的信息
**由于輸出數據較多,可用 less 命令查看:`dmesg | less`**
~~~bash
[vagrant~] ]$dmesg | less
[ 0.000000] Initializing cgroup subsys cpuset
[ 0.000000] Initializing cgroup subsys cpu
[ 0.000000] Initializing cgroup subsys cpuacct
[ 0.000000] Linux version 3.13.0-24-generic (buildd@panlong) (gcc version 4.8.2 (Ubuntu 4.8.2-19ubuntu1) ) #46-Ubuntu SMP Thu Apr 10 19:11:08 UTC 2014 (Ubuntu 3.13.0-24.46-generic 3.13.9)
[ 0.000000] Command line: BOOT_IMAGE=/boot/vmlinuz-3.13.0-24-generic root=UUID=64ecdf77-db7b-48a8-9066-abfb837f2e24 ro
...省略n行...
[ 0.000000] Zone ranges:
[ 0.000000] DMA [mem 0x00001000-0x00ffffff]
[ 0.000000] DMA32 [mem 0x01000000-0xffffffff]
[ 0.000000] Normal empty
[ 0.000000] Movable zone start for each node
:
~~~
2. 查找包含指定內容的信息
**查找開機時CPU的相關信息:`dmesg | grep CPU`**
~~~
[vagrant~] ]$dmesg | grep CPU
[ 0.000000] CPU MTRRs all blank - virtualized system.
[ 0.000000] ACPI: SSDT 000000001fff02a0 0001CC (v01 VBOX VBOXCPUT 00000002 INTL 20100528)
[ 0.000000] smpboot: Allowing 1 CPUs, 0 hotplug CPUs
[ 0.000000] setup_percpu: NR_CPUS:256 nr_cpumask_bits:256 nr_cpu_ids:1 nr_node_ids:1
[ 0.000000] PERCPU: Embedded 29 pages/cpu @ffff88001fc00000 s86336 r8192 d24256 u2097152
[ 0.000000] SLUB: HWalign=64, Order=0-3, MinObjects=0, CPUs=1, Nodes=1
[ 0.000000] RCU restricting CPUs from NR_CPUS=256 to nr_cpu_ids=1.
[ 0.000000] Offload RCU callbacks from all CPUs
[ 0.000000] Offload RCU callbacks from CPUs: 0.
[ 0.004582] mce: CPU supports 0 MCE banks
[ 0.078327] smpboot: CPU0: AMD A10-7870K Radeon R7, 12 Compute Cores 4C+8G (fam: 15, model: 38, stepping: 01)
[ 0.083763] x86: Booted up 1 node, 1 CPUs
[ 0.844045] microcode: CPU0: patch_level=0x06000626
[ 0.908285] ledtrig-cpu: registered to indicate activity on CPUs
[ 3.624181] CPU: 0 PID: 293 Comm: systemd-udevd Tainted: GF O 3.13.0-24-generic #46-Ubuntu
~~~
**查找開機時硬盤的相關信息:`dmesg | grep -i hd`**
~~~
[vagrant~] ]$dmesg | grep -i hd
[ 0.084000] NMI watchdog: disabled (cpu0): hardware events not enabled
~~~
**查找開機時網卡的相關信息:`dmesg | grep -i eth`**
~~~
[vagrant~] ]$dmesg | grep -i eth
~~~
## `free` 命令(查看內存使用狀態)
~~~
[vagrant~] ]$free
total used free shared buffers cached
Mem: 501832 248436 253396 2372 33216 109116
-/+ buffers/cache: 106104 395728
Swap: 522236 0 522236
[vagrant~] ]$free -b
total used free shared buffers cached
Mem: 513875968 254517248 259358720 2428928 34029568 111755264
-/+ buffers/cache: 108732416 405143552
Swap: 534769664 0 534769664
[vagrant~] ]$free -k
total used free shared buffers cached
Mem: 501832 248576 253256 2372 33232 109136
-/+ buffers/cache: 106208 395624
Swap: 522236 0 522236
[vagrant~] ]$free -m
total used free shared buffers cached
Mem: 490 242 247 2 32 106
-/+ buffers/cache: 103 386
Swap: 509 0 509
[vagrant~] ]$free -g
total used free shared buffers cached
Mem: 0 0 0 0 0 0
-/+ buffers/cache: 0 0
Swap: 0 0 0
~~~
## 查看CPU信息
~~~
[vagrant~] ]$cat /proc/cpuinfo
processor : 0
vendor_id : AuthenticAMD
cpu family : 21
model : 56
model name : AMD A10-7870K Radeon R7, 12 Compute Cores 4C+8G
stepping : 1
microcode : 0x6000626
cpu MHz : 3892.536
cache size : 2048 KB
physical id : 0
siblings : 1
core id : 0
cpu cores : 1
apicid : 0
initial apicid : 0
fpu : yes
fpu_exception : yes
cpuid level : 13
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 syscall nx mmxext fxsr_opt rdtscp lm rep_good nopl extd_apicid pni pclmulqdq monitor ssse3 cx16
sse4_1 sse4_2 popcnt aes xsave avx lahf_lm cr8_legacy abm sse4a misalignsse 3dnowprefetch arat fsgsbase
bogomips : 7785.07
TLB size : 1536 4K pages
clflush size : 64
cache_alignment : 64
address sizes : 48 bits physical, 48 bits virtual
power management:
~~~
## `uptime` 命令(顯示系統的啟動時間和平均負載)
輸出結果與`top`命令和`w`的第一行相同。
~~~
[vagrant~] ]$uptime
02:35:11 up 2:43, 1 user, load average: 0.00, 0.06, 0.07
[vagrant~] ]$w
02:35:13 up 2:43, 1 user, load average: 0.00, 0.06, 0.07
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
vagrant pts/0 10.0.2.2 23:52 1.00s 0.49s 0.00s w
[vagrant~] ]$top
top - 02:36:52 up 2:45, 1 user, load average: 0.00, 0.04, 0.06
Tasks: 80 total, 1 running, 79 sleeping, 0 stopped, 0 zombie
%Cpu(s): 0.1 us, 0.3 sy, 0.0 ni, 99.5 id, 0.1 wa, 0.0 hi, 0.0 si, 0.0 st
KiB Mem: 501832 total, 251856 used, 249976 free, 35560 buffers
KiB Swap: 522236 total, 0 used, 522236 free. 109596 cached Mem
...省略n行...
~~~
## `uname`查看系統與內核相關信息
### `uname` 命令
~~~
[root@tatahy-lamp ~]# uname [-asrmpi]
~~~
參數:
`-a`:所有系統相關的信息,包括下面的數據都會被列出來;
`-s`:系統內核名稱;
`-r`:內核的版本;
`-m`:本系統的硬件名稱,例如1686或x86_64等;
`-p`:CPU的類型,與`-m`類似,只是顯示的是 CPU 的類型;
`-i`:硬件的平臺(ix86)。
示例:
~~~
[root@tatahy-lamp ~]# uname -a
Linux tatahy-lamp 3.10.0-123.9.3.el7.x86_64 #1 SMP Thu Nov 6 15:06:03 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux
~~~
主機內核:Linux
主機名:tata-lamp
內核版本:3.10.0-123.9.3.el7.x86_64
內核版本創建時間:Thu Nov 6 15:06:03 UTC 2014
本系統的硬件名稱:x86_64
CPU的類型:x86_64
適用的硬件平臺:x86_64
### 1 判斷當前系統的位數
~~~
[vagrant~] ]$file /bin/ls
/bin/ls: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.24, BuildID[sha1]=64d095bc6589dd4bfbf1c6d62ae985385965461b, stripped
~~~
### 2 查看當前Linux系統的發行版本
~~~
[vagrant~] ]$lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 14.04 LTS
Release: 14.04
Codename: trusty
~~~
## `lsof` 命令(list open file 列出進程打開或使用的文件信息)
### 1 查詢系統中所有進程調用的文件
~~~
[vagrant~] ]$sudo lsof | less
COMMAND PID TID USER FD TYPE DEVICE SIZE/OFF NODE NAME
init 1 root cwd DIR 8,1 4096 2 /
init 1 root rtd DIR 8,1 4096 2 /
init 1 root txt REG 8,1 265848 1835051 /sbin/init
init 1 root mem REG 8,1 47712 1053013 /lib/x86_64-linux-gnu/libnss_files-2.19.so
init 1 root mem REG 8,1 47760 1053033
...省略n行...
khelper 13 root cwd DIR 8,1 4096 2 /
khelper 13 root rtd DIR 8,1 4096 2 /
khelper 13 root txt unknown /proc/13/exe
:
~~~
### 2 查詢某個文件被哪個進程調用
~~~
[vagrant~] ]$sudo lsof /sbin/init
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
init 1 root txt REG 8,1 265848 1835051 /sbin/init
~~~
### 3 查詢某個進程調用了哪些文件
~~~
[vagrant~] ]$sudo lsof -c nginx
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
nginx 1000 root cwd DIR 8,1 4096 2 /
nginx 1000 root rtd DIR 8,1 4096 2 /
nginx 1000 root txt REG 8,1 873176 1573477 /usr/sbin/nginx
nginx 1000 root mem REG 8,1 47712 1053013
...省略n行...
nginx 1004 www-data 14u unix 0xffff88001b776000 0t0 9508 socket
nginx 1004 www-data 15u 0000 0,9 0 5249 anon_inode
~~~
### 4 查詢某個用戶的進程調用了哪些文件
~~~
[vagrant~] ]$sudo lsof -u vagrant
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
sshd 1516 vagrant cwd DIR 8,1 4096 2 /
sshd 1516 vagrant rtd DIR 8,1 4096 2 /
sshd 1516 vagrant txt REG 8,1 766784 1583210 /usr/sbin/sshd
sshd 1516 vagrant DEL REG 0,4 11386 /dev/zero
sshd 1516 vagrant mem REG 8,1 14464 1048876 /lib/x86_64-linux-gnu/security/pam_env.so
...省略n行...
bash 1517 vagrant 0u CHR 136,0 0t0 3 /dev/pts/0
bash 1517 vagrant 1u CHR 136,0 0t0 3 /dev/pts/0
bash 1517 vagrant 2u CHR 136,0 0t0 3 /dev/pts/0
bash 1517 vagrant 255u CHR 136,0 0t0 3 /dev/pts/0
~~~
### 5 查詢某個PID的進程調用了哪些文件
~~~
[vagrant~] ]$sudo lsof -p 1003
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
nginx 1003 www-data cwd DIR 8,1 4096 2 /
nginx 1003 www-data rtd DIR 8,1 4096 2 /
nginx 1003 www-data txt REG 8,1 873176 1573477 /usr/sbin/nginx
nginx 1003 www-data mem REG 8,1 47712 1053013 /lib/x86_64-linux-gnu/libnss_files-2.19.so
...省略n行...
nginx 1003 www-data 6u IPv4 9497 0t0 TCP *:http (LISTEN)
nginx 1003 www-data 7u IPv6 9498 0t0 TCP *:http (LISTEN)
nginx 1003 www-data 13u 0000 0,9 0 5249 anon_inode
~~~
### 更多 lsof 的命令:
~~~
lsof -i:8080:查看8080端口占用
lsof abc.txt:顯示開啟文件abc.txt的進程
lsof -c abc:顯示abc進程現在打開的文件
lsof -c -p 1234:列出進程號為1234的進程所打開的文件
lsof -g gid:顯示歸屬gid的進程情況
lsof +d /usr/local/:顯示目錄下被進程開啟的文件
lsof +D /usr/local/:同上,但是會搜索目錄下的目錄,時間較長
lsof -d 4:顯示使用fd為4的進程
lsof -i -U:顯示所有打開的端口和UNIX domain文件
~~~
## `ps` 靜態查看進程
### 1 `ps -l`只查看自己bash(操作環境)有關的進程
~~~
[root@tatahy-lamp ~]# ps -l
F S UID PID PPID C PRI NI ADDR SZ WCHAN TTY TIME CMD
4 S 0 7092 7090 0 80 0 - 29103 wait pts/0 00:00:00 bash
0 R 0 7124 7092 0 80 0 - 34334 - pts/0 00:00:00 ps
~~~
### 2 `ps aux`或`ps -lA`查看系統所有進程
~~~
[root@tatahy-lamp ~]# ps aux
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.7 65028 27552 ? Ss 2018 16:46 /usr/lib/systemd/systemd --switched-root --system -
root 2 0.0 0.0 0 0 ? S 2018 0:04 [kthreadd]
root 3 0.0 0.0 0 0 ? S 2018 0:16 [ksoftirqd/0]
root 5 0.0 0.0 0 0 ? S< 2018 0:00 [kworker/0:0H]
root 6 0.0 0.0 0 0 ? S 2018 0:00 [kworker/u4:0]
root 7 0.0 0.0 0 0 ? S 2018 0:01 [migration/0]
root 8 0.0 0.0 0 0 ? S 2018 0:00 [rcu_bh]
......
mysql 4492 0.0 0.0 113120 1592 ? Ss 2018 0:00 /bin/sh /usr/bin/mysqld_safe
mysql 4692 0.0 3.0 896772 119540 ? Sl 2018 108:02 /usr/sbin/mysqld --basedir=/usr --datadir=/var/lib/
apache 32405 0.0 0.2 606700 10152 ? S Mar24 0:00 /usr/sbin/httpd -DFOREGROUND
apache 32406 0.0 0.2 606700 10644 ? S Mar24 0:00 /usr/sbin/httpd -DFOREGROUND
apache 32407 0.0 0.2 606700 10152 ? S Mar24 0:00 /usr/sbin/httpd -DFOREGROUND
apache 32408 0.0 0.2 606700 10152 ? S Mar24 0:00 /usr/sbin/httpd -DFOREGROUND
apache 32409 0.0 0.2 606700 10152 ? S Mar24 0:00 /usr/sbin/httpd -DFOREGROUND
~~~
### 3 `ps axjf`查看系統進程樹
~~~
[root@tatahy-lamp ~]# ps axjf
PPID PID PGID SID TTY TPGID STAT UID TIME COMMAND
0 2 0 0 ? -1 S 0 0:04 [kthreadd]
2 3 0 0 ? -1 S 0 0:16 \_ [ksoftirqd/0]
2 5 0 0 ? -1 S< 0 0:00 \_ [kworker/0:0H]
2 6 0 0 ? -1 S 0 0:00 \_ [kworker/u4:0]
2 7 0 0 ? -1 S 0 0:01 \_ [migration/0]
2 8 0 0 ? -1 S 0 0:00 \_ [rcu_bh]
......
1 741 741 741 ? -1 Ss 0 13:19 /usr/sbin/httpd -DFOREGROUND
741 32405 741 741 ? -1 S 48 0:00 \_ /usr/sbin/httpd -DFOREGROUND
741 32406 741 741 ? -1 S 48 0:00 \_ /usr/sbin/httpd -DFOREGROUND
741 32407 741 741 ? -1 S 48 0:00 \_ /usr/sbin/httpd -DFOREGROUND
741 32408 741 741 ? -1 S 48 0:00 \_ /usr/sbin/httpd -DFOREGROUND
741 32409 741 741 ? -1 S 48 0:00 \_ /usr/sbin/httpd -DFOREGROUND
1 755 755 755 ? -1 Ss 0 0:12 /usr/sbin/sshd -D
755 7090 7090 7090 ? -1 Ss 0 0:00 \_ sshd: root@pts/0
7090 7092 7092 7092 pts/0 7133 Ss 0 0:00 \_ -bash
7092 7133 7133 7092 pts/0 7133 R+ 0 0:00 \_ ps axjf
......
1 4492 4492 4492 ? -1 Ss 27 0:00 /bin/sh /usr/bin/mysqld_safe
4492 4692 4492 4492 ? -1 Sl 27 108:02 \_ /usr/sbin/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib64/mysql/plugin --log-error=/var/log/mysqld.log --pid-file=/var/
1 18460 18460 18460 ? -1 S<sl 0 22:57 /usr/local/aegis/aegis_update/AliYunDunUpdate
1 18481 18481 18481 ? -1 S<sl 0 263:26 /usr/local/aegis/aegis_client/aegis_10_59/AliYunDun
1 16057 16057 16057 ? -1 Ssl 0 38:33 /usr/local/cloudmonitor/CmsGoAgent.linux-amd64
~~~
### 4 `pstree`進程樹/相關性
~~~
[root@tatahy-lamp ~]# pstree -Aup
systemd(1)-+-AliYunDun(18481)-+-{AliYunDun}(18482)
| |-{AliYunDun}(18483)
| |-{AliYunDun}(18494)
| |-{AliYunDun}(18495)
| |-{AliYunDun}(18496)
| |-{AliYunDun}(18497)
| |-{AliYunDun}(18498)
| |-{AliYunDun}(18499)
| |-{AliYunDun}(18500)
| |-{AliYunDun}(18501)
| |-{AliYunDun}(18502)
| |-{AliYunDun}(18503)
| |-{AliYunDun}(18504)
| |-{AliYunDun}(18505)
| |-{AliYunDun}(18506)
| |-{AliYunDun}(18507)
| |-{AliYunDun}(18508)
| `-{AliYunDun}(19062)
|-AliYunDunUpdate(18460)-+-{AliYunDunUpdate}(18462)
| |-{AliYunDunUpdate}(18463)
| `-{AliYunDunUpdate}(18466)
|-CmsGoAgent.linu(16057)-+-{CmsGoAgent.linu}(16058)
| |-{CmsGoAgent.linu}(16059)
| |-{CmsGoAgent.linu}(16060)
| |-{CmsGoAgent.linu}(16061)
| |-{CmsGoAgent.linu}(16062)
| |-{CmsGoAgent.linu}(16063)
| |-{CmsGoAgent.linu}(16067)
| |-{CmsGoAgent.linu}(16068)
| |-{CmsGoAgent.linu}(16076)
| |-{CmsGoAgent.linu}(16077)
| |-{CmsGoAgent.linu}(16091)
| |-{CmsGoAgent.linu}(16159)
| `-{CmsGoAgent.linu}(16160)
|-aliyun-service(30690)-+-{aliyun-service}(30691)
| |-{aliyun-service}(30692)
| |-{aliyun-service}(30693)
| |-{aliyun-service}(30694)
| `-{aliyun-service}(30695)
~~~
### 5 找出與`cron`和`syslog`服務有關的PID
~~~
[root@tatahy-lamp ~]# ps aux | egrep '(cron|syslog)'
root 481 0.0 0.2 396480 10424 ? Ssl 2018 0:40 /usr/sbin/rsyslogd -n
root 517 0.0 0.0 126300 1604 ? Ss 2018 1:17 /usr/sbin/crond -n
root 7142 0.0 0.0 108040 888 pts/0 S+ 14:49 0:00 egrep --color=auto (cron|syslog)
~~~
### 6 僵尸(zombie)進程
通常, 造成僵尸進程的成因是因為該進程應該已經執行完畢,或者是因故應該要終止了,但是該進程的父進程卻無法完整將該進程結束掉,而造成那個進程一直存在內存當中。
如果你發現在某個進程的 CMD 后面還接上`<defunct>`時, 就代表該進程是僵尸進程, 例如:

當系統不穩定的時候就容易造成所謂的僵尸進程。可能的原因:
* 程序寫得不好
* 用戶的操作習慣不良等
如果系統中很多僵尸進程時,要找出該進程的父進程,然后好好做個跟蹤,好好進行主機的環境優化。 看看有什么地方需要改善的,不要只是直接將它殺掉而已呢! 不然的話, 萬一它一直產生, 那可就麻煩了!
事實上,通常僵尸進程都已經無法控管,而直接是交給`init`這個程序來負責了,偏偏`init`是系統第一個執行的程序,它是所有進程的父進程! 我們無法殺掉該進程 ( 殺掉它,系統就死掉了)。
所以,如果產生僵尸進程,而系統過一陣子還沒有辦法通過內核非經常性的特殊處理來將該進程刪除時,那你只好通過`reboot`的方式來將該進程抹去了!
## `top` 動態查看進程
~~~
[root@tatahy-lamp ~]# top [-d 數字] | top [-bnp]
~~~
參數:
`-d`:后面可以接秒數,就是整個進程界面更新的秒數,默認是5秒。
`-b`:以批次的方式執行top,還有更多的參數可以使用。
通常會搭配數據流重定向來將批處理的結果輸出成為文件。
`-n`:與`-b`搭配,意義是,需要進行幾次 top 的輸出結果。
`-p`:指定某些個 PID來進行查看監測而已。
在top執行過程當中可以使用的按鍵命令:
`?`:顯示在 top 當中可以輸入的按鍵命令;
`P`:以CPU 的使用資源排序顯示;
`M`:以內存的使用資源排序顯示;
`N`:以PID 來排序;
`T`:由該進程使用的 CPU 時間累積(TIME+) 排序;
`k`: 給予某個 PID一個信號(signal);
`r`:給予某個 PID 重新制定一個 nice 值;
`q`:離開top 軟件的按鍵。
### 1 查看自己bash的PID信息
~~~
[root@tatahy-lamp ~]# echo $$
7092
[root@tatahy-lamp ~]# top -d 2 -p 7092
top - 15:11:06 up 275 days, 4:09, 2 users, load average: 0.00, 0.01, 0.05
Tasks: 1 total, 0 running, 1 sleeping, 0 stopped, 0 zombie
%Cpu(s): 0.2 us, 0.2 sy, 0.0 ni, 99.5 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
KiB Mem: 3883400 total, 1799432 used, 2083968 free, 178004 buffers
KiB Swap: 0 total, 0 used, 0 free. 1032172 cached Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
7092 root 20 0 116412 3148 1672 S 0.0 0.1 0:00.05 bash
~~~
### 2 將`top`的信息存儲到指定文件
~~~
[root@tatahy-lamp ~]# top -b -n 2 >/tmp/top.txt
[root@tatahy-lamp ~]# cat /tmp/top.txt
~~~
## `netstat` 跟蹤網絡
常被用于網絡監控。
~~~
[root@tatahy-lamp ~]# netstat [-atunlp]
~~~
參數:
`-a`:將目前系統上所有的連接,監聽,Socket 數據都列出來;
`-t`:列出 tcp 網絡數據包的數據;
`-u`:列出udp 網絡數據包的數據;
`-n`:不列出進程的服務名稱,以端口號(port number)來顯示;
`-l`:列出目前正在網絡監聽 (listen)的服務;
`-p`:列出該網絡服務的進程 PID。
### 1 列出目前系統已經新建的網絡連接與unix socket狀態
~~~
[root@tatahy-lamp ~]# netstat
Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 240 tatahy-lamp:ssh 121.33.248.6:65422 ESTABLISHED
tcp 0 0 tatahy-lamp:ssh 121.33.248.6:65044 ESTABLISHED
tcp 0 0 tatahy-lamp:40759 100.100.30.25:http ESTABLISHED
Active UNIX domain sockets (w/o servers)
Proto RefCnt Flags Type State I-Node Path
unix 2 [ ] DGRAM 9664 /run/systemd/shutdownd
unix 2 [ ] DGRAM 6881 /run/systemd/notify
unix 5 [ ] DGRAM 6902 /run/systemd/journal/socket
unix 8 [ ] DGRAM 6904 /dev/log
unix 2 [ ] DGRAM 11514 /var/run/nscd/socket
unix 3 [ ] STREAM CONNECTED 11807
unix 3 [ ] STREAM CONNECTED 10822055
~~~
### 2 找出目前系統上已在監聽的網絡連接及其PID
~~~
[root@tatahy-lamp ~]# netstat -tlnp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:8080 0.0.0.0:* LISTEN 741/httpd
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 755/sshd
tcp6 0 0 :::3306 :::* LISTEN 4692/mysqld
~~~
## 查看系統的服務和對應端口號
查看文件`/etc/services`內容就可以知道當前系統的服務和對應端口號。也可以通過修改該文件中的內容來更改某個服務的端口號。
~~~
[root@tatahy-lamp selinux]# cat /etc/services | grep 'mysql'
mysql 3306/tcp # MySQL
mysql 3306/udp # MySQL
mysql-cluster 1186/tcp # MySQL Cluster Manager
mysql-cluster 1186/udp # MySQL Cluster Manager
mysql-cm-agent 1862/tcp # MySQL Cluster Manager Agent
mysql-cm-agent 1862/udp # MySQL Cluster Manager Agent
mysql-im 2273/tcp # MySQL Instance Manager
mysql-im 2273/udp # MySQL Instance Manager
mysql-proxy 6446/tcp # MySQL Proxy
mysql-proxy 6446/udp # MySQL Proxy
~~~
>[warning] 關閉服務/端口:就關閉(kill)提供服務的進程就可以了。
## `service`(進程)顯示/管理系統目前的服務
CentOS中提供`service`可以用來啟動`stand alone`類型的服務的腳本。`service`就是一個腳本(路徑:`/sbin/service`),它通過分析`service`后面的參數再到`/etc/init.d/`目錄下取得正確的服務來`start`或`stop`。
~~~
[root@www~]# service [service name](start|stop|restartl...)
[root@www~]# service --status-all
~~~
`–status-all` 將會運行所有的啟動腳本來顯示各個服務的運行狀態:
參數:
`service name`:即是需要啟動的服務名稱,需與 `/etc/init.d/` 對應;
`startl...`:即是該服務要進行的工作;
`--status-all`:將系統所有的`stand alone`的服務狀態全部列出來。
`systemctl`,`init`,`systemd`是什么??
- WebAPP
- Linux Command
- 入門
- 處理文件
- 查找文件單詞
- 環境
- 聯網
- Linux
- Linux目錄配置標準:FHS
- Linux文件與目錄管理
- Linux賬號管理與ACL權限設置
- Linux系統資源查看
- 軟件包管理
- Bash
- Daemon/Systemd
- ftp
- Apache
- MySQL
- Command
- Replication
- mysqld
- remote access
- remark
- 限制
- PHP
- String
- Array
- Function
- Class
- File
- JAVA
- Protocals
- http
- mqtt
- IDE
- phpDesigner
- eclipse
- vscode
- Notepad++
- WebAPI
- Javasript
- DOM
- BOM
- Event
- Class
- Module
- Ajax
- Fetch
- Promise
- async/await
- Statements and declarations
- Function
- Framwork
- jQurey
- Types
- Promise
- BootStrap
- v4
- ThinkPHP5
- install
- 定時任務
- CodeIgniter
- React.js
- node.js
- npm
- npm-commands
- npm-folder
- package.json
- Docker and private modules
- module
- webpack.js
- install
- configuration
- package.json
- entry
- modules
- plugins
- Code Splitting
- loaders
- libs
- API
- webpack-cli
- Vue.js
- install
- Compile
- VueAPI
- vuex
- vue-router
- vue-devtools
- vue-cli
- vue-loader
- VDOM
- vue-instance
- components
- template
- Single-File Components
- props
- data
- methods
- computed
- watch
- Event-handling
- Render Func
- remark
- 案例學習
- bootstrap-vue
- modal
- fontAwesome
- Hosting Font Awesome Yourself
- using with jquery
- using with Vue.js
- HTML
- CSS
- plugins
- Chart.js
- D3.js
- phpSpreadSheet
- Guzzle
- Cmder
- Git
- git命令
- git流程
- Postman
- Markdown
- Regular Expressions
- PowerDesigner
- 附錄1-學習資源