簡介
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ò)套接字一樣稿黄,但是只能用于本機通信喊衫,性能可以提高一倍)。
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 顯示擴展信息,例如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
udp00*:bootpc? ? ? ? ? ? ? ? *:*
udp00*:49119*:*
udp00*:mdns? ? ? ? ? ? ? ? ? *:*
查看nginx服務(wù)端口占用
# netstat -anp|grep nginx