linux命令分享:netstat命令詳解,netstat命令詳解

來源:互聯網
上載者:User

linux命令分享:netstat命令詳解,netstat命令詳解

netstat命令用於顯示與IP、TCP、UDP和ICMP協議相關的統計資料,一般用於檢驗本機各連接埠的網路連接情況。netstat是在核心中訪問網路及相關資訊的程式,它能提供TCP串連,TCP和UDP監聽,進程記憶體管理的相關報告。

如果你的電腦有時候接收到的資料報導致出錯資料或故障,你不必感到奇怪,TCP/IP可以容許這些類型的錯誤,並能夠自動重發資料報。但如果累計的出錯情況數目佔到所接收的IP資料報相當大的百分比,或者它的數目正迅速增加,那麼你就應該使用netstat查一查為什麼會出現這些情況了。

1.命令格式:

netstat[-acCeFghilMnNoprstuvVwx][-A<網路類型>][–ip]

2.命令功能:

netstat用於顯示與IP、TCP、UDP和ICMP協議相關的統計資料,一般用於檢驗本機各連接埠的網路連接情況。

3.命令參數:

-a或–all顯示所有連線中的Socket。

-A<網路類型>或–<網路類型>列出該網路類型連線中的相關地址。

-c或–continuous持續列出網路狀態。

-C或–cache顯示路由器配置的快取資訊。

-e或–extend顯示網路其他相關資訊。

-F或–fib顯示FIB。

-g或–groups顯示多重廣播功能群組組員名單。

-h或–help線上協助。

-i或–interfaces顯示網路介面資訊表單。

-l或–listening顯示監控中的伺服器的Socket。

-M或–masquerade顯示偽裝的網路連線。

-n或–numeric直接使用IP地址,而不通過網域名稱伺服器。

-N或–netlink或–symbolic顯示網路硬體外圍裝置的符號串連名稱。

-o或–timers顯示計時器。

-p或–programs顯示正在使用Socket的程式識別碼和程式名稱。

-r或–route顯示RoutingTable。

-s或–statistice顯示網路工作資訊統計表。

-t或–tcp顯示TCP傳輸協議的連線狀況。

-u或–udp顯示UDP傳輸協議的連線狀況。

-v或–verbose顯示指令執行過程。

-V或–version顯示版本資訊。

-w或–raw顯示RAW傳輸協議的連線狀況。

-x或–unix此參數的效果和指定”-Aunix”參數相同。

–ip或–inet此參數的效果和指定”-Ainet”參數相同。

4.使用執行個體:

執行個體1:無參數使用

命令:

netstat

輸出:

[root@localhost~]#netstat

ActiveInternetconnections(w/oservers)

ProtoRecv-QSend-QLocalAddressForeignAddressState

tcp0268192.168.120.204:ssh10.2.0.68:62420ESTABLISHED

udp00192.168.120.204:437110.58.119.119:domainESTABLISHED

ActiveUNIXdomainsockets(w/oservers)

ProtoRefCntFlagsTypeStateI-NodePath

unix2[]DGRAM1491@/org/kernel/udev/udevd

unix4[]DGRAM7337/dev/log

unix2[]DGRAM708823

unix2[]DGRAM7539

unix3[]STREAMCONNECTED7287

unix3[]STREAMCONNECTED7286

[root@localhost~]#

說明:

從整體上看,netstat的輸出結果可以分為兩個部分:

一個是ActiveInternetconnections,稱為有源TCP串連,其中”Recv-Q”和”Send-Q”指的是接收隊列和發送隊列。這些數字一般都應該是0。如果不是則表示軟體包正在隊列中堆積。這種情況只能在非常少的情況見到。

另一個是ActiveUNIXdomainsockets,稱為有源Unix域套介面(和網路通訊端一樣,但是只能用於本機通訊,效能可以提高一倍)。

Proto顯示串連使用的協議,RefCnt表示串連到本套介面上的進程號,Types顯示套介面的類型,State顯示套介面當前的狀態,Path表示串連到套介面的其它進程使用的路徑名。

套介面類型:

-t:TCP

-u:UDP

-raw:RAW類型

–unix:UNIX域類型

–ax25:AX25類型

–ipx:ipx類型

