溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶(hù)服務(wù)條款》

Netstat命令介紹

發(fā)布時(shí)間:2020-06-19 02:46:25 來(lái)源:網(wǎng)絡(luò) 閱讀:1338 作者:jiaohuanji88 欄目:系統(tǒng)運(yùn)維

                             Netstat命令介紹


Netstat 命令用于顯示各種網(wǎng)絡(luò)相關(guān)信息,如網(wǎng)絡(luò)連接,路由表,接口狀態(tài) (Interface Statistics),masquerade 連接,

多播成員 (Multicast Memberships) 等等。


輸出信息含義


執(zhí)行netstat后,其輸出結(jié)果為

[root@clientA ~]# netstat
Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address               Foreign Address             State
tcp        0      0 192.168.20.5:ssh            192.168.20.6:56605          ESTABLISHED
Active UNIX domain sockets (w/o servers)
Proto RefCnt Flags       Type       State         I-Node Path
unix  2      [ ]         DGRAM                    8447   @/org/kernel/udev/udevd
unix  13     [ ]         DGRAM                    10614  /dev/log
unix  2      [ ]         DGRAM                    136984
unix  2      [ ]         DGRAM                    136953
unix  2      [ ]         DGRAM                    136889
unix  2      [ ]         DGRAM                    99938
unix  2      [ ]         DGRAM                    55471
unix  2      [ ]         DGRAM                    11897
unix  2      [ ]         DGRAM                    11882
unix  3      [ ]         STREAM     CONNECTED     11861
unix  3      [ ]         STREAM     CONNECTED     11860
unix  3      [ ]         STREAM     CONNECTED     11857
unix  3      [ ]         STREAM     CONNECTED     11856
[root@clientA ~]#

從整體上看,netstat的輸出結(jié)果可以分為兩個(gè)部分:


一個(gè)是Active Internet connections,稱(chēng)為有源TCP連接,其中"Recv-Q"和"Send-Q"指%0A的是接收隊(duì)列和發(fā)送隊(duì)列。這些數(shù)字一般都應(yīng)該是0。

如果不是則表示軟件包正在隊(duì)列中堆積。這種情況只能在非常少的情況見(jiàn)到。另一個(gè)是Active UNIX domain sockets,稱(chēng)為有源Unix域套接口

(和網(wǎng)絡(luò)套接字一樣,但是只能用于本機(jī)通信,性能可以提高一倍)。Proto顯示連接使用的協(xié)議,RefCnt表示連接到本套接口上的進(jìn)程號(hào),

Types顯示套接口的類(lèi)型,State顯示套接口當(dāng)前的狀態(tài),Path表示連接到套接口的其它進(jìn)程使用的路徑名。


常見(jiàn)參數(shù)


-a (all)顯示所有選項(xiàng),默認(rèn)不顯示LISTEN相關(guān)


-t (tcp)僅顯示tcp相關(guān)選項(xiàng)


-u (udp)僅顯示udp相關(guān)選項(xiàng)


-n 拒絕顯示別名,能顯示數(shù)字的全部轉(zhuǎn)化成數(shù)字。


-l 僅列出有在 Listen (監(jiān)聽(tīng)) 的服務(wù)狀態(tài)


-p 顯示建立相關(guān)鏈接的程序名


-r 顯示路由信息,路由表


-e 顯示擴(kuò)展信息,例如uid等


-s 按各個(gè)協(xié)議進(jìn)行統(tǒng)計(jì)


-c 每隔一個(gè)固定時(shí)間,執(zhí)行該netstat命令。


提示:LISTEN和LISTENING的狀態(tài)只有用-a或者-l才能看到


實(shí)用命令實(shí)例


1. 列出所有端口 (包括監(jiān)聽(tīng)和未監(jiān)聽(tīng)的)


【列出所有端口 netstat -a】

[root@clientA ~]#  netstat -a | more
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address               Foreign Address             State
tcp        0      0 192.168.20.5:rsync          *:*                         LISTEN
tcp        0      0 *:sunrpc                    *:*                         LISTEN
tcp        0      0 *:ssh                       *:*                         LISTEN
tcp        0      0 localhost.localdomain:smtp  *:*                         LISTEN
tcp        0      0 *:52923                     *:*                         LISTEN
tcp        0      0 *:57214                     *:*                         LISTEN
tcp        0      0 *:51327                     *:*                         LISTEN
Active UNIX domain sockets (servers and established)
Proto RefCnt Flags       Type       State         I-Node Path
unix  2      [ ACC ]     STREAM     LISTENING     11775  public/cleanup
unix  2      [ ACC ]     STREAM     LISTENING     11782  private/tlsmgr
unix  2      [ ACC ]     STREAM     LISTENING     11786  private/rewrite
unix  2      [ ACC ]     STREAM     LISTENING     11790  private/bounce
unix  2      [ ACC ]     STREAM     LISTENING     11794  private/defer

【列出所有 tcp 端口 netstat -at】

[root@clientA ~]# netstat -at
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address               Foreign Address             State
tcp        0      0 192.168.20.5:rsync          *:*                         LISTEN
tcp        0      0 *:sunrpc                    *:*                         LISTEN
tcp        0      0 *:ssh                       *:*                         LISTEN
tcp        0      0 localhost.localdomain:smtp  *:*                         LISTEN
tcp        0      0 *:52923                     *:*                         LISTEN
tcp        0      0 *:57214                     *:*                         LISTEN
tcp        0      0 *:51327                     *:*                         LISTEN
tcp        0      0 *:nfs                       *:*                         LISTEN

【列出所有 udp 端口 netstat -au 】

[root@clientA ~]# netstat -au
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address               Foreign Address             State
udp        0      0 *:sunrpc                    *:*
udp        0      0 *:nfs                       *:*
udp        0      0 *:smpnameres                *:*
udp        0      0 *:58775                     *:*
udp        0      0 localhost.localdomain:920   *:*
udp        0      0 *:37294                     *:*
udp        0      0 *:58800                     *:*

2. 列出所有處于監(jiān)聽(tīng)狀態(tài)的 Sockets


【只顯示監(jiān)聽(tīng)端口 netstat -l 】

[root@clientA ~]# netstat -l
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address               Foreign Address             State
tcp        0      0 192.168.20.5:rsync          *:*                         LISTEN
tcp        0      0 *:sunrpc                    *:*                         LISTEN
tcp        0      0 *:ssh                       *:*                         LISTEN
tcp        0      0 localhost.localdomain:smtp  *:*                         LISTEN
tcp        0      0 *:52923                     *:*                         LISTEN
tcp        0      0 *:57214                     *:*                         LISTEN
tcp        0      0 *:51327                     *:*                         LISTEN
Active UNIX domain sockets (only servers)
Proto RefCnt Flags       Type       State         I-Node Path
unix  2      [ ACC ]     STREAM     LISTENING     11775  public/cleanup
unix  2      [ ACC ]     STREAM     LISTENING     11782  private/tlsmgr
unix  2      [ ACC ]     STREAM     LISTENING     11786  private/rewrite
unix  2      [ ACC ]     STREAM     LISTENING     11790  private/bounce
unix  2      [ ACC ]     STREAM     LISTENING     11794  private/defer
unix  2      [ ACC ]     STREAM     LISTENING     11798  private/trace
unix  2      [ ACC ]     STREAM     LISTENING     11802  private/verify
unix  2      [ ACC ]     STREAM     LISTENING     11806  public/flush
unix  2      [ ACC ]     STREAM     LISTENING     11810  private/proxymap


【只列出所有監(jiān)聽(tīng) tcp 端口 netstat -lt 】

[root@clientA ~]# netstat -lt
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address               Foreign Address             State
tcp        0      0 192.168.20.5:rsync          *:*                         LISTEN
tcp        0      0 *:sunrpc                    *:*                         LISTEN
tcp        0      0 *:ssh                       *:*                         LISTEN
tcp        0      0 localhost.localdomain:smtp  *:*                         LISTEN
tcp        0      0 *:52923                     *:*                         LISTEN
tcp        0      0 *:57214                     *:*                         LISTEN
tcp        0      0 *:51327                     *:*                         LISTEN
tcp        0      0 *:nfs                       *:*                         LISTEN

【只列出所有監(jiān)聽(tīng) udp 端口 netstat -lu 】

[root@clientA ~]# netstat -lu
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address               Foreign Address             State
udp        0      0 *:sunrpc                    *:*
udp        0      0 *:nfs                       *:*
udp        0      0 *:smpnameres                *:*
udp        0      0 *:58775                     *:*
udp        0      0 localhost.localdomain:920   *:*
udp        0      0 *:37294                     *:*
udp        0      0 *:58800                     *:*
udp        0      0 *:39099                     *:*
udp        0      0 *:bootpc                    *:*

【只列出所有監(jiān)聽(tīng) UNIX 端口 netstat -lx】

[root@clientA ~]# netstat -lx
Active UNIX domain sockets (only servers)
Proto RefCnt Flags       Type       State         I-Node Path
unix  2      [ ACC ]     STREAM     LISTENING     11775  public/cleanup
unix  2      [ ACC ]     STREAM     LISTENING     11782  private/tlsmgr
unix  2      [ ACC ]     STREAM     LISTENING     11786  private/rewrite
unix  2      [ ACC ]     STREAM     LISTENING     11790  private/bounce
unix  2      [ ACC ]     STREAM     LISTENING     11794  private/defer
unix  2      [ ACC ]     STREAM     LISTENING     11798  private/trace
unix  2      [ ACC ]     STREAM     LISTENING     11802  private/verify

3. 顯示每個(gè)協(xié)議的統(tǒng)計(jì)信息


【顯示所有端口的統(tǒng)計(jì)信息 netstat -s 】

[root@clientA ~]# netstat -s
Ip:
    25997 total packets received
    1 with invalid addresses
    0 forwarded
    0 incoming packets discarded
    23969 incoming packets delivered
    20845 requests sent out
Icmp:
    1539 ICMP messages received
    0 input ICMP message failed.
    ICMP input histogram:
        destination unreachable: 1539
    1539 ICMP messages sent
    0 ICMP messages failed
    ICMP output histogram:
        destination unreachable: 1539

【顯示 TCP 或 UDP 端口的統(tǒng)計(jì)信息 netstat -st 或 -su 】

4. 在 netstat 輸出中顯示 PID 和進(jìn)程名稱(chēng) netstat -p

netstat -p 可以與其它開(kāi)關(guān)一起使用,就可以添加 “PID/進(jìn)程名稱(chēng)” 到 netstat 輸出中,

這樣 debugging 的時(shí)候可以很方便的發(fā)現(xiàn)特定端口運(yùn)行的程序

[root@clientA ~]# netstat -pt
Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address               Foreign Address             State       PID/Program name
tcp        0      0 192.168.20.5:ssh            192.168.20.6:56605          ESTABLISHED 10120/0

5. 在 netstat 輸出中不顯示主機(jī),端口和用戶(hù)名 (host, port or user)


當(dāng)你不想讓主機(jī),端口和用戶(hù)名顯示,使用 netstat -n。將會(huì)使用數(shù)字代替那些名稱(chēng)。


同樣可以加速輸出,因?yàn)椴挥眠M(jìn)行比對(duì)查詢(xún)。


# netstat -an



6. 持續(xù)輸出 netstat 信息


netstat 將每隔一秒輸出網(wǎng)絡(luò)信息。


# netstat -c


7. 顯示系統(tǒng)不支持的地址族 (Address Families)


netstat --verbose


在輸出的末尾,會(huì)有如下的信息

[root@clientA ~]netstat --verbose

【 顯示核心路由信息 netstat -r】

[root@clientA ~]# netstat -r
Kernel IP routing table
Destination     Gateway         Genmask         Flags   MSS Window  irtt Iface
192.168.20.0    *               255.255.255.0   U         0 0          0 eth0
link-local      *               255.255.0.0     U         0 0          0 eth0
default         192.168.20.1    0.0.0.0         UG        0 0          0 eth0

注意: 使用 netstat -rn 顯示數(shù)字格式,不查詢(xún)主機(jī)名稱(chēng)

【9. 找出程序運(yùn)行的端口】

并不是所有的進(jìn)程都能找到,沒(méi)有權(quán)限的會(huì)不顯示,使用 root 權(quán)限查看所有的信息

[root@clientA ~]# netstat -ap | grep ssh
tcp        0      0 *:ssh                       *:*                         LISTEN      1284/sshd          
tcp        0     64 192.168.20.5:ssh            192.168.20.6:56605          ESTABLISHED 10120/0            
tcp        0      0 *:ssh                       *:*                         LISTEN      1284/sshd


找出運(yùn)行在指定端口的進(jìn)程

# netstat -an | grep ':80'
[root@clientA ~]# netstat -an | grep "80"
udp        0      0 0.0.0.0:58800               0.0.0.0:*
unix  2      [ ACC ]     STREAM     LISTENING     11802  private/verify
unix  2      [ ACC ]     STREAM     LISTENING     11806  public/flush
unix  3      [ ]         STREAM     CONNECTED     11809

10. 顯示網(wǎng)絡(luò)接口列表

# 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 0000 00000 BMU
eth3 1500 02619600 026883600 BMRU
lo 16436 0400 04000 LRU


顯示詳細(xì)信息,像是 ifconfig 使用 netstat -ie:


# netstat -ie


【IP和TCP分析】

查看連接某服務(wù)端口最多的的IP地址:

[root@clientA ~]# netstat -nat | grep "192.168.20.5" |awk '{print $5}'|awk -F: '{print $1}'|sort|uniq -c|sort -nr|head -20
      1 192.168.20.6
      1 0.0.0.0

TCP各種狀態(tài)列表

[root@clientA ~]# netstat -nat |awk '{print $6}'
established)
Foreign
LISTEN
LISTEN
LISTEN
LISTEN

先把狀態(tài)全都取出來(lái),然后使用uniq -c統(tǒng)計(jì),之后再進(jìn)行排序。

[root@clientA ~]# netstat -nat |awk '{print $6}'|sort|uniq -c
      1 established)
      1 ESTABLISHED
      1 Foreign
     18 LISTEN

最后的命令如下:

netstat -nat |awk '{print $6}'|sort|uniq -c|sort -rn
[root@clientA ~]# netstat -nat |awk '{print $6}'|sort|uniq -c|sort -rn
     18 LISTEN
      1 Foreign
      1 ESTABLISHED
      1 established

分析access.log獲得訪問(wèn)前10位的ip地址


awk '{print $1}' access.log |sort|uniq -c|sort -nr|head -10



向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI