引言
對於系統和網絡管理員來講天天監控和調試Linux系統的性能問題是一項繁重的工做。這些命令行工具能夠在各類Linux系統下使用,能夠用於監控和查找產生性能問題的緣由。這個命令行工具列表提供了足夠的工具,您能夠挑選適用於您的監控場景的工具。
在許多Linux或者類Unix系統裏都有lsof命令,它經常使用於以列表的形式顯示全部打開的文件和進程。打開的文件包括磁盤文件、網絡套接字、管道、設備和進程。使用這條命令的主要情形之一就是在沒法掛載磁盤和顯示正在使用或者打開某個文件的錯誤信息的時候。node
經常使用的參數列表:[root@CentOS7 ~]# lsof -i :22 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME sshd 925 root 3u IPv4 18374 0t0 TCP *:ssh (LISTEN) sshd 925 root 4u IPv6 18383 0t0 TCP *:ssh (LISTEN) sshd 9452 root 3u IPv4 1169003 0t0 TCP CentOS7.2:ssh->192.168.56.1:61347 (ESTABLISHED)
[root@CentOS7 ~]# lsof -a -u root -d txt COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME systemd 1 root txt REG 0,34 1489960 56083 /usr/lib/systemd/systemd kthreadd 2 root txt unknown /proc/2/exe ksoftirqd 3 root txt unknown /proc/3/exe migration 7 root txt unknown /proc/7/exe rcu_bh 8 root txt unknown /proc/8/exe
經常使用熱鍵
linux
top - 11:20:12 up 2 days, 18:39, 3 users, load average: 0.00, 0.01, 0.05 Tasks: 173 total, 1 running, 172 sleeping, 0 stopped, 0 zombie %Cpu(s): 0.3/0.7 1[| ] KiB Mem : 500780 total, 13236 free, 249296 used, 238248 buff/cache KiB Swap: 8388604 total, 7848788 free, 539816 used. 200108 avail Mem PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 17166 root 20 0 0 0 0 S 0.3 0.0 0:09.05 kworker/0:2 19196 root 20 0 146144 2056 1420 R 0.3 0.4 0:02.25 top 23944 root 20 0 1239128 38724 8388 S 0.3 7.7 6:39.63 firefox 1 root 20 0 191492 4212 2328 S 0.0 0.8 0:27.20 systemd 2 root 20 0 0 0 0 S 0.0 0.0 0:02.22 kthreadd 3 root 20 0 0 0 0 S 0.0 0.0 7:11.69 ksoftirqd/0 7 root rt 0 0 0 0 S 0.0 0.0 0:00.00 migration/0 8 root 20 0 0 0 0 S 0.0 0.0 0:00.00 rcu_bh 9 root 20 0 0 0 0 S 0.0 0.0 0:00.00 rcuob/0 10 root 20 0 0 0 0 S 0.0 0.0 6:09.07 rcu_sched 11 root 20 0 0 0 0 S 0.0 0.0 14:11.36 rcuos/0 12 root rt 0 0 0 0 S 0.0 0.0 0:03.03 watchdog/0 13 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 khelper 14 root 20 0 0 0 0 S 0.0 0.0 0:00.00 kdevtmpfs 15 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 netns 16 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 perf
top - 11:22:12 up 2 days, 18:41, 3 users, load average: 0.00, 0.01, 0.05 Tasks: 173 total, 1 running, 172 sleeping, 0 stopped, 0 zombie %Cpu(s): 0.0 us, 0.3 sy, 0.0 ni, 99.7 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st KiB Mem : 60.0/500780 [|||||||||||||||||||||||||||||||||||||||||||||| ] KiB Swap: 6.4/8388604 [||||| ]
1:Def - 11:23:40 up 2 days, 18:42, 3 users, load average: 0.00, 0.01, 0.05 Tasks: 174 total, 1 running, 173 sleeping, 0 stopped, 0 zombie %Cpu(s): 0.7 us, 0.7 sy, 0.0 ni, 98.7 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st KiB Mem : 500780 total, 21124 free, 249376 used, 230280 buff/cache KiB Swap: 8388604 total, 7848788 free, 539816 used. 200072 avail Mem 1 PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 920 root 20 0 644044 11096 3360 S 0.3 2.2 10:28.34 daomonit 19196 root 20 0 146144 2056 1420 R 0.3 0.4 0:03.00 top 1 root 20 0 191492 4212 2328 S 0.0 0.8 0:27.20 systemd 2 PID PPID TIME+ %CPU %MEM PR NI S VIRT RES UID COMMAND 24416 2 0:03.29 0.0 0.0 0 -20 S 0 0 0 kworker/0:2H 24365 1 0:00.04 0.0 0.3 20 0 S 238724 1380 0 gvfsd-metadata 23944 23598 6:40.25 0.0 7.7 20 0 S 1239128 38724 0 firefox 3 PID %MEM VIRT RES CODE DATA SHR nMaj nDRT %CPU COMMAND 23598 10.7 1473588 53752 16 746528 8912 26k 0 0.0 gnome-shell 23944 7.7 1239128 38724 108 582752 8388 46k 0 0.0 firefox 18581 3.2 110512 15824 392 12292 3364 23 0 0.0 dhclient 4 PID PPID UID USER RUSER TTY TIME+ %CPU %MEM S COMMAND 536 1 172 rtkit rtkit ? 0:03.06 0.0 0.1 S rtkit-daemon 1 0 0 root root ? 0:27.20 0.0 0.8 S systemd 2 0 0 root root ? 0:02.22 0.0 0.0 S kthreadd 3 2 0 root root ? 7:11.70 0.0 0.0 S ksoftirqd/0
Fields Management for window 1:Def, whose current sort field is PPID Navigate with Up/Dn, Right selects for move then <Enter> or Left commits, 'd' or <Space> toggles display, 's' sets sort. Use 'q' or <Esc> to end! * PID = Process Id GROUP = Group Name TGID = Thread Group Id * USER = Effective User Name PGRP = Process Group Id ENVIRON = Environment vars* PR = Priority TTY = Controlling Tty vMj = Major Faults delta * NI = Nice Value TPGID = Tty Process Grp Id vMn = Minor Faults delta * VIRT = Virtual Image (KiB) SID = Session Id USED = Res+Swap Size (KiB) * RES = Resident Size (KiB) nTH = Number of Threads nsIPC=IPC namespace Inode * SHR = Shared Memory (KiB) P = Last Used Cpu (SMP) nsMNT = MNT namespace Inode * S = Process Status TIME = CPU Time nsNET = NET namespace Inode * %MEM = Memory Usage (RES) SWAP = Swapped Size (KiB) nsPID=PID namespace Inode * TIME+ = CPU Time, hundredths CODE = Code Size (KiB) nsUSER=USER namespace Inode * COMMAND = Command Name/Line DATA = Data+Stack (KiB) nsUTS= UTS namespace Inode * %CPU = CPU Usage nMaj = Major Page Faults PPID = Parent Process pid nMin = Minor Page Faults UID = Effective User Id nDRT = Dirty Pages Count RUID = Real User Id WCHAN = Sleeping in Function RUSER = Real User Name Flags = Task Flags <sched.h> SUID = Saved User Id CGROUPS = Control Groups SUSER = Saved User Name SUPGIDS = Supp Groups IDs GID = Group Id SUPGRPS = Supp Groups Names
PID to renice [default pid = 920]
PID to signal/kill [default pid = 19156]
[root@CentOS7 ~]# vmstat -m Cache Num Total Size Pages fuse_inode 10 10 768 10 nf_conntrack_ffff88001a2d8000 0 0 320 12 nf_conntrack_ffffffff81a25e00 180 180 320 12 kcopyd_job 0 0 3312 9 dm_uevent 0 0 2608 12 dm_rq_target_io 0 0 136 30
1 [root@CentOS7 ~]# vmstat 3 5 2 procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu----- 3 r b swpd free buff cache si so bi bo in cs us sy id wa st 4 0 0 539680 17040 8 234828 508 252 7085 420 84 176 6 4 86 4 0 5 0 0 539680 17040 8 234828 0 0 0 0 29 53 0 0 100 0 0 6 1 0 539680 17040 8 234828 0 0 0 0 31 58 0 0 100 0 0 7 0 0 539680 17008 8 234828 0 0 0 0 34 67 0 0 100 0 0 8 1 0 539680 16668 8 234828 0 0 0 0 71 151 1 2 97 0 0
[root@CentOS7 ~]# w root 12:26:14 up 2 days, 19:45, 3 users, load average: 0.05, 0.12, 0.13 USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT root :0 :0 Sat23 ?xdm? 1:08m 0.65s gdm-session-worker [pam/gdm-password] root pts/0 192.168.56.1 Sun22 4:35m 0.61s 0.00s lsof root pts/1 192.168.56.1 11:09 6.00s 0.20s 0.01s w root
[root@CentOS7 ~]# uptime 12:30:39 up 2 days, 19:49, 3 users, load average: 0.06, 0.08, 0.12
[root@CentOS7 ~]# ps -l F S UID PID PPID C PRI NI ADDR SZ WCHAN TTY TIME CMD 4 S 0 19160 19156 0 80 0 - 29172 wait pts/1 00:00:00 bash 0 R 0 20465 19160 0 80 0 - 34343 - pts/1 00:00:00 ps
[root@CentOS7 ~]# ps aux USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND root 1 0.0 0.8 191492 4300 ? Ss May20 0:28 /usr/lib/systemd/systemd root 2 0.0 0.0 0 0 ? S May20 0:02 [kthreadd] root 3 0.1 0.0 0 0 ? S May20 7:12 [ksoftirqd/0] root 7 0.0 0.0 0 0 ? S May20 0:00 [migration/0] root 8 0.0 0.0 0 0 ? S May20 0:00 [rcu_bh] root 9 0.0 0.0 0 0 ? S May20 0:00 [rcuob/0] root 10 0.1 0.0 0 0 ? S May20 6:10 [rcu_sched] root 11 0.3 0.0 0 0 ? S May20 14:13 [rcuos/0] root 12 0.0 0.0 0 0 ? S May20 0:03 [watchdog/0] root 13 0.0 0.0 0 0 ? S< May20 0:00 [khelper] root 14 0.0 0.0 0 0 ? S May20 0:00 [kdevtmpfs] root 15 0.0 0.0 0 0 ? S< May20 0:00 [netns] root 16 0.0 0.0 0 0 ? S< May20 0:00 [perf]
[root@CentOS7 ~]# ps -lA F S UID PID PPID C PRI NI ADDR SZ WCHAN TTY TIME CMD 4 S 0 1 0 0 80 0 - 47873 ep_pol ? 00:00:29 systemd 1 S 0 2 0 0 80 0 - 0 kthrea ? 00:00:02 kthreadd 1 S 0 3 2 0 80 0 - 0 smpboo ? 00:07:13 ksoftirqd/0 1 S 0 7 2 0 -40 - - 0 smpboo ? 00:00:00 migration/0 1 S 0 8 2 0 80 0 - 0 rcu_gp ? 00:00:00 rcu_bh 1 S 0 9 2 0 80 0 - 0 rcu_no ? 00:00:00 rcuob/0 1 S 0 10 2 0 80 0 - 0 rcu_gp ? 00:06:12 rcu_sched 1 S 0 11 2 0 80 0 - 0 rcu_no ? 00:14:16 rcuos/0 5 S 0 12 2 0 -40 - - 0 smpboo ? 00:00:03 watchdog/0 1 S 0 13 2 0 60 -20 - 0 rescue ? 00:00:00 khelper
[root@CentOS7 ~]# ps -axjf PPID PID PGID SID TTY TPGID STAT UID TIME COMMAND 0 2 0 0 ? -1 S 0 0:02 [kthreadd] 2 3 0 0 ? -1 S 0 7:13 \_ [ksoftirqd/0] 2 7 0 0 ? -1 S 0 0:00 \_ [migration/0] 2 8 0 0 ? -1 S 0 0:00 \_ [rcu_bh] 2 9 0 0 ? -1 S 0 0:00 \_ [rcuob/0] 2 10 0 0 ? -1 S 0 6:12 \_ [rcu_sched] 2 11 0 0 ? -1 S 0 14:16 \_ [rcuos/0]
[root@CentOS7 ~]# ps aux | egrep '(crypto|bash)' root 30 0.0 0.0 0 0 ? S< May20 0:00 [crypto] root 597 0.0 0.1 115240 772 ? S May20 0:14 /bin/bash /usr/sbin/ksmtuned root 23362 0.0 0.6 116556 3272 pts/0 Ss 16:50 0:00 -bash root 23521 0.0 0.0 52852 44 ? Ss May21 0:01 /usr/bin/ssh-agent /bin/sh -c exec -l /bin/bash -c "env GNOME_SHELL_SESSION_MODE=classic gnome-session --session gnome-classic" root 25992 0.0 0.1 112648 956 pts/0 S+ 20:08 0:00 grep -E --color=auto (crypto|bash)
[root@CentOS7 ~]# free 1 2 3 4 5 6 1 total used free shared buffers cached 2 Mem: 24677460 23276064 1401396 0 870540 12084008 3 -/+ buffers/cache: 10321516 14355944 4 Swap: 25151484 224188 24927296
[root@CentOS7 ~]# iostat Linux 3.10.0-327.el7.x86_64 (CentOS7.2) 05/23/2016 _x86_64_ (1 CPU) avg-cpu: %user %nice %system %iowait %steal %idle 5.20 0.00 3.53 3.54 0.00 87.73 Device: tps kB_read/s kB_wrtn/s kB_read kB_wrtn sda 197.17 6120.99 371.74 1677443445 101873448
[root@CentOS7 ~]# iostat -p sda 2 1 Linux 3.10.0-327.el7.x86_64 (CentOS7.2) 05/23/2016 _x86_64_ (1 CPU) avg-cpu: %user %nice %system %iowait %steal %idle 5.19 0.00 3.52 3.54 0.00 87.75 Device: tps kB_read/s kB_wrtn/s kB_read kB_wrtn sda 196.83 6110.31 371.21 1677443609 101907192 sda1 0.01 0.02 0.01 4585 2060 sda2 43.56 438.87 217.75 120482400 59778512 sda3 152.42 5671.42 153.45 1556955824 42126620
[root@CentOS7 ~]# mpstat -P ALL Linux 3.10.0-327.el7.x86_64 (CentOS7.2) 05/23/2016 _x86_64_ (1 CPU) 10:45:33 PM CPU %usr %nice %sys %iowait %irq %soft %steal %guest %gnice %idle 10:45:33 PM all 5.19 0.00 2.70 3.54 0.00 0.81 0.00 0.00 0.00 87.76 10:45:33 PM 0 5.19 0.00 2.70 3.54 0.00 0.81 0.00 0.00 0.00 87.76
[root@CentOS7 ~]# pmap -d 23362 23362: -bash Address Kbytes Mode Offset Device Mapping 0000000000400000 884 r-x-- 0000000000000000 000:00020 bash 00000000006dc000 4 r---- 00000000000dc000 000:00020 bash 00000000006dd000 36 rw--- 00000000000dd000 000:00020 bash 00000000006e6000 24 rw--- 0000000000000000 000:00000 [ anon ] 000000000221d000 1576 rw--- 0000000000000000 000:00000 [ anon ] 00007f7d3fc3d000 103580 r---- 0000000000000000 000:00020 locale-archive 00007f7d46164000 44 r-x-- 0000000000000000 000:00020 libnss_files-2.17.so 00007f7d4616f000 2044 ----- 000000000000b000 000:00020 libnss_files-2.17.so 00007f7d4636e000 4 r---- 000000000000a000 000:00020 libnss_files-2.17.so 00007f7d4636f000 4 rw--- 000000000000b000 000:00020 libnss_files-2.17.so 00007f7d46370000 24 rw--- 0000000000000000 000:00000 [ anon ] 00007f7d46376000 1752 r-x-- 0000000000000000 000:00020 libc-2.17.so 00007f7d4652c000 2048 ----- 00000000001b6000 000:00020 libc-2.17.so 00007f7d4672c000 16 r---- 00000000001b6000 000:00020 libc-2.17.so 00007f7d46730000 8 rw--- 00000000001ba000 000:00020 libc-2.17.so 00007f7d46732000 20 rw--- 0000000000000000 000:00000 [ anon ] 00007f7d46737000 12 r-x-- 0000000000000000 000:00020 libdl-2.17.so 00007f7d4673a000 2044 ----- 0000000000003000 000:00020 libdl-2.17.so 00007f7d46939000 4 r---- 0000000000002000 000:00020 libdl-2.17.so 00007f7d4693a000 4 rw--- 0000000000003000 000:00020 libdl-2.17.so 00007f7d4693b000 148 r-x-- 0000000000000000 000:00020 libtinfo.so.5.9 00007f7d46960000 2048 ----- 0000000000025000 000:00020 libtinfo.so.5.9 00007f7d46b60000 16 r---- 0000000000025000 000:00020 libtinfo.so.5.9 00007f7d46b64000 4 rw--- 0000000000029000 000:00020 libtinfo.so.5.9 00007f7d46b65000 132 r-x-- 0000000000000000 000:00020 ld-2.17.so 00007f7d46d6d000 12 rw--- 0000000000000000 000:00000 [ anon ] 00007f7d46d7c000 4 rw--- 0000000000000000 000:00000 [ anon ] 00007f7d46d7d000 28 r--s- 0000000000000000 000:00020 gconv-modules.cache 00007f7d46d84000 8 rw--- 0000000000000000 000:00000 [ anon ] 00007f7d46d86000 4 r---- 0000000000021000 000:00020 ld-2.17.so 00007f7d46d87000 4 rw--- 0000000000022000 000:00020 ld-2.17.so 00007f7d46d88000 4 rw--- 0000000000000000 000:00000 [ anon ] 00007fffc4354000 132 rw--- 0000000000000000 000:00000 [ stack ] 00007fffc43e0000 8 r-x-- 0000000000000000 000:00000 [ anon ] ffffffffff600000 4 r-x-- 0000000000000000 000:00000 [ anon ] mapped: 116688K writeable/private: 1864K shared: 28K
[root@CentOS7 ~]# netstat Active Internet connections (w/o servers) Proto Recv-Q Send-Q Local Address Foreign Address State tcp 0 0 10.0.2.15:33164 ec2-54-191-11-118:https ESTABLISHED tcp 0 0 10.0.2.15:52665 123.59.42.81:https ESTABLISHED tcp 0 52 CentOS7.2:ssh 192.168.56.1:61340 ESTABLISHED tcp 0 0 10.0.2.15:53607 123.59.42.81:https ESTABLISHED tcp 0 0 10.0.2.15:52666 123.59.42.81:https ESTABLISHED tcp 0 0 10.0.2.15:52321 123.59.42.81:https ESTABLISHED tcp 0 0 10.0.2.15:40108 123.59.138.92:https ESTABLISHED tcp 0 0 10.0.2.15:55494 123.59.42.81:https ESTABLISHED tcp 0 0 10.0.2.15:55490 123.59.42.81:https ESTABLISHED tcp 0 0 10.0.2.15:53608 123.59.42.81:https ESTABLISHED tcp 0 0 10.0.2.15:55495 123.59.42.81:https ESTABLISHED tcp 0 0 10.0.2.15:54883 123.59.42.81:https ESTABLISHED Active UNIX domain sockets (w/o servers) Proto RefCnt Flags Type State I-Node Path unix 2 [ ] DGRAM 6846 /run/systemd/notify unix 5 [ ] DGRAM 6858 /run/systemd/journal/socket unix 22 [ ] DGRAM 6860 /dev/log unix 2 [ ] DGRAM 9469 /run/systemd/shutdownd unix 2 [ ] DGRAM 14069 unix 2 [ ] DGRAM 1048183 unix 3 [ ] STREAM CONNECTED 1046317
[root@CentOS7 ~]# netstat -at 列出全部 tcp 端口 Active Internet connections (servers and established) Proto Recv-Q Send-Q Local Address Foreign Address State tcp 0 0 192.168.122.1:domain 0.0.0.0:* LISTEN tcp 0 0 0.0.0.0:ssh 0.0.0.0:* LISTEN tcp 0 0 localhost:ipp 0.0.0.0:* LISTEN tcp 0 0 localhos:x11-ssh-offset 0.0.0.0:* LISTEN tcp 0 0 10.0.2.15:33164 ec2-54-191-11-118:https ESTABLISHED tcp 0 0 10.0.2.15:52665 123.59.42.81:https ESTABLISHED tcp 0 52 CentOS7.2:ssh 192.168.56.1:61340 ESTABLISHED tcp 0 0 10.0.2.15:53607 123.59.42.81:https ESTABLISHED tcp 0 0 10.0.2.15:52666 123.59.42.81:https ESTABLISHED tcp 0 0 10.0.2.15:37671 123.59.87.202:https ESTABLISHED tcp 0 0 10.0.2.15:52321 123.59.42.81:https ESTABLISHED tcp 0 0 10.0.2.15:55494 123.59.42.81:https ESTABLISHED tcp 0 0 10.0.2.15:55490 123.59.42.81:https ESTABLISHED tcp 0 0 10.0.2.15:53608 123.59.42.81:https ESTABLISHED tcp 0 0 10.0.2.15:55495 123.59.42.81:https ESTABLISHED tcp 0 0 10.0.2.15:54883 123.59.42.81:https ESTABLISHED tcp6 0 0 [::]:ssh [::]:* LISTEN tcp6 0 0 localhost:ipp [::]:* LISTEN tcp6 0 0 localhos:x11-ssh-offset [::]:* LISTEN
[root@CentOS7 ~]# netstat -au 列出全部 udp 端口 Active Internet connections (servers and established) Proto Recv-Q Send-Q Local Address Foreign Address State udp 0 0 0.0.0.0:mdns 0.0.0.0:* udp 0 0 0.0.0.0:53491 0.0.0.0:* udp 0 0 0.0.0.0:47643 0.0.0.0:* udp 0 0 192.168.122.1:domain 0.0.0.0:* udp 0 0 0.0.0.0:bootps 0.0.0.0:* udp 0 0 localhost:323 0.0.0.0:* udp 0 0 0.0.0.0:bootpc 0.0.0.0:* udp 0 0 0.0.0.0:bootpc 0.0.0.0:* udp 0 0 0.0.0.0:14662 0.0.0.0:* udp6 0 0 [::]:49441 [::]:* udp6 0 0 localhost:323 [::]:* udp6 0 0 [::]:61519 [::]:*
[root@CentOS7 ~]# netstat -lt 只列出全部監聽 tcp 端口 Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State tcp 0 0 192.168.122.1:domain 0.0.0.0:* LISTEN tcp 0 0 0.0.0.0:ssh 0.0.0.0:* LISTEN tcp 0 0 localhost:ipp 0.0.0.0:* LISTEN tcp 0 0 localhos:x11-ssh-offset 0.0.0.0:* LISTEN tcp6 0 0 [::]:ssh [::]:* LISTEN tcp6 0 0 localhost:ipp [::]:* LISTEN tcp6 0 0 localhos:x11-ssh-offset [::]:* LISTEN
[root@CentOS7 ~]# netstat -lu 只列出全部監聽 udp 端口 Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State udp 0 0 0.0.0.0:mdns 0.0.0.0:* udp 0 0 0.0.0.0:53491 0.0.0.0:* udp 0 0 0.0.0.0:47643 0.0.0.0:* udp 0 0 192.168.122.1:domain 0.0.0.0:* udp 0 0 0.0.0.0:bootps 0.0.0.0:* udp 0 0 localhost:323 0.0.0.0:* udp 0 0 0.0.0.0:bootpc 0.0.0.0:* udp 0 0 0.0.0.0:bootpc 0.0.0.0:* udp 0 0 0.0.0.0:14662 0.0.0.0:* udp6 0 0 [::]:49441 [::]:* udp6 0 0 localhost:323 [::]:* udp6 0 0 [::]:61519 [::]:*
[root@CentOS7 ~]# netstat -lu 只列出全部監聽 udp 端口 Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State udp 0 0 0.0.0.0:mdns 0.0.0.0:* udp 0 0 0.0.0.0:53491 0.0.0.0:* udp 0 0 0.0.0.0:47643 0.0.0.0:* udp 0 0 192.168.122.1:domain 0.0.0.0:* udp 0 0 0.0.0.0:bootps 0.0.0.0:* udp 0 0 localhost:323 0.0.0.0:* udp 0 0 0.0.0.0:bootpc 0.0.0.0:* udp 0 0 0.0.0.0:bootpc 0.0.0.0:* udp 0 0 0.0.0.0:14662 0.0.0.0:* udp6 0 0 [::]:49441 [::]:* udp6 0 0 localhost:323 [::]:* udp6 0 0 [::]:61519 [::]:*
[root@CentOS7 ~]# netstat -lx 只列出全部監聽 UNIX 端口 Active UNIX domain sockets (only servers) Proto RefCnt Flags Type State I-Node Path unix 2 [ ACC ] STREAM LISTENING 1046533 /run/user/0/pulse/native unix 2 [ ACC ] STREAM LISTENING 9738 /run/lvm/lvmpolld.socket unix 2 [ ACC ] STREAM LISTENING 1046289 /run/user/0/keyring/pkcs11 unix 2 [ ACC ] STREAM LISTENING 1046292 /run/user/0/keyring/ssh unix 2 [ ACC ] STREAM LISTENING 1044502 /run/user/0/keyring/control unix 2 [ ACC ] STREAM LISTENING 19100 @/tmp/.X11-unix/X0
顯示 TCP 或 UDP 端口的統計信息 netstat -st 或 -suios
[root@CentOS7 ~]# netstat -st IcmpMsg: InType3: 42 InType11: 24 OutType3: 48 Tcp: 71276 active connections openings 26415 passive connection openings 443 failed connection attempts 22567 connection resets received 13 connections established 6169222 segments received 6167435 segments send out 1531 segments retransmited 0 bad segments received. 101775 resets sent UdpLite: TcpExt: 4160 TCP sockets finished time wait in fast timer 159348 delayed acks sent 243 delayed acks further delayed because of locked socket Quick ack mode was activated 64 times 119 packets directly queued to recvmsg prequeue. 113274 bytes directly in process context from backlog 346692 bytes directly received in process context from prequeue 642042 packet headers predicted 151 packets header predicted and directly queued to user 2108179 acknowledgments not containing data payload received 833025 predicted acknowledgments 49 congestion windows recovered without slow start after partial ack 6 retransmits in slow start 172 other TCP timeouts TCPLossProbes: 855 TCPLossProbeRecovery: 831 48 DSACKs sent for old packets 863 DSACKs received 20455 connections reset due to unexpected data 44 connections reset due to early user close 21 connections aborted due to timeout TCPDSACKIgnoredNoUndo: 833 TCPSpuriousRTOs: 4 IPReversePathFilter: 314 TCPRetransFail: 10 TCPRcvCoalesce: 30786 TCPChallengeACK: 7 TCPSpuriousRtxHostQueues: 29 TCPAutoCorking: 1236 TCPSynRetrans: 616 TCPOrigDataSent: 1370187 TCPHystartTrainDetect: 4 TCPHystartTrainCwnd: 82 IpExt: InNoRoutes: 32 InMcastPkts: 1724 OutMcastPkts: 738 InBcastPkts: 43901 InOctets: 1655483718 OutOctets: 1440836672 InMcastOctets: 374261 OutMcastOctets: 119692 InBcastOctets: 3810738 InNoECTPkts: 6421160
[root@CentOS7 ~]# netstat -su IcmpMsg: InType3: 42 InType11: 24 OutType3: 48 Udp: 115000 packets received 48 packets to unknown port received. 0 packet receive errors 114624 packets sent 0 receive buffer errors 0 send buffer errors UdpLite: IpExt: InNoRoutes: 32 InMcastPkts: 1724 OutMcastPkts: 738 InBcastPkts: 43901 InOctets: 1655485325 OutOctets: 1440841862 InMcastOctets: 374261 OutMcastOctets: 119692 InBcastOctets: 3810738 InNoECTPkts: 6421188
[root@CentOS7 ~]# netstat -pt Active Internet connections (w/o servers) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 10.0.2.15:33164 ec2-54-191-11-118:https ESTABLISHED 23944/firefox tcp 0 0 10.0.2.15:52665 123.59.42.81:https ESTABLISHED 24872/daotunnel tcp 0 388 CentOS7.2:ssh 192.168.56.1:61340 ESTABLISHED 23358/sshd: root@pt tcp 0 0 10.0.2.15:53607 123.59.42.81:https ESTABLISHED 24872/daotunnel tcp 0 0 10.0.2.15:52666 123.59.42.81:https ESTABLISHED 24872/daotunnel tcp 0 0 10.0.2.15:52321 123.59.42.81:https ESTABLISHED 24872/daotunnel tcp 0 0 10.0.2.15:40833 123.59.87.200:https ESTABLISHED 920/daomonit
[root@CentOS7 ~]# netstat -c Active Internet connections (w/o servers) Proto Recv-Q Send-Q Local Address Foreign Address State tcp 0 0 10.0.2.15:33164 ec2-54-191-11-118:https ESTABLISHED tcp 0 0 10.0.2.15:52665 123.59.42.81:https ESTABLISHED tcp 0 52 CentOS7.2:ssh 192.168.56.1:61340 ESTABLISHED
[root@CentOS7 ~]# netstat --verbose netstat: no support for `AF IPX' on this system. netstat: no support for `AF AX25' on this system. netstat: no support for `AF X25' on this system. netstat: no support for `AF NETROM' on this system.
[root@CentOS7 ~]# netstat -r Kernel IP routing table Destination Gateway Genmask Flags MSS Window irtt Iface default 10.0.2.2 0.0.0.0 UG 0 0 0 eth0 10.0.2.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0 172.17.0.0 0.0.0.0 255.255.0.0 U 0 0 0 docker0 192.168.56.0 0.0.0.0 255.255.255.0 U 0 0 0 eth1 192.168.122.0 0.0.0.0 255.255.255.0 U 0 0 0 virbr0
[root@CentOS7 ~]# netstat -ap | grep ssh tcp 0 0 0.0.0.0:ssh 0.0.0.0:* LISTEN 925/sshd tcp 0 0 localhos:x11-ssh-offset 0.0.0.0:* LISTEN 23358/sshd: root@pt tcp 0 52 CentOS7.2:ssh 192.168.56.1:61340 ESTABLISHED 23358/sshd: root@pt
[root@CentOS7 ~]# netstat -i Kernel Interface table Iface MTU RX-OK RX-ERR RX-DRP RX-OVR TX-OK TX-ERR TX-DRP TX-OVR Flg docker0 1500 186339 0 0 0 186275 0 0 0 BMU eth0 1500 866623 0 0 0 747980 0 0 0 BMRU eth1 1500 101288 0 0 0 83187 0 0 0 BMRU lo 65536 5276771 0 0 0 5276771 0 0 0 LRU virbr0 1500 0 0 0 0 0 0 0 0 BMU
[root@CentOS7 ~]# ss -t -a State Recv-Q Send-Q Local Address:Port Peer Address:Port LISTEN 0 5 192.168.122.1:domain *:* LISTEN 0 128 *:ssh *:* LISTEN 0 128 127.0.0.1:ipp *:* LISTEN 0 128 127.0.0.1:x11-ssh-offset *:* ESTAB 0 0 10.0.2.15:33164 54.191.11.118:https
[root@CentOS7 ~]# ss -s Total: 555 (kernel 567) TCP: 21 (estab 13, closed 1, orphaned 0, synrecv 0, timewait 0/0), ports 0 Transport Total IP IPv6 * 567 - - RAW 2 0 2 UDP 14 11 3 TCP 20 17 3 INET 36 28 8 FRAG 0 0 0
[root@CentOS7 ~]# ss -l Netid State Recv-Q Send-Q Local Address:Port Peer Address:Port nl UNCONN 0 0 rtnl:1216349100 * nl UNCONN 0 0 rtnl:2059403794 * nl UNCONN 0 0 rtnl:geoclue/23600 *
[root@CentOS7 ~]# ss -pl Netid State Recv-Q Send-Q Local Address:Port Peer Address:Port nl UNCONN 0 0 rtnl:1216349100 * nl UNCONN 0 0 rtnl:2059403794 *
[root@CentOS7 ~]# ss -u -a State Recv-Q Send-Q Local Address:Port Peer Address:Port UNCONN 0 0 *:mdns *:* UNCONN 0 0 *:53491 *:* ESTAB 0 0 10.0.2.15:42245 120.25.108.11:ntp UNCONN 0 0 *:47643 *:* UNCONN 0 0 192.168.122.1:domain *:* UNCONN 0 0 *%virbr0:bootps *:* UNCONN 0 0 127.0.0.1:323 *:* UNCONN 0 0 *:bootpc *:* UNCONN 0 0 *:bootpc *:* UNCONN 0 0 *:14662 *:* ESTAB 0 0 10.0.2.15:39026 202.118.1.81:ntp
[root@CentOS7 ~]# tcpdump -i eth0 tcpdump: verbose output suppressed, use -v or -vv for full protocol decode listening on eth0, link-type EN10MB (Ethernet), capture size 65535 bytes 02:58:12.453576 IP 10.0.2.15.41384 > 123.59.138.93.https: Flags [F.], seq 3046243613, ack 2957421906, win 42600, length 0 02:58:12.453863 IP 123.59.138.93.https > 10.0.2.15.41384: Flags [.], ack 1, win 65535, length 0 02:58:12.454352 IP 10.0.2.15.43009 > 112.54.207.8.domain: 15449+ PTR? 93.138.59.123.in-addr.arpa. (44) 02:58:12.484893 IP 123.59.138.93.https > 10.0.2.15.41384: Flags [P.], seq 1:54, ack 1, win 65535, length 53 02:58:12.484932 IP 10.0.2.15.41384 > 123.59.138.93.https: Flags [R], seq 3046243614, win 0, length 0 02:58:12.485000 IP 123.59.138.93.https > 10.0.2.15.41384: Flags [F.], seq 54, ack 1, win 65535, length 0 35 packets captured 162 packets received by filter 97 packets dropped by kernel