一.概述:
netstat命令用于顯示與IP、TCP、UDP和ICMP協(xié)議相關(guān)的統(tǒng)計(jì)數(shù)據(jù),一般用于檢驗(yàn)本機(jī)各端口的網(wǎng)絡(luò)連接情況。netstat是在內(nèi)核中訪問(wèn)網(wǎng)絡(luò)及相關(guān)信息的程序,它能提供TCP連接,TCP和UDP監(jiān)聽(tīng),進(jìn)程內(nèi)存管理的相關(guān)報(bào)告。
如果你的計(jì)算機(jī)有時(shí)候接收到的數(shù)據(jù)報(bào)導(dǎo)致出錯(cuò)數(shù)據(jù)或故障,你不必感到奇怪,TCP/IP可以容許這些類(lèi)型的錯(cuò)誤,并能夠自動(dòng)重發(fā)數(shù)據(jù)報(bào)。但如果累計(jì)的出錯(cuò)情況數(shù)目占到所接收的IP數(shù)據(jù)報(bào)相當(dāng)大的百分比,或者它的數(shù)目正迅速增加,那么你就應(yīng)該使用netstat查一查為什么會(huì)出現(xiàn)這些情況了。
二.常見(jiàn)格式:
-a 顯示所有選項(xiàng),默認(rèn)不顯示LISTEN相關(guān)
-t 僅顯示tcp相關(guān)選項(xiàng)
-u 僅顯示udp相關(guān)選項(xiàng)
-n (numeric)拒絕顯示別名,能顯示數(shù)字的全部轉(zhuǎn)化成數(shù)字。
-l 僅列出有在 Listen (監(jiān)聽(tīng)) 的服務(wù)狀態(tài)
-p 顯示建立相關(guān)鏈接的程序名
-r 顯示路由信息,路由表
-e (extend)顯示擴(kuò)展信息,例如uid等
-s (statistice)按各個(gè)協(xié)議進(jìn)行統(tǒng)計(jì)
-c (continuous)每隔一個(gè)固定時(shí)間,執(zhí)行該netstat命令。
三.使用實(shí)例:
列出所有端口 netstat -a
# netstat -a | more Active Internet connections (servers and established) Proto Recv-Q Send-Q Local Address Foreign Address State tcp localhost: *:* LISTEN udp *:bootpc *:* Active UNIX domain sockets (servers and established) Proto RefCnt Flags Type State I-Node Path unix [ ACC ] STREAM LISTENING /tmp/.X11-unix/X0 unix [ ACC ] STREAM LISTENING //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 localhost: *:* LISTEN tcp localhost:ipp *:* LISTEN tcp *:smtp *:* LISTEN tcp6 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 *:bootpc *:* udp *: *:* udp *:mdns *:*
只顯示監(jiān)聽(tīng)端口 netstat -l
# netstat -l Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State tcp localhost:ipp *:* LISTEN tcp6 localhost:ipp [::]:* LISTEN udp *: *:*
只列出所有監(jiān)聽(tīng) tcp 端口 netstat -lt
# netstat -lt Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State tcp localhost: *:* LISTEN tcp *:smtp *:* LISTEN tcp6 localhost:ipp [::]:* LISTEN
只列出所有監(jiān)聽(tīng) udp 端口 netstat -lu
# netstat -lu Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State udp *: *:* udp *:mdns *:*
只列出所有監(jiān)聽(tīng) UNIX 端口 netstat -lx
# netstat -lx Active UNIX domain sockets (only servers) Proto RefCnt Flags Type State I-Node Path unix [ ACC ] STREAM LISTENING /maildrop unix [ ACC ] STREAM LISTENING /cleanup unix [ ACC ] STREAM LISTENING /ifmail unix [ ACC ] STREAM LISTENING /bsmtp
顯示所有端口的統(tǒng)計(jì)信息 netstat -s
# netstat -s Ip: total packets received with invalid addresses forwarded incoming packets discarded incoming packets delivered requests sent Icmp: ICMP messages received input ICMP message failed. Tcp: active connections openings failed connection attempts connection resets received Udp: packets received packets to unknown port received. .....
顯示 TCP 或 UDP 端口的統(tǒng)計(jì)信息 netstat -st 或 -su
# netstat -st # netstat -su
netstat -p 可以與其它開(kāi)關(guān)一起使用,就可以添加 “PID/進(jìn)程名稱(chēng)” 到 netstat 輸出中,這樣 debugging 的時(shí)候可以很方便的發(fā)現(xiàn)特定端口運(yùn)行的程序。
# netstat -pt Active Internet connections (w/o servers) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp ramesh-laptop.loc: .:www CLOSE_WAIT /firefox tcp ramesh-laptop.loc: lax:www ESTABLISHED /firefox
當(dāng)你不想讓主機(jī),端口和用戶(hù)名顯示,使用 netstat -n。將會(huì)使用數(shù)字代替那些名稱(chēng)。
同樣可以加速輸出,因?yàn)椴挥眠M(jìn)行比對(duì)查詢(xún)。
# netstat -an
如果只是不想讓這三個(gè)名稱(chēng)中的一個(gè)被顯示,使用以下命令
# netsat -a --numeric-ports # netsat -a --numeric-hosts # netsat -a --numeric-users
netstat 將每隔一秒輸出網(wǎng)絡(luò)信息。
# netstat -c Active Internet connections (w/o servers) Proto Recv-Q Send-Q Local Address Foreign Address State tcp ramesh-laptop.loc: ---.ama:www ESTABLISHED tcp ramesh-laptop.loc: .:www CLOSING tcp ramesh-laptop.loc: server----:www ESTABLISHED tcp ramesh-laptop.loc: .:www CLOSING ^C
netstat --verbose
在輸出的末尾,會(huì)有如下的信息
netstat: no support `AF IPXnetstat: no support `AF AX25netstat: no support `AF X25netstat: no support `AF NETROM
# netstat -r Kernel IP routing table Destination Gateway Genmask Flags MSS Window irtt Iface . * . U eth3 link-local * . U eth3 . . UG eth3
注意: 使用 netstat -rn 顯示數(shù)字格式,不查詢(xún)主機(jī)名稱(chēng)。
并不是所有的進(jìn)程都能找到,沒(méi)有權(quán)限的會(huì)不顯示,使用 root 權(quán)限查看所有的信息。
# netstat -ap | grep ssh tcp dev-db:ssh .: CLOSE_WAIT - tcp dev-db:ssh .: CLOSE_WAIT -
找出運(yùn)行在指定端口的進(jìn)程
# netstat -an | grep ':80'
# 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 BMU eth3 BMRU lo LRU
顯示詳細(xì)信息,像是 ifconfig 使用 netstat -ie:
# netstat -ie Kernel Interface table eth0 Link encap:Ethernet HWaddr ::::: UP BROADCAST MULTICAST MTU: Metric: RX packets: errors: dropped: overruns: frame: TX packets: errors: dropped: overruns: carrier: collisions: txqueuelen: RX bytes: ( B) TX bytes: ( B) Memory:f6ae0000-f6b00000
查看連接某服務(wù)端口最多的的IP地址
wss8848@ubuntu:~$ netstat -nat | grep |awk |awk -F: |sort|uniq -c|sort -nr|head - . . . . . . . . . .
TCP各種狀態(tài)列表
wss8848@ubuntu:~$ netstat -nat |awk established) Foreign LISTEN TIME_WAIT ESTABLISHED TIME_WAIT SYN_SENT先把狀態(tài)全都取出來(lái),然后使用uniq -c統(tǒng)計(jì),之后再進(jìn)行排序。
wss8848@ubuntu:~$ netstat -nat |awk |sort|uniq -c ESTABLISHED FIN_WAIT1 Foreign LAST_ACK LISTEN SYN_SENT TIME_WAIT established)
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)cdcxhl.cn,海內(nèi)外云服務(wù)器15元起步,三天無(wú)理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性?xún)r(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專(zhuān)為企業(yè)上云打造定制,能夠滿(mǎn)足用戶(hù)豐富、多元化的應(yīng)用場(chǎng)景需求。
網(wǎng)頁(yè)題目:netstat簡(jiǎn)單使用-創(chuàng)新互聯(lián)
文章來(lái)源:http://www.chinadenli.net/article36/cchcpg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站營(yíng)銷(xiāo)、網(wǎng)站排名、響應(yīng)式網(wǎng)站、定制網(wǎng)站、定制開(kāi)發(fā)、靜態(tài)網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容