Linux netstat命令詳解
簡介
Netstat 命令用于顯示各種網(wǎng)絡(luò)相關(guān)信息,如網(wǎng)絡(luò)連接,路由表,接口狀態(tài) (Interface Statistics),masquerade 連接,多播成員 (Multicast Memberships) 等等。
輸出信息含義
執(zhí)行netstat后,其輸出結(jié)果為
- 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的輸出結(jié)果可以分為兩個部分:
一個是Active Internet connections,稱為有源TCP連接,其中"Recv-Q"和"Send-Q"指%0A的是接收隊列和發(fā)送隊列。這些數(shù)字一般都應(yīng)該是0。如果不是則表示軟件包正在隊列中堆積。這種情況只能在非常少的情況見到。
另一個是Active UNIX domain sockets,稱為有源Unix域套接口(和網(wǎng)絡(luò)套接字一樣,但是只能用于本機(jī)通信,性能可以提高一倍)。Proto顯示連接使用的協(xié)議,RefCnt表示連接到本套接口上的進(jìn)程號,Types顯示套接口的類型,State顯示套接口當(dāng)前的狀態(tài),Path表示連接到套接口的其它進(jìn)程使用的路徑名。
常見參數(shù)
-a (all)顯示所有選項,默認(rèn)不顯示LISTEN相關(guān) -t (tcp)僅顯示tcp相關(guān)選項 -u (udp)僅顯示udp相關(guān)選項 -n 拒絕顯示別名,能顯示數(shù)字的全部轉(zhuǎn)化成數(shù)字。 -l 僅列出有在 Listen (監(jiān)聽) 的服務(wù)狀態(tài) -p 顯示建立相關(guān)鏈接的程序名 -r 顯示路由信息,路由表 -e 顯示擴(kuò)展信息,例如uid等 -s 按各個協(xié)議進(jìn)行統(tǒng)計 -c 每隔一個固定時間,執(zhí)行該netstat命令。
提示:LISTEN和LISTENING的狀態(tài)只有用-a或者-l才能看到
實用命令實例
1.列出所有端口 (包括監(jiān)聽和未監(jiān)聽的)
列出所有端口 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 *:*
2. 列出所有處于監(jiān)聽狀態(tài)的 Sockets
只顯示監(jiān)聽端口 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 *:*
只列出所有監(jiān)聽 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
只列出所有監(jiān)聽 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 *:*
只列出所有監(jiān)聽 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