–netrom:netrom類型

狀態說明:

LISTEN:偵聽來自遠方的TCP連接埠的串連請求

SYN-SENT:再發送串連請求後等待匹配的串連請求(如果有大量這樣的狀態包,檢查是否中招了)

SYN-RECEIVED:再收到和發送一個串連請求後等待對方對串連請求的確認(如有大量此狀態,估計被flood攻擊了)

ESTABLISHED:代表一個開啟的串連

FIN-WAIT-1:等待遠程TCP串連插斷要求,或先前的串連插斷要求的確認

FIN-WAIT-2:從遠程TCP等待串連插斷要求

CLOSE-WAIT:等待從本機使用者發來的串連插斷要求

CLOSING:等待遠程TCP對串連中斷的確認

LAST-ACK:等待原來的發向遠程TCP的串連插斷要求的確認(不是什麼好東西,此項出現,檢查是否被攻擊)

TIME-WAIT:等待足夠的時間以確保遠程TCP接收到串連插斷要求的確認

CLOSED:沒有任何串連狀態

執行個體2:列出所有連接埠

命令:

netstat-a

輸出:

[root@localhost~]#netstat-a

ActiveInternetconnections(serversandestablished)

ProtoRecv-QSend-QLocalAddressForeignAddressState

tcp00localhost:smux*:*LISTEN

tcp00*:svn*:*LISTEN

tcp00*:ssh*:*LISTEN

tcp0284192.168.120.204:ssh10.2.0.68:62420ESTABLISHED

udp00localhost:syslog*:*

udp00*:snmp*:*

ActiveUNIXdomainsockets(serversandestablished)

ProtoRefCntFlagsTypeStateI-NodePath

unix2[ACC]STREAMLISTENING708833/tmp/ssh-yKnDB15725/agent.15725

unix2[ACC]STREAMLISTENING7296/var/run/audispd_events

unix2[]DGRAM1491@/org/kernel/udev/udevd

unix4[]DGRAM7337/dev/log

unix2[]DGRAM708823

unix2[]DGRAM7539

unix3[]STREAMCONNECTED7287

unix3[]STREAMCONNECTED7286

[root@localhost~]#

說明:

顯示一個所有的有效串連資訊列表,包括已建立的串連(ESTABLISHED),也包括監聽串連請(LISTENING)的那些串連。

執行個體3:顯示當前UDP串連狀況

命令:

netstat-nu

輸出:

[root@andy~]#netstat-nu

ActiveInternetconnections(w/oservers)

ProtoRecv-QSend-QLocalAddressForeignAddressState

udp00::ffff:192.168.12:53392::ffff:192.168.9.120:10000ESTABLISHED

udp00::ffff:192.168.12:56723::ffff:192.168.9.120:10000ESTABLISHED

udp00::ffff:192.168.12:56480::ffff:192.168.9.120:10000ESTABLISHED

udp00::ffff:192.168.12:58154::ffff:192.168.9.120:10000ESTABLISHED

udp00::ffff:192.168.12:44227::ffff:192.168.9.120:10000ESTABLISHED

udp00::ffff:192.168.12:36954::ffff:192.168.9.120:10000ESTABLISHED

udp00::ffff:192.168.12:53984::ffff:192.168.9.120:10000ESTABLISHED

udp00::ffff:192.168.12:57703::ffff:192.168.9.120:10000ESTABLISHED

udp00::ffff:192.168.12:53613::ffff:192.168.9.120:10000ESTABLISHED

[root@andy~]#

說明:

執行個體4:顯示UDP連接埠號碼的使用方式

命令:

netstat-apu

輸出:

[root@andy~]#netstat-apu

ActiveInternetconnections(serversandestablished)

ProtoRecv-QSend-QLocalAddressForeignAddressStatePID/Programname

udp00*:57604*:*28094/java

udp00*:40583*:*21220/java

udp00*:45451*:*14583/java

udp00::ffff:192.168.12:53392::ffff:192.168.9.120:ndmpESTABLISHED19327/java

udp00*:52370*:*15841/java

udp00::ffff:192.168.12:56723::ffff:192.168.9.120:ndmpESTABLISHED15841/java

udp00*:44182*:*31757/java

udp00*:48155*:*5476/java

udp00*:59808*:*17333/java

udp00::ffff:192.168.12:56480::ffff:192.168.9.120:ndmpESTABLISHED28094/java

udp00::ffff:192.168.12:58154::ffff:192.168.9.120:ndmpESTABLISHED15429/java

udp00*:36780*:*10091/java

udp00*:36795*:*24594/java

udp00*:41922*:*20506/java

udp00::ffff:192.168.12:44227::ffff:192.168.9.120:ndmpESTABLISHED17333/java

udp00*:34258*:*8866/java

udp00*:55508*:*11667/java

udp00*:36055*:*12425/java

udp00::ffff:192.168.12:36954::ffff:192.168.9.120:ndmpESTABLISHED16532/java

udp00::ffff:192.168.12:53984::ffff:192.168.9.120:ndmpESTABLISHED20506/java

udp00::ffff:192.168.12:57703::ffff:192.168.9.120:ndmpESTABLISHED31757/java

udp00::ffff:192.168.12:53613::ffff:192.168.9.120:ndmpESTABLISHED3199/java

udp00*:56309*:*15429/java

udp00*:54007*:*16532/java

udp00*:39544*:*3199/java

udp00*:43900*:*19327/java

[root@andy~]#

說明:

執行個體5:顯示網卡列表

命令:

netstat-i

輸出:

[root@andy~]#netstat-i

KernelInterfacetable

IfaceMTUMetRX-OKRX-ERRRX-DRPRX-OVRTX-OKTX-ERRTX-DRPTX-OVRFlg

eth015000151818887000198928403000BMRU

lo164360107235000107235000LRU

[root@andy~]#

說明:

執行個體6:顯示組播組的關係

命令:

netstat-g

輸出:

[root@andy~]#netstat-g

IPv6/IPv4GroupMemberships

InterfaceRefCntGroup

——————————————

lo1all-systems.mcast.net

eth01all-systems.mcast.net

lo1ff02::1

eth01ff02::1:ffff:9b0c

eth01ff02::1

[root@andy~]#

說明:

執行個體7:顯示網路統計資訊

命令:

netstat-s

輸出:

[root@localhost~]#netstat-s

Ip:

530999totalpacketsreceived

0forwarded

0incomingpacketsdiscarded

530999incomingpacketsdelivered

8258requestssentout

1droppedbecauseofmissingroute

Icmp:

90ICMPmessagesreceived

0inputICMPmessagefailed.

ICMPinputhistogram:

destinationunreachable:17

echorequests:1

echoreplies:72

106ICMPmessagessent

0ICMPmessagesfailed

ICMPoutputhistogram:

destinationunreachable:8

echorequest:97

echoreplies:1

IcmpMsg:

InType0:72

InType3:17

InType8:1

OutType0:1

OutType3:8

OutType8:97

Tcp:

8activeconnectionsopenings

15passiveconnectionopenings

8failedconnectionattempts

3connectionresetsreceived

1connectionsestablished

3132segmentsreceived

2617segmentssendout

53segmentsretransmited

0badsegmentsreceived.

252resetssent

Udp:

0packetsreceived

0packetstounknownportreceived.

0packetreceiveerrors

5482packetssent

TcpExt:

1invalidSYNcookiesreceived

1TCPsocketsfinishedtimewaitinfasttimer

57delayedackssent

Quickackmodewasactivated50times

60packetsdirectlyqueuedtorecvmsgprequeue.

68packetsdirectlyreceivedfrombacklog

4399packetsdirectlyreceivedfromprequeue

520packetsheaderpredicted

51packetsheaderpredictedanddirectlyqueuedtouser

1194acknowledgmentsnotcontainingdatareceived

21predictedacknowledgments

0TCPdatalossevents

1timeoutsafterrenofastretransmit

9retransmitsinslowstart

42otherTCPtimeouts

3connectionsabortedduetotimeout

IpExt:

InBcastPkts:527777

說明:

按照各個協議分別顯示其統計資料。如果我們的應用程式(如Web瀏覽器)運行速度比較慢,或者不能顯示Web頁之類的資料,那麼我們就可以用本選項來查看一下所顯示的資訊。我們需要仔細查看統計資料的各行,找到出錯的關鍵字,進而確定問題所在。

執行個體8:顯示監聽的套介面

命令:

netstat-l

輸出:

[root@localhost~]#netstat-l

ActiveInternetconnections(onlyservers)

ProtoRecv-QSend-QLocalAddressForeignAddressState

tcp00localhost:smux*:*LISTEN

tcp00*:svn*:*LISTEN

tcp00*:ssh*:*LISTEN

udp00localhost:syslog*:*

udp00*:snmp*:*

ActiveUNIXdomainsockets(onlyservers)

ProtoRefCntFlagsTypeStateI-NodePath

unix2[ACC]STREAMLISTENING708833/tmp/ssh-yKnDB15725/agent.15725

unix2[ACC]STREAMLISTENING7296/var/run/audispd_events

[root@localhost~]#

說明:

執行個體9:顯示所有已建立的有效串連

命令:

netstat-n

輸出:

[root@localhost~]#netstat-n

ActiveInternetconnections(w/oservers)

ProtoRecv-QSend-QLocalAddressForeignAddressState

tcp0268192.168.120.204:2210.2.0.68:62420ESTABLISHED

ActiveUNIXdomainsockets(w/oservers)

ProtoRefCntFlagsTypeStateI-NodePath

unix2[]DGRAM1491@/org/kernel/udev/udevd

unix4[]DGRAM7337/dev/log

unix2[]DGRAM708823

unix2[]DGRAM7539

unix3[]STREAMCONNECTED7287

unix3[]STREAMCONNECTED7286

[root@localhost~]#

說明:

執行個體10:顯示關於乙太網路的統計資料

命令:

netstat-e

輸出:

[root@localhost~]#netstat-e

ActiveInternetconnections(w/oservers)

ProtoRecv-QSend-QLocalAddressForeignAddressStateUserInode

tcp0248192.168.120.204:ssh10.2.0.68:62420ESTABLISHEDroot708795

ActiveUNIXdomainsockets(w/oservers)

ProtoRefCntFlagsTypeStateI-NodePath

unix2[]DGRAM1491@/org/kernel/udev/udevd

unix4[]DGRAM7337/dev/log

unix2[]DGRAM708823

unix2[]DGRAM7539

unix3[]STREAMCONNECTED7287

unix3[]STREAMCONNECTED7286

[root@localhost~]#

說明:

用於顯示關於乙太網路的統計資料。它列出的項目包括傳送的資料報的總位元組數、錯誤數、刪除數、資料報的數量和廣播的數量。這些統計資料既有發送的資料報數量,也有接收的資料報數量。這個選項可以用來統計一些基本的網路流量)

執行個體11:顯示關於路由表的資訊

命令:

netstat-r

輸出:

[root@localhost~]#netstat-r

KernelIProutingtable

DestinationGatewayGenmaskFlagsMSSWindowirttIface

192.168.120.0*255.255.255.0U000eth0

192.168.0.0192.168.120.1255.255.0.0UG000eth0

10.0.0.0192.168.120.1255.0.0.0UG000eth0

default192.168.120.2400.0.0.0UG000eth0

[root@localhost~]#

說明:

執行個體12:列出所有tcp連接埠

命令:

netstat-at

輸出:

[root@localhost~]#netstat-at

ActiveInternetconnections(serversandestablished)

ProtoRecv-QSend-QLocalAddressForeignAddressState

tcp00localhost:smux*:*LISTEN

tcp00*:svn*:*LISTEN

tcp00*:ssh*:*LISTEN

tcp0284192.168.120.204:ssh10.2.0.68:62420ESTABLISHED

[root@localhost~]#

說明:

執行個體13:統計機器中網路連接各個狀態個數

命令:

netstat-a|awk’/^tcp/{++S[NF]} END {for(a in S) print a, S[a]}'

輸出:

[root@localhost ~]# netstat -a | awk '/^tcp/ {++S[NF]} END {for(a in S) print a, S[a]}'

ESTABLISHED 1

LISTEN 3

[root@localhost ~]#

說明:

執行個體14:把狀態全都取出來後使用uniq -c統計後再進行排序

命令:

netstat -nat |awk '{print 6}'|sort|uniq -c

輸出:

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.