Netstat 是一款命令行工具,可用於列出系統上所有的網絡套接字連接情況,包括 tcp, udp 以及 unix 套接字,另外它還能列出處於監聽狀態(即等待接入請求)的套接字。如果你想確認系統上的 Web 服務有沒有起來,你可以查看80端口有沒有打開。以上功能使 netstat 成為網管和系統管理員的必備利器。在這篇教程中,我會列出幾個例子,教大家如何使用 netstat 去查找網絡連接信息和系統開啟的端口號。
以下的簡單介紹來自 netstat 的 man 手冊:
netstat - 打印網絡連接、路由表、連接的數據統計、偽裝連接以及廣播域成員。
第一個要介紹的,是最簡單的命令:列出所有當前的連接。使用 -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 協議下所有套接字的所有連接。然而這些信息還不夠詳細,管理員往往需要查看某個協議或端口的具體連接情況。
使用 -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 的連接。
默認情況下 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 ? 非常好。
任何網絡服務的後台進程都會打開一個端口,用於監聽接入的請求。這些正在監聽的套接字也和連接的套接字一樣,也能被 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 會列出所有連接,而不僅僅是監聽端口。
相關閱讀:
Linux netstat命令 http://www.linuxidc.com/Linux/2013-06/85528.htm
Linux netstat命令詳解 http://www.linuxidc.com/Linux/2012-12/75667.htm
Linux命令:service & netstat http://www.linuxidc.com/Linux/2011-12/48395.htm
Linux下用netstat命令查看網絡負載狀況的一條語句 http://www.linuxidc.com/Linux/2011-08/41429.htm
LPI認證考試學習之Linux netstat 命令詳解 http://www.linuxidc.com/Linux/2009-12/23197.htm