Netstat命令介紹

                             Netstat命令介紹

創(chuàng)新互聯(lián)專注于企業(yè)營銷型網(wǎng)站建設(shè)、網(wǎng)站重做改版、藤縣網(wǎng)站定制設(shè)計、自適應(yīng)品牌網(wǎng)站建設(shè)、HTML5建站、商城網(wǎng)站建設(shè)、集團公司官網(wǎng)建設(shè)、成都外貿(mào)網(wǎng)站建設(shè)公司、高端網(wǎng)站制作、響應(yīng)式網(wǎng)頁設(shè)計等建站業(yè)務(wù),價格優(yōu)惠性價比高,為藤縣等各大城市提供網(wǎng)站開發(fā)制作服務(wù)。

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é)果可以分為兩個部分:

一個是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表示連接到本套接口上的進程號,

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

常見參數(shù)

-a (all)顯示所有選項,默認不顯示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é)議進行統(tǒng)計

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

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

實用命令實例

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

【列出所有端口 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ài)的 Sockets

【只顯示監(jiān)聽端口 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)聽 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)聽 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)聽 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. 顯示每個協(xié)議的統(tǒng)計信息

【顯示所有端口的統(tǒng)計信息 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)計信息 netstat -st 或 -su 】

4. 在 netstat 輸出中顯示 PID 和進程名稱 netstat -p

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

這樣 debugging 的時候可以很方便的發(fā)現(xià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 輸出中不顯示主機,端口和用戶名 (host, port or user)

當你不想讓主機,端口和用戶名顯示,使用 netstat -n。將會使用數(shù)字代替那些名稱。

同樣可以加速輸出,因為不用進行比對查詢。

# netstat -an

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

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

# netstat -c

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

netstat --verbose

在輸出的末尾,會有如下的信息

[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ù)字格式,不查詢主機名稱

【9. 找出程序運行的端口】

并不是所有的進程都能找到,沒有權(quán)限的會不顯示,使用 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

找出運行在指定端口的進程

# 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

顯示詳細信息,像是 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)全都取出來,然后使用uniq -c統(tǒng)計,之后再進行排序。

[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獲得訪問前10位的ip地址

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

當前標題:Netstat命令介紹
分享地址:http://muchs.cn/article20/pdpcco.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供動態(tài)網(wǎng)站、定制網(wǎng)站、建站公司、定制開發(fā)、網(wǎng)站改版、外貿(mào)網(wǎng)站建設(shè)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都定制網(wǎng)站網(wǎng)頁設(shè)計