轉自:https://linux.cn/article-2434-1.html
netstat 的10個基本用法
Netstat 簡介
Netstat 是一款命令行工具,可用於列出系統上所有的網絡套接字連接情況,包括 tcp, udp 以及 unix 套接字,另外它還能列出處於監聽狀態(即等待接入請求)的套接字。如果你想確認系統上的 Web 服務有沒有起來,你可以查看80端口有沒有打開。以上功能使 netstat 成為網管和系統管理員的必備利器。在這篇教程中,我會列出幾個例子,教大家如何使用 netstat 去查找網絡連接信息和系統開啟的端口號。
以下的簡單介紹來自 netstat 的 man 手冊:
netstat - 打印網絡連接、路由表、連接的數據統計、偽裝連接以及廣播域成員。
1. 列出所有連接
第一個要介紹的,是最簡單的命令:列出所有當前的連接。使用 -a 選項即可。
- $ netstat -a
- Active Internet connections (servers and established)
- Proto Recv-Q Send-Q Local Address Foreign Address State
- tcp 0 0 enlightened:domain *:* LISTEN
- tcp 0 0 localhost:ipp *:* LISTEN
- tcp 0 0 enlightened.local:54750 li240-5.members.li:http ESTABLISHED
- tcp 0 0 enlightened.local:49980 del01s07-in-f14.1:https ESTABLISHED
- tcp6 0 0 ip6-localhost:ipp [::]:* LISTEN
- udp 0 0 enlightened:domain *:*
- udp 0 0 *:bootpc *:*
- udp 0 0 enlightened.local:ntp *:*
- udp 0 0 localhost:ntp *:*
- udp 0 0 *:ntp *:*
- udp 0 0 *:58570 *:*
- udp 0 0 *:mdns *:*
- udp 0 0 *:49459 *:*
- udp6 0 0 fe80::216:36ff:fef8:ntp [::]:*
- udp6 0 0 ip6-localhost:ntp [::]:*
- udp6 0 0 [::]:ntp [::]:*
- udp6 0 0 [::]:mdns [::]:*
- udp6 0 0 [::]:63811 [::]:*
- udp6 0 0 [::]:54952 [::]:*
- Active UNIX domain sockets (servers and established)
- Proto RefCnt Flags Type State I-Node Path
- unix 2 [ ACC ] STREAM LISTENING 12403 @/tmp/dbus-IDgfj3UGXX
- unix 2 [ ACC ] STREAM LISTENING 40202 @/dbus-vfs-daemon/socket-6nUC6CCx
上述命令列出 tcp, udp 和 unix 協議下所有套接字的所有連接。然而這些信息還不夠詳細,管理員往往需要查看某個協議或端口的具體連接情況。
2. 只列出 TCP 或 UDP 協議的連接
使用 -t 選項列出 TCP 協議的連接:
- $ netstat -at
- Active Internet connections (servers and established)
- Proto Recv-Q Send-Q Local Address Foreign Address State
- tcp 0 0 enlightened:domain *:* LISTEN
- tcp 0 0 localhost:ipp *:* LISTEN
- tcp 0 0 enlightened.local:36310 del01s07-in-f24.1:https ESTABLISHED
- tcp 0 0 enlightened.local:45038 a96-17-181-10.depl:http ESTABLISHED
- tcp 0 0 enlightened.local:37892 ABTS-North-Static-:http ESTABLISHED
- .....
使用 -u 選項列出 UDP 協議的連接:
- $ netstat -au
- Active Internet connections (servers and established)
- Proto Recv-Q Send-Q Local Address Foreign Address State
- udp 0 0 *:34660 *:*
- udp 0 0 enlightened:domain *:*
- udp 0 0 *:bootpc *:*
- udp 0 0 enlightened.local:ntp *:*
- udp 0 0 localhost:ntp *:*
- udp 0 0 *:ntp *:*
- udp6 0 0 fe80::216:36ff:fef8:ntp [::]:*
- udp6 0 0 ip6-localhost:ntp [::]:*
- udp6 0 0 [::]:ntp [::]:*
上面同時顯示了 IPv4 和 IPv6 的連接。
3. 禁用反向域名解析,加快查詢速度
默認情況下 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 173.255.230.5:80 ESTABLISHED
- tcp 0 0 192.168.1.2:33324 173.194.36.117:443 ESTABLISHED
- tcp6 0 0 ::1:631 :::* LISTEN
上述命令列出所有 TCP 協議的連接,沒有使用域名解析技術。So easy ? 非常好。
4. 只列出監聽中的連接
任何網絡服務的后台進程都會打開一個端口,用於監聽接入的請求。這些正在監聽的套接字也和連接的套接字一樣,也能被 netstat 列出來。使用 -l 選項列出正在監聽的套接字。
- $ netstat -tnl
- Active Internet connections (only servers)
- 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
- tcp6 0 0 ::1:631 :::* LISTEN
現在我們可以看到處於監聽狀態的 TCP 端口和連接。如果你查看所有監聽端口,去掉 -t 選項。如果你只想查看 UDP 端口,使用 -u 選項,代替 -t 選項。
注意:不要使用 -a 選項,否則 netstat 會列出所有連接,而不僅僅是監聽端口。
5. 獲取進程名、進程號以及用戶 ID
查看端口和連接的信息時,能查看到它們對應的進程名和進程號對系統管理員來說是非常有幫助的。舉個栗子,Apache 的 httpd 服務開啟80端口,如果你要查看 http 服務是否已經啟動,或者 http 服務是由 apache 還是 nginx 啟動的,這時候你可以看看進程名。
使用 -p 選項查看進程信息。
- ~$ sudo netstat -nlpt
- Active Internet connections (only servers)
- Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
- tcp 0 0 127.0.1.1:53 0.0.0.0:* LISTEN 1144/dnsmasq
- tcp 0 0 127.0.0.1:631 0.0.0.0:* LISTEN 661/cupsd
- tcp6 0 0 ::1:631 :::* LISTEN 661/cupsd
使用 -p 選項時,netstat 必須運行在 root 權限之下,不然它就不能得到運行在 root 權限下的進程名,而很多服務包括 http 和 ftp 都運行在 root 權限之下。
相比進程名和進程號而言,查看進程的擁有者會更有用。使用 -ep 選項可以同時查看進程名和用戶名。
- $ sudo 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 enlightened:domain *:* LISTEN root 11090 1144/dnsmasq
- tcp 0 0 localhost:ipp *:* LISTEN root 9755 661/cupsd
- tcp6 0 0 ip6-localhost:ipp [::]:* LISTEN root 9754 661/cupsd
上面列出 TCP 協議下的監聽套接字,同時顯示進程信息和一些額外信息。
這些額外的信息包括用戶名和進程的索引節點號。這個命令對網管來說很有用。
注意 - 假如你將 -n 和 -e 選項一起使用,User 列的屬性就是用戶的 ID 號,而不是用戶名。
6. 打印統計數據
netstat 可以打印出網絡統計數據,包括某個協議下的收發包數量。
下面列出所有網絡包的統計情況:
- $ netstat -s
- Ip:
- 32797 total packets received
- 0 forwarded
- 0 incoming packets discarded
- 32795 incoming packets delivered
- 29115 requests sent out
- 60 outgoing packets dropped
- Icmp:
- 125 ICMP messages received
- 0 input ICMP message failed.
- ICMP input histogram:
- destination unreachable: 125
- 125 ICMP messages sent
- 0 ICMP messages failed
- ICMP output histogram:
- destination unreachable: 125
- ... OUTPUT TRUNCATED ...
如果想只打印出 TCP 或 UDP 協議的統計數據,只要加上對應的選項(-t 和 -u)即可,so easy。
7. 顯示內核路由信息
使用 -r 選項打印內核路由信息。打印出來的信息與 route 命令輸出的信息一樣。我們也可以使用 -n 選項禁止域名解析。
- $ netstat -rn
- Kernel IP routing table
- Destination Gateway Genmask Flags MSS Window irtt Iface
- 0.0.0.0 192.168.1.1 0.0.0.0 UG 0 0 0 eth0
- 192.168.1.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0
8. 打印網絡接口
netstat 也能打印網絡接口信息,-i 選項就是為這個功能而生。
- $ 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 31611 0 0 0 27503 0 0 0 BMRU
- lo 65536 0 2913 0 0 0 2913 0 0 0 LRU
上面輸出的信息比較原始。我們將 -e 選項和 -i 選項搭配使用,可以輸出用戶友好的信息。
- $ netstat -ie
- Kernel Interface table
- eth0 Link encap:Ethernet HWaddr 00:16:36:f8:b2:64
- inet addr:192.168.1.2 Bcast:192.168.1.255 Mask:255.255.255.0
- inet6 addr: fe80::216:36ff:fef8:b264/64 Scope:Link
- UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
- RX packets:31682 errors:0 dropped:0 overruns:0 frame:0
- TX packets:27573 errors:0 dropped:0 overruns:0 carrier:0
- collisions:0 txqueuelen:1000
- RX bytes:29637117 (29.6 MB) TX bytes:4590583 (4.5 MB)
- Interrupt:18 Memory:da000000-da020000
- lo Link encap:Local Loopback
- inet addr:127.0.0.1 Mask:255.0.0.0
- inet6 addr: ::1/128 Scope:Host
- UP LOOPBACK RUNNING MTU:65536 Metric:1
- RX packets:2921 errors:0 dropped:0 overruns:0 frame:0
- TX packets:2921 errors:0 dropped:0 overruns:0 carrier:0
- collisions:0 txqueuelen:0
- RX bytes:305297 (305.2 KB) TX bytes:305297 (305.2 KB)
上面的輸出信息與 ifconfig 輸出的信息一樣。
9. netstat 持續輸出
我們可以使用 netstat 的 -c 選項持續輸出信息。
- $ netstat -ct
這個命令可持續輸出 TCP 協議信息。
10. 顯示多播組信息
選項 -g 會輸出 IPv4 和 IPv6 的多播組信息。
- $ netstat -g
- IPv6/IPv4 Group Memberships
- Interface RefCnt Group
- --------------- ------ ---------------------
- lo 1 all-systems.mcast.net
- eth0 1 224.0.0.251
- eth0 1 all-systems.mcast.net
- lo 1 ip6-allnodes
- lo 1 ff01::1
- eth0 1 ff02::fb
- eth0 1 ff02::1:fff8:b264
- eth0 1 ip6-allnodes
- eth0 1 ff01::1
- wlan0 1 ip6-allnodes
- wlan0 1 ff01::1
更多用法
目前為止我們列出了 netstat 的基本用法,現在讓我們一起來 geek 吧~
打印 active 狀態的連接
active 狀態的套接字連接用 "ESTABLISHED" 字段表示,所以我們可以使用 grep 命令獲得 active 狀態的連接:
- $ netstat -atnp | grep ESTA
- (Not all processes could be identified, non-owned process info
- will not be shown, you would have to be root to see it all.)
- tcp 0 0 192.168.1.2:49156 173.255.230.5:80 ESTABLISHED 1691/chrome
- tcp 0 0 192.168.1.2:33324 173.194.36.117:443 ESTABLISHED 1691/chrome
配合 watch 命令監視 active 狀態的連接:
- $ watch -d -n0 "netstat -atnp | grep ESTA"
查看服務是否在運行
如果你想看看 http,smtp 或 ntp 服務是否在運行,使用 grep。
- $ sudo netstat -aple | grep ntp
- udp 0 0 enlightened.local:ntp *:* root 17430 1789/ntpd
- udp 0 0 localhost:ntp *:* root 17429 1789/ntpd
- udp 0 0 *:ntp *:* root 17422 1789/ntpd
- udp6 0 0 fe80::216:36ff:fef8:ntp [::]:* root 17432 1789/ntpd
- udp6 0 0 ip6-localhost:ntp [::]:* root 17431 1789/ntpd
- udp6 0 0 [::]:ntp [::]:* root 17423 1789/ntpd
- unix 2 [ ] DGRAM 17418 1789/ntpd
從這里可以看到 ntp 服務正在運行。使用 grep 命令你可以查看 http 或 smtp 或其它任何你想查看的服務。
好了,netstat 的大部分功能都介紹過了,如果你想知道 netstat 更高級的功能,閱讀它的手冊吧(man netstat)。
歡迎在下面留下你的反饋和建議。