天天看點

netstat 基本用法

Netstat 是一款指令行工具,可用于列出系統上所有的網絡套接字連接配接情況,包括 tcp, udp 以及 unix 套接字,另外它還能列出處于監聽狀态(即等待接入請求)的套接字。如果你想确認系統上的 Web 服務有沒有起來,你可以檢視80端口有沒有打開。以上功能使 netstat 成為網管和系統管理者的必備利器。在這篇教程中,我會列出幾個例子,教大家如何使用 netstat 去查找網絡連接配接資訊和系統開啟的端口号。

以下的簡單介紹來自 netstat 的 man 手冊:

netstat - 列印網絡連接配接、路由表、連接配接的資料統計、僞裝連接配接以及廣播域成員。

1. 列出所有連接配接

第一個要介紹的,是最簡單的指令:列出所有目前的連接配接。使用 -a 選項即可。

netstat 基本用法
$ 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      
netstat 基本用法

上述指令列出 tcp, udp 和 unix 協定下所有套接字的所有連接配接。然而這些資訊還不夠詳細,管理者往往需要檢視某個協定或端口的具體連接配接情況。

2. 隻列出 TCP 或 UDP 協定的連接配接

使用 -t 選項列出 TCP 協定的連接配接:

netstat 基本用法
$ 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
.....      
netstat 基本用法

使用 -u 選項列出 UDP 協定的連接配接:

netstat 基本用法
$ 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 [::]:*      
netstat 基本用法

上面同時顯示了 IPv4 和 IPv6 的連接配接。

3. 禁用反向域名解析,加快查詢速度

預設情況下 netstat 會通過反向域名解析技術查找每個 IP 位址對應的主機名。這會降低查找速度。如果你覺得 IP 位址已經足夠,而沒有必要知道主機名,就使用 -n 選項禁用域名解析功能。

netstat 基本用法
$ 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      
netstat 基本用法

上述指令列出所有 TCP 協定的連接配接,沒有使用域名解析技術。So easy ? 非常好。

4. 隻列出監聽中的連接配接

任何網絡服務的背景程序都會打開一個端口,用于監聽接入的請求。這些正在監聽的套接字也和連接配接的套接字一樣,也能被 netstat 列出來。使用 -l 選項列出正在監聽的套接字。

netstat 基本用法
$ 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      
netstat 基本用法

現在我們可以看到處于監聽狀态的 TCP 端口和連接配接。如果你檢視所有監聽端口,去掉 -t 選項。如果你隻想檢視 UDP 端口,使用 -u 選項,代替 -t 選項。

注意:不要使用 -a 選項,否則 netstat 會列出所有連接配接,而不僅僅是監聽端口。

5. 擷取程序名、程序号以及使用者 ID

檢視端口和連接配接的資訊時,能檢視到它們對應的程序名和程序号對系統管理者來說是非常有幫助的。舉個栗子,Apache 的 httpd 服務開啟80端口,如果你要檢視 http 服務是否已經啟動,或者 http 服務是由 apache 還是 nginx 啟動的,這時候你可以看看程序名。

使用 -p 選項檢視程序資訊。

netstat 基本用法
~$ 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      
netstat 基本用法

使用 -p 選項時,netstat 必須運作在 root 權限之下,不然它就不能得到運作在 root 權限下的程序名,而很多服務包括 http 和 ftp 都運作在 root 權限之下。

相比程序名和程序号而言,檢視程序的擁有者會更有用。使用 -ep 選項可以同時檢視程序名和使用者名。

netstat 基本用法
$ 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      
netstat 基本用法

上面列出 TCP 協定下的監聽套接字,同時顯示程序資訊和一些額外資訊。

這些額外的資訊包括使用者名和程序的索引節點号。這個指令對網管來說很有用。

注意 - 假如你将 -n 和 -e 選項一起使用,User 列的屬性就是使用者的 ID 号,而不是使用者名。

6. 列印統計資料

netstat 可以列印出網絡統計資料,包括某個協定下的收發包數量。

下面列出所有網絡包的統計情況:

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 ...      
netstat 基本用法

如果想隻列印出 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 基本用法
$ 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)      
netstat 基本用法

上面的輸出資訊與 ifconfig 輸出的資訊一樣。

9. netstat 持續輸出

我們可以使用 netstat 的 -c 選項持續輸出資訊。

$ netstat -ct      

這個指令可持續輸出 TCP 協定資訊。

10. 顯示多點傳播組資訊

選項 -g 會輸出 IPv4 和 IPv6 的多點傳播組資訊。

netstat 基本用法
$ 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 基本用法

更多用法

目前為止我們列出了 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。

netstat 基本用法
$ 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      
netstat 基本用法

從這裡可以看到 ntp 服務正在運作。使用 grep 指令你可以檢視 http 或 smtp 或其它任何你想檢視的服務。

好了,netstat 的大部分功能都介紹過了,如果你想知道 netstat 更進階的功能,閱讀它的手冊吧(man netstat)。