一:簡介
netstat 命令用於顯示各種網絡相關信息,如網絡連接,路由表,接口狀態 (Interface Statistics),masquerade 連接,多播成員 (Multicast Memberships) 等等。
二:netstat --help 命令
# netstat --help usage: netstat [-vWeenNcCF] [<Af>] -r netstat {-V|--version|-h|--help} netstat [-vWnNcaeol] [<Socket> ...] netstat { [-vWeenNac] -I[<Iface>] | [-veenNac] -i | [-cnNe] -M | -s [-6tuw] } [delay] -r, --route display routing table # 顯示路由 -I, --interfaces=<Iface> display interface table for <Iface> # 顯示網絡界面信息 -i, --interfaces display interface table # 顯示網絡界面信息
-g, --groups display multicast group memberships # 顯示多重廣播功能群組組員名單 -s, --statistics display networking statistics (like SNMP) # 顯示網絡統計表 -M, --masquerade display masqueraded connections -v, --verbose be verbose # 顯示指令執行過程 -W, --wide don't truncate IP addresses # 不能截斷IP地址 -n, --numeric don't resolve names # 直接使用ip地址,而不通過域名服務器 --numeric-hosts don't resolve host names --numeric-ports don't resolve port names --numeric-users don't resolve user names -N, --symbolic resolve hardware names # 顯示網絡硬件外圍設備的符號鏈接名稱 -e, --extend display other/more information # 顯示網絡的其他相關信息 -p, --programs display PID/Program name for sockets # 顯示正在使用socket的程序PID和程序名稱 -o, --timers display timers # 顯示計時器 -c, --continuous continuous listing # 持續顯示網絡狀態 -l, --listening display listening server sockets # 顯示監控中的服務器socket -a, --all display all sockets (default: connected) # 顯示所有連接中的socket -F, --fib display Forwarding Information Base (default) # 顯示FIB信息 -C, --cache display routing cache instead of FIB # 顯示路由器的緩存信息來替代FIB信息
-Z, --context display SELinux security context for sockets <Socket>={-t|--tcp} {-u|--udp} {-U|--udplite} {-w|--raw} {-x|--unix} --ax25 --ipx --netrom <AF>=Use '-6|-4' or '-A <af>' or '--<af>'; default: inet List of possible address families (which support routing): inet (DARPA Internet) inet6 (IPv6) ax25 (AMPR AX.25) netrom (AMPR NET/ROM) ipx (Novell IPX) ddp (Appletalk DDP) x25 (CCITT X.25)
三:選項(上面help命令選項)
-a或--all:顯示所有連線中的Socket; -A<網絡類型>或--<網絡類型>:列出該網絡類型連線中的相關地址; -c或--continuous:持續列出網絡狀態; -C或--cache:顯示路由器配置的快取信息; -e或--extend:顯示網絡其他相關信息; -F或--fib:顯示FIB; -g或--groups:顯示多重廣播功能群組組員名單; -h或--help:在線幫助; -i或--interfaces:顯示網絡界面信息表單; -l或--listening:顯示監控中的服務器的Socket; -M或--masquerade:顯示偽裝的網絡連線; -n或--numeric:直接使用ip地址,而不通過域名服務器; -N或--netlink或--symbolic:顯示網絡硬件外圍設備的符號連接名稱; -o或--timers:顯示計時器; -p或--programs:顯示正在使用Socket的程序識別碼和程序名稱; -r或--route:顯示Routing Table; -s或--statistice:顯示網絡工作信息統計表; -t或--tcp:顯示TCP傳輸協議的連線狀況; -u或--udp:顯示UDP傳輸協議的連線狀況; -v或--verbose:顯示指令執行過程; -V或--version:顯示版本信息; -w或--raw:顯示RAW傳輸協議的連線狀況; -x或--unix:此參數的效果和指定"-A unix"參數相同; --ip或--inet:此參數的效果和指定"-A inet"參數相同。
提示:LISTEN和LISTENING的狀態只有用-a或者-l才能看到
四:輸出信息含義
在centos命令行,執行netstat后,其輸出結果為
Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 2 210.34.6.89:telnet 210.34.6.96:2873 ESTABLISHED
tcp 296 0 210.34.6.89:1165 210.34.6.84:netbios-ssn ESTABLISHED
tcp 0 0 localhost.localdom:9001 localhost.localdom:1162 ESTABLISHED
tcp 0 0 localhost.localdom:1162 localhost.localdom:9001 ESTABLISHED
tcp 0 80 210.34.6.89:1161 210.34.6.10:netbios-ssn CLOSE
Active UNIX domain sockets (w/o servers)
Proto RefCnt Flags Type State I-Node Path
unix 1 [ ] STREAM CONNECTED 16178 @000000dd
unix 1 [ ] STREAM CONNECTED 16176 @000000dc
unix 9 [ ] DGRAM 5292 /dev/log
unix 1 [ ] STREAM CONNECTED 16182 @000000df
從整體上看,netstat的輸出結果可以分為兩個部分:
一個是Active Internet connections,稱為有源TCP連接,其中"Recv-Q"和"Send-Q"指%0A的是接收隊列和發送隊列。這些數字一般都應該是0。如果不是則表示軟件包正在隊列中堆積。這種情況只能在非常少的情況見到。
另一個是Active UNIX domain sockets,稱為有源Unix域套接口(和網絡套接字一樣,但是只能用於本機通信,性能可以提高一倍)。
Proto顯示連接使用的協議,RefCnt表示連接到本套接口上的進程號,Types顯示套接口的類型,State顯示套接口當前的狀態,Path表示連接到套接口的其它進程使用的路徑名。
五:命令實例
1. 列出所有端口 (包括監聽和未監聽的)
列出所有端口 netstat -a
# netstat -a | more
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 localhost:30037 *:* LISTEN
udp 0 0 *:bootpc *:*
Active UNIX domain sockets (servers and established)
Proto RefCnt Flags Type State I-Node Path
unix 2 [ ACC ] STREAM LISTENING 6135 /tmp/.X11-unix/X0
unix 2 [ ACC ] STREAM LISTENING 5140 /var/run/acpid.socket
列出所有 tcp 端口 netstat -at
# netstat -at
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 localhost:30037 *:* LISTEN
tcp 0 0 localhost:ipp *:* LISTEN
tcp 0 0 *:smtp *:* LISTEN
tcp6 0 0 localhost:ipp [::]:* LISTEN
列出所有 udp 端口 netstat -au
# netstat -au
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State
udp 0 0 *:bootpc *:*
udp 0 0 *:49119 *:*
udp 0 0 *:mdns *:*
禁用反向域名解析,加快查詢速度
默認情況下 netstat 會通過反向域名解析技術查找每個 IP 地址對應的主機名。這會降低查找速度。如果你覺得 IP 地址已經足夠,而沒有必要知道主機名,就使用 -n 選項禁用域名解析功能
# netstat -ant Active Internet connections (servers and established) Proto Recv-Q Send-Q Local Address Foreign Address State tcp 0 0 127.0.1.1:53 0.0.0.0:* LISTEN tcp 0 0 127.0.0.1:631 0.0.0.0:* LISTEN tcp 0 0 192.168.1.2:49058 *.*.*.5:80 ESTABLISHED tcp 0 0 192.168.1.2:33324 *.*.*.117:443 ESTABLISHED tcp6 0 0 ::1:631 :::* LISTEN
2. 列出所有處於監聽狀態的 Sockets
只顯示監聽端口 netstat -l
# netstat -l
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 localhost:ipp *:* LISTEN
tcp6 0 0 localhost:ipp [::]:* LISTEN
udp 0 0 *:49119 *:*
Active UNIX domain sockets (only servers)
Proto RefCnt Flags Type State I-Node Path
unix 2 [ ACC ] STREAM LISTENING 9472 /run/systemd/private
unix 2 [ ACC ] SEQPACKET LISTENING 9524 /run/udev/control
unix 2 [ ACC ] STREAM LISTENING 9537 /run/lvm/lvmpolld.socket
unix 2 [ ACC ] STREAM LISTENING 9540 /run/lvm/lvmetad.socket
unix 2 [ ACC ] STREAM LISTENING 6807 /run/systemd/journal/stdout
unix 2 [ ACC ] STREAM LISTENING 11162 /var/run/dbus/system_bus_socket
unix 2 [ ACC ] STREAM LISTENING 11958 /var/run/acpid.socket
unix 2 [ ACC ] STREAM LISTENING 11467 /var/run/lsm/ipc/sim
unix 2 [ ACC ] STREAM LISTENING 11469 /var/run/lsm/ipc/simc
unix 2 [ ACC ] STREAM LISTENING 31743620 /var/lib/mysql/mysql.sock
任何網絡服務的后台進程都會打開一個端口,用於監聽接入的請求。這些正在監聽的套接字也和連接的套接字一樣,也能被 netstat 列出來。使用 -l 選項列出正在監聽的套接字
現在我們可以看到處於監聽狀態的 TCP 端口和連接。如果你查看所有監聽端口,去掉 -t 選項。如果你只想查看 UDP 端口,使用 -u 選項,代替 -t 選項。
注意:不要使用 -a 選項,否則 netstat 會列出所有連接,而不僅僅是監聽端口
# netstat -tnl
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN
tcp 0 0 127.0.0.1:6379 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN
tcp6 0 0 :::3306 :::* LISTEN
tcp6 0 0 :::22 :::* LISTEN
只列出所有監聽 tcp 端口 netstat -lt
# netstat -lt
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 localhost:30037 *:* LISTEN
tcp 0 0 *:smtp *:* LISTEN
tcp6 0 0 localhost:ipp [::]:* LISTEN
只列出所有監聽 udp 端口 netstat -lu
# netstat -lu
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State
udp 0 0 *:49119 *:*
udp 0 0 *:mdns *:*
只列出所有監聽 UNIX 端口 netstat -lx
# netstat -lx
Active UNIX domain sockets (only servers)
Proto RefCnt Flags Type State I-Node Path
unix 2 [ ACC ] STREAM LISTENING 6294 private/maildrop
unix 2 [ ACC ] STREAM LISTENING 6203 public/cleanup
unix 2 [ ACC ] STREAM LISTENING 6302 private/ifmail
unix 2 [ ACC ] STREAM LISTENING 6306 private/bsmtp
3. 顯示每個協議的統計信息
顯示所有端口的統計信息 netstat -s
# netstat -s
Ip:
11150 total packets received
1 with invalid addresses
0 forwarded
0 incoming packets discarded
11149 incoming packets delivered
11635 requests sent out
Icmp:
0 ICMP messages received
0 input ICMP message failed.
Tcp:
582 active connections openings
2 failed connection attempts
25 connection resets received
Udp:
1183 packets received
4 packets to unknown port received.
.....
顯示 TCP 或 UDP 端口的統計信息 netstat -st 或 -su
# netstat -st
# netstat -su
4. 在 netstat 輸出中顯示 PID 和進程名稱 netstat -p
netstat -p 可以與其它開關一起使用,就可以添加 “PID/進程名稱” 到 netstat 輸出中,這樣 debugging 的時候可以很方便的發現特定端口運行的程序。
使用 -p 選項時,netstat 必須運行在 root 權限之下,不然它就不能得到運行在 root 權限下的進程名,而很多服務包括 http 和 ftp 都運行在 root 權限之下。
# netstat -pt
Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 1 0 ramesh-laptop.loc:47212 192.168.185.75:www CLOSE_WAIT 2109/firefox
tcp 0 0 ramesh-laptop.loc:52750 lax:www ESTABLISHED 2109/firefox
相比進程名和進程號而言,查看進程的擁有者會更有用。使用 -ep 選項可以同時查看進程名和用戶名。
# netstat -ltpe Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State User Inode PID/Program name tcp 0 0 localhost:cslistener 0.0.0.0:* LISTEN root 28774494 11886/php-fpm: mast tcp 0 0 localhost:6379 0.0.0.0:* LISTEN root 5368096 31546/redis-s tcp 0 0 0.0.0.0:http 0.0.0.0:* LISTEN root 28255272 16701/nginx: master tcp 0 0 0.0.0.0:ssh 0.0.0.0:* LISTEN root 12459 468/sshd tcp6 0 0 [::]:mysql [::]:* LISTEN mysql 31743619 9672/mysqld tcp6 0 0 [::]:ssh [::]:* LISTEN root 12468 468/sshd
5. 在 netstat 輸出中不顯示主機,端口和用戶名 (host, port or user)
當你不想讓主機,端口和用戶名顯示,使用 netstat -n。將會使用數字代替那些名稱。
同樣可以加速輸出,因為不用進行比對查詢。
# netstat -an
如果只是不想讓這三個名稱中的一個被顯示,使用以下命令
# netsat -a --numeric-ports
# netsat -a --numeric-hosts
# netsat -a --numeric-users
6. 持續輸出 netstat 信息
netstat 將每隔一秒輸出網絡信息。
# netstat -c
Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 ramesh-laptop.loc:36130 aaa.ama:www ESTABLISHED
tcp 1 1 ramesh-laptop.loc:52564 *.*.169.230:www CLOSING
tcp 0 0 ramesh-laptop.loc:43758 server-43-2:www ESTABLISHED
7. 顯示系統不支持的地址族 (Address Families)
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.
8. 顯示核心路由信息 netstat -r
# netstat -r
Kernel IP routing table
Destination Gateway Genmask Flags MSS Window irtt Iface
192.168.1.0 * 255.255.255.0 U 0 0 0 eth2
link-local * 255.255.0.0 U 0 0 0 eth2
default 192.168.1.1 0.0.0.0 UG 0 0 0 eth2
注意: 使用 netstat -rn 顯示數字格式,不查詢主機名稱。
# netstat -rn Kernel IP routing table Destination Gateway Genmask Flags MSS Window irtt Iface 0.0.0.0 172.*.0.1 0.0.0.0 UG 0 0 0 eth0 169.254.0.0 0.0.0.0 255.255.0.0 U 0 0 0 eth0 172.*.0.0 0.0.0.0 255.255.240.0 U 0 0 0 eth0
9. 找出程序運行的端口
並不是所有的進程都能找到,沒有權限的會不顯示,使用 root 權限查看所有的信息。
# netstat -ap | grep ssh
tcp 1 0 dev-db:ssh 101.174.100.22:39213 CLOSE_WAIT -
tcp 1 0 dev-db:ssh 101.174.100.22:57643 CLOSE_WAIT -
找出運行在指定端口的進程
# netstat -an | grep ':80'
打印鏈接狀態
active 狀態的套接字連接用 "ESTABLISHED" 字段表示,所以我們可以使用 grep 命令獲得 active 狀態的連接
# netstat -atnp | grep ESTA tcp 0 52 172.*.*.15:22 *.*.105.255:49938 ESTABLISHED 14370/sshd: root@pt tcp 0 0 172.*.*.15:22 *.*.105.255:47108 ESTABLISHED 11384/sshd: centos tcp 0 0 172.*.*.*:22 *.*.*.43:55607 ESTABLISHED 15964/sshd: root [p tcp 0 0 172.*.*.*:39182 *.*.0.55:5574 ESTABLISHED 1851/YDService
10. 顯示網絡接口列表
# netstat -i
Kernel Interface table
Iface MTU Met RX-OK RX-ERR RX-DRP RX-OVR TX-OK TX-ERR TX-DRP TX-OVR Flg
eth0 1500 0 0 0 0 0 0 0 0 0 BMU
eth2 1500 0 26196 0 0 0 26883 6 0 0 BMRU
lo 16436 0 4 0 0 0 4 0 0 0 LRU
顯示詳細信息, 使用 netstat -ie
此命令類似 ifconfig
# netstat -ie
Kernel Interface table
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.*.*.* netmask 255.255.240.0 broadcast *.*.*.*
ether 52:*:*:*:*:* txqueuelen 1000 (Ethernet)
RX packets 33093066 bytes 4113720313 (3.8 GiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 32928019 bytes 4414472855 (4.1 GiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
loop txqueuelen 1 (Local Loopback)
RX packets 4275400 bytes 1148810073 (1.0 GiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 4275400 bytes 1148810073 (1.0 GiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
11. IP和TCP分析
查看連接某服務端口最多的的IP地址
# netstat -nat | grep "192.168.1.101:22" |awk '{print $5}'|awk -F: '{print $1}'|sort|uniq -c|sort -nr|head -20
18 221.136.168.36
3 154.74.45.242
2 78.173.31.236
2 62.183.207.98
2 192.168.1.14
2 182.48.111.215
2 124.193.219.34
2 119.145.41.2
2 114.255.41.30
1 75.102.11.99
TCP各種狀態列表
# netstat -nat |awk '{print $6}'
established)
Foreign
LISTEN
TIME_WAIT
ESTABLISHED
TIME_WAIT
SYN_SENT
先把狀態全都取出來,然后使用uniq -c統計,之后再進行排序。
# netstat -nat |awk '{print $6}'|sort|uniq -c
143 ESTABLISHED
1 FIN_WAIT1
1 Foreign
1 LAST_ACK
36 LISTEN
6 SYN_SENT
113 TIME_WAIT
1 established)
最后的命令如下:
netstat -nat |awk '{print $6}'|sort|uniq -c|sort -rn
分析access.log獲得訪問前10位的ip地址
awk '{print $1}' access.log |sort|uniq -c|sort -nr|head -10
12.查看php進程數
netstat -anpo | grep "php-cgi" | wc -l
13. 打印網絡包的統計(netstat 可以打印出網絡統計數據,包括某個協議下的收發包數量)
# netstat -s Ip: 36811488 total packets received 0 forwarded 158 with unknown protocol 0 incoming packets discarded 36811318 incoming packets delivered 36957933 requests sent out 40 dropped because of missing route Icmp: 4137854 ICMP messages received 15481 input ICMP message failed. ICMP input histogram: destination unreachable: 15848 timeout in transit: 139 source quenches: 1 redirects: 2 echo requests: 4121824 echo replies: 15 timestamp request: 25 4148314 ICMP messages sent 0 ICMP messages failed ICMP output histogram: destination unreachable: 26465 echo replies: 4121824 timestamp replies: 25 IcmpMsg: InType0: 15 InType3: 15848 InType4: 1 InType5: 2 InType8: 4121824 InType11: 139 InType13: 25 OutType0: 4121824 OutType3: 26465 OutType14: 25 Tcp: 2078274 active connections openings 408218 passive connection openings 8831 failed connection attempts 5683 connection resets received 4 connections established 28410463 segments received 28806036 segments send out 112716 segments retransmited 2788 bad segments received. 2882644 resets sent InCsumErrors: 2701 Udp: 4156204 packets received 27872 packets to unknown port received. 61 packet receive errors 4161787 packets sent 0 receive buffer errors 0 send buffer errors InCsumErrors: 61 UdpLite: TcpExt: 227685 invalid SYN cookies received 8396 resets received for embryonic SYN_RECV sockets 67 packets pruned from receive queue because of socket buffer overrun 35788 TCP sockets finished time wait in fast timer 109 packets rejects in established connections because of timestamp 935149 delayed acks sent 25 delayed acks further delayed because of locked socket Quick ack mode was activated 8903 times
... ... ... ... ...
14.配合 watch 命令監控 active 狀態鏈接
watch -d -n0 "netstat -atnp | grep ESTA"
它是一個動態變化的圖
15.查看服務是否運行
如果你想看看 http,smtp 或 ntp 服務是否在運行,使用 grep。
查看http是否運行
# netstat -aple | grep http tcp 0 0 0.0.0.0:http 0.0.0.0:* LISTEN root 28255272 16701/nginx: master
使用 grep 命令你可以查看 http 或 smtp 或其它任何你想查看的服務
http://man.linuxde.net/netstat
https://linux.cn/article-2434-1.html
http://www.cnblogs.com/ggjucheng/archive/2012/01/08/2316661.html