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 顯示Routing Table。
  • -s–statistice 顯示網路工作資訊統計表。
  • -t–tcp 顯示TCP傳輸協定的連線狀況。
  • -u–udp 顯示UDP傳輸協定的連線狀況。
  • -v–verbose 顯示指令執行過程。
  • -V–version 顯示版本資訊。
  • -w–raw 顯示RAW傳輸協定的連線狀況。
  • -x–unix 此引數的效果和指定」-A unix」引數相同。
  • –ip–inet 此引數的效果和指定」-A inet」引數相同。

4.使用範例

範例1:無引數使用

命令:

netstat

輸出:

[yiibai@localhost ~]$ netstat
Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State
tcp        0      0 localhost.localdoma:ssh 192.168.0.5:54835       ESTABLISHED
Active UNIX domain sockets (w/o servers)
Proto RefCnt Flags       Type       State         I-Node   Path
unix  2      [ ]         DGRAM                    13100    /run/systemd/shutdownd
unix  2      [ ]         DGRAM                    8385     /run/systemd/notify
unix  2      [ ]         DGRAM                    8387     /run/systemd/cgroups-agent
unix  5      [ ]         DGRAM                    8398     /run/systemd/journal/socket
unix  13     [ ]         DGRAM                    8400     /dev/log
unix  3      [ ]         STREAM     CONNECTED     19870
unix  3      [ ]         STREAM     CONNECTED     19850
unix  3      [ ]         STREAM     CONNECTED     19871
unix  3      [ ]         STREAM     CONNECTED     19830
unix  3      [ ]         STREAM     CONNECTED     16422
unix  3      [ ]         STREAM     CONNECTED     19852
unix  3      [ ]         STREAM     CONNECTED     16423    /var/run/dbus/system_bus_socket
unix  3      [ ]         STREAM     CONNECTED     19873
unix  3      [ ]         STREAM     CONNECTED     19874
unix  3      [ ]         STREAM     CONNECTED     15501
unix  3      [ ]         STREAM     CONNECTED     19847
unix  3      [ ]         STREAM     CONNECTED     19821
unix  3      [ ]         STREAM     CONNECTED     19849
unix  3      [ ]         STREAM     CONNECTED     19844
unix  3      [ ]         STREAM     CONNECTED     19820
unix  3      [ ]         STREAM     CONNECTED     19876
unix  3      [ ]         STREAM     CONNECTED     19833
unix  3      [ ]         STREAM     CONNECTED     19877
unix  2      [ ]         DGRAM                    16501
unix  3      [ ]         STREAM     CONNECTED     16576
unix  3      [ ]         STREAM     CONNECTED     19879
unix  3      [ ]         STREAM     CONNECTED     19829
unix  2      [ ]         DGRAM                    80277
unix  3      [ ]         STREAM     CONNECTED     19880
unix  3      [ ]         STREAM     CONNECTED     15803    /run/systemd/journal/stdout
unix  3      [ ]         STREAM     CONNECTED     19882
unix  3      [ ]         STREAM     CONNECTED     19263
unix  3      [ ]         STREAM     CONNECTED     19883
unix  3      [ ]         STREAM     CONNECTED     19853
unix  2      [ ]         DGRAM                    16412
unix  3      [ ]         STREAM     CONNECTED     19855
unix  3      [ ]         STREAM     CONNECTED     15802
unix  3      [ ]         STREAM     CONNECTED     19856
unix  3      [ ]         STREAM     CONNECTED     15502    /run/systemd/journal/stdout
unix  3      [ ]         STREAM     CONNECTED     80280
unix  3      [ ]         STREAM     CONNECTED     19858
unix  3      [ ]         STREAM     CONNECTED     19859
unix  3      [ ]         STREAM     CONNECTED     80281
unix  3      [ ]         STREAM     CONNECTED     19861
unix  3      [ ]         STREAM     CONNECTED     19843
unix  3      [ ]         STREAM     CONNECTED     19862
unix  3      [ ]         STREAM     CONNECTED     15457
unix  3      [ ]         STREAM     CONNECTED     19864
unix  3      [ ]         STREAM     CONNECTED     15613    /var/run/dbus/system_bus_socket
unix  3      [ ]         STREAM     CONNECTED     19865
unix  3      [ ]         STREAM     CONNECTED     19846
unix  2      [ ]         DGRAM                    80112
unix  3      [ ]         STREAM     CONNECTED     19867
unix  3      [ ]         STREAM     CONNECTED     16577    /var/run/dbus/system_bus_socket
unix  3      [ ]         STREAM     CONNECTED     19868
unix  2      [ ]         DGRAM                    15509
unix  3      [ ]         STREAM     CONNECTED     19264    /var/run/dbus/system_bus_socket
unix  3      [ ]         STREAM     CONNECTED     16129    /var/run/dbus/system_bus_socket
unix  3      [ ]         STREAM     CONNECTED     13393
unix  3      [ ]         STREAM     CONNECTED     19837
unix  2      [ ]         DGRAM                    13285
unix  3      [ ]         STREAM     CONNECTED     13530    /run/systemd/journal/stdout
unix  2      [ ]         DGRAM                    19784
unix  3      [ ]         STREAM     CONNECTED     16379    /run/systemd/journal/stdout
unix  3      [ ]         STREAM     CONNECTED     16160
unix  3      [ ]         STREAM     CONNECTED     13796    /run/systemd/journal/stdout
unix  3      [ ]         STREAM     CONNECTED     15732    /run/systemd/journal/stdout
unix  3      [ ]         STREAM     CONNECTED     15611
unix  3      [ ]         STREAM     CONNECTED     13777
unix  2      [ ]         DGRAM                    20011
unix  3      [ ]         STREAM     CONNECTED     16106    /run/systemd/journal/stdout
unix  3      [ ]         STREAM     CONNECTED     19840
......

說明:
從整體上看,netstat的輸出結果可以分為兩個部分:
一個是Active Internet connections,稱為有源TCP連線,其中」Recv-Q」和」Send-Q」指的是接收佇列和傳送佇列。這些數位一般都應該是0。如果不是則表示軟體包正在佇列中堆積。這種情況只能在非常少的情況見到。
另一個是Active UNIX domain sockets,稱為有源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

輸出:

[yiibai@localhost ~]$ netstat -a
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State
tcp        0      0 0.0.0.0:ssh             0.0.0.0:*               LISTEN
tcp        0      0 localhost:smtp          0.0.0.0:*               LISTEN
tcp        0     52 localhost.localdoma:ssh 192.168.0.5:54835       ESTABLISHED
tcp6       0      0 [::]:ssh                [::]:*                  LISTEN
tcp6       0      0 localhost:smtp          [::]:*                  LISTEN
udp        0      0 0.0.0.0:trnsprntproxy   0.0.0.0:*
udp        0      0 localhost:323           0.0.0.0:*
udp        0      0 0.0.0.0:bootpc          0.0.0.0:*
udp6       0      0 [::]:rnm                [::]:*
udp6       0      0 localhost:323           [::]:*
raw6       0      0 [::]:ipv6-icmp          [::]:*                  7
Active UNIX domain sockets (servers and established)
Proto RefCnt Flags       Type       State         I-Node   Path
unix  2      [ ACC ]     STREAM     LISTENING     12805    /run/lvm/lvmpolld.socket
unix  2      [ ACC ]     SEQPACKET  LISTENING     12811    /run/udev/control
unix  2      [ ACC ]     STREAM     LISTENING     12827    /run/lvm/lvmetad.socket
unix  2      [ ]         DGRAM                    13100    /run/systemd/shutdownd
unix  2      [ ACC ]     STREAM     LISTENING     19872    public/showq
unix  2      [ ACC ]     STREAM     LISTENING     19842    private/rewrite
unix  2      [ ACC ]     STREAM     LISTENING     15231    /var/run/dbus/system_bus_socket
unix  2      [ ACC ]     STREAM     LISTENING     19845    private/bounce
unix  2      [ ACC ]     STREAM     LISTENING     19848    private/defer
unix  2      [ ACC ]     STREAM     LISTENING     19851    private/trace
unix  2      [ ACC ]     STREAM     LISTENING     19854    private/verify
unix  2      [ ACC ]     STREAM     LISTENING     19860    private/proxymap
unix  2      [ ACC ]     STREAM     LISTENING     19863    private/proxywrite
unix  2      [ ACC ]     STREAM     LISTENING     19866    private/smtp
unix  2      [ ACC ]     STREAM     LISTENING     19869    private/relay
unix  2      [ ACC ]     STREAM     LISTENING     19875    private/error
unix  2      [ ACC ]     STREAM     LISTENING     19878    private/retry
unix  2      [ ACC ]     STREAM     LISTENING     19881    private/discard
unix  2      [ ACC ]     STREAM     LISTENING     19884    private/local
unix  2      [ ACC ]     STREAM     LISTENING     19887    private/virtual
unix  2      [ ACC ]     STREAM     LISTENING     19890    private/lmtp
unix  2      [ ACC ]     STREAM     LISTENING     19893    private/anvil
unix  2      [ ACC ]     STREAM     LISTENING     19896    private/scache
unix  2      [ ACC ]     STREAM     LISTENING     19822    public/pickup
unix  2      [ ACC ]     STREAM     LISTENING     19832    public/cleanup
unix  2      [ ACC ]     STREAM     LISTENING     19835    public/qmgr
unix  2      [ ACC ]     STREAM     LISTENING     19857    public/flush
unix  2      [ ACC ]     STREAM     LISTENING     17836    /var/run/NetworkManager/private-dhcp
unix  2      [ ]         DGRAM                    8385     /run/systemd/notify
unix  2      [ ]         DGRAM                    8387     /run/systemd/cgroups-agent
unix  2      [ ACC ]     STREAM     LISTENING     19839    private/tlsmgr
......

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

範例3:顯示當前UDP連線狀況

命令:

netstat -nu

輸出:

[yiibai@localhost ~]$ netstat -nu
Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State
[yiibai@localhost ~]$

範例4:顯示UDP埠號的使用情況

命令:

netstat -apu

輸出:

[yiibai@localhost ~]$ netstat -apu
(No info could be read for "-p": geteuid()=1000 but you should be root.)
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
udp        0      0 0.0.0.0:trnsprntproxy   0.0.0.0:*                           -
udp        0      0 localhost:323           0.0.0.0:*                           -
udp        0      0 0.0.0.0:bootpc          0.0.0.0:*                           -
udp6       0      0 [::]:rnm                [::]:*                              -
udp6       0      0 localhost:323           [::]:*                              -
[yiibai@localhost ~]$

範例5:顯示網絡卡列表

命令:

netstat -i

輸出:

[yiibai@localhost ~]$ netstat -i
Kernel Interface table
Iface      MTU    RX-OK RX-ERR RX-DRP RX-OVR    TX-OK TX-ERR TX-DRP TX-OVR Flg
ens33     1500    63831      0      0 0          6313      0      0      0 BMRU
lo       65536       19      0      0 0            19      0      0      0 LRU
[yiibai@localhost ~]$

範例6:顯示組播組的關係

命令:

netstat -g

輸出:

[yiibai@localhost ~]$ netstat -g
IPv6/IPv4 Group Memberships
Interface       RefCnt Group
--------------- ------ ---------------------
lo              1      all-systems.mcast.net
ens33           1      all-systems.mcast.net
lo              1      ff02::1
lo              1      ff01::1
ens33           1      ff02::1:ff35:dd8c
ens33           1      ff02::1
ens33           1      ff01::1
[yiibai@localhost ~]$

範例7:顯示網路統計資訊

命令:

netstat -s

輸出:

[yiibai@localhost ~]$ netstat -s
Ip:
    28607 total packets received
    0 forwarded
    0 incoming packets discarded
    4123 incoming packets delivered
    4143 requests sent out
    50 dropped because of missing route
Icmp:
    767 ICMP messages received
    402 input ICMP message failed.
    ICMP input histogram:
        destination unreachable: 152
        timeout in transit: 164
        echo requests: 402
        echo replies: 49
    467 ICMP messages sent
    0 ICMP messages failed
    ICMP output histogram:
        destination unreachable: 9
        echo request: 458
IcmpMsg:
        InType0: 49
        InType3: 152
        InType8: 402
        InType11: 164
        OutType3: 9
        OutType8: 458
Tcp:
    3 active connections openings
    3 passive connection openings
    0 failed connection attempts
    0 connection resets received
    1 connections established
    2880 segments received
    2548 segments send out
    0 segments retransmited
    0 bad segments received.
    0 resets sent
Udp:
    474 packets received
    2 packets to unknown port received.
    0 packet receive errors
    1160 packets sent
    0 receive buffer errors
    0 send buffer errors
UdpLite:
TcpExt:
    3 TCP sockets finished time wait in fast timer
    11 delayed acks sent
    1 delayed acks further delayed because of locked socket
    1 packets directly queued to recvmsg prequeue.
    1139 packet headers predicted
    441 acknowledgments not containing data payload received
    1356 predicted acknowledgments
    IPReversePathFilter: 1
    TCPRcvCoalesce: 69
    TCPOrigDataSent: 2427
IpExt:
    InNoRoutes: 5
    InMcastPkts: 435
    InBcastPkts: 24444
    OutBcastPkts: 402
    InOctets: 2452235
    OutOctets: 539407
    InMcastOctets: 13920
    InBcastOctets: 2027384
    OutBcastOctets: 33768
    InNoECTPkts: 28642
[yiibai@localhost ~]$

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

範例8:顯示監聽的套介面

命令:

netstat -l

輸出:

[yiibai@localhost ~]$ netstat -l
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State
tcp        0      0 0.0.0.0:ssh             0.0.0.0:*               LISTEN
tcp        0      0 localhost:smtp          0.0.0.0:*               LISTEN
tcp6       0      0 [::]:ssh                [::]:*                  LISTEN
tcp6       0      0 localhost:smtp          [::]:*                  LISTEN
udp        0      0 0.0.0.0:trnsprntproxy   0.0.0.0:*
udp        0      0 localhost:323           0.0.0.0:*
udp        0      0 0.0.0.0:bootpc          0.0.0.0:*
udp6       0      0 [::]:rnm                [::]:*
udp6       0      0 localhost:323           [::]:*
raw6       0      0 [::]:ipv6-icmp          [::]:*                  7
Active UNIX domain sockets (only servers)
Proto RefCnt Flags       Type       State         I-Node   Path
unix  2      [ ACC ]     STREAM     LISTENING     12805    /run/lvm/lvmpolld.socket
unix  2      [ ACC ]     SEQPACKET  LISTENING     12811    /run/udev/control
unix  2      [ ACC ]     STREAM     LISTENING     12827    /run/lvm/lvmetad.socket
unix  2      [ ACC ]     STREAM     LISTENING     19872    public/showq
unix  2      [ ACC ]     STREAM     LISTENING     19842    private/rewrite
unix  2      [ ACC ]     STREAM     LISTENING     15231    /var/run/dbus/system_bus_socket
unix  2      [ ACC ]     STREAM     LISTENING     19845    private/bounce
unix  2      [ ACC ]     STREAM     LISTENING     19848    private/defer
unix  2      [ ACC ]     STREAM     LISTENING     19851    private/trace
unix  2      [ ACC ]     STREAM     LISTENING     19854    private/verify
unix  2      [ ACC ]     STREAM     LISTENING     19860    private/proxymap
unix  2      [ ACC ]     STREAM     LISTENING     19863    private/proxywrite
unix  2      [ ACC ]     STREAM     LISTENING     19866    private/smtp
unix  2      [ ACC ]     STREAM     LISTENING     19869    private/relay
unix  2      [ ACC ]     STREAM     LISTENING     19875    private/error
unix  2      [ ACC ]     STREAM     LISTENING     19878    private/retry
unix  2      [ ACC ]     STREAM     LISTENING     19881    private/discard
unix  2      [ ACC ]     STREAM     LISTENING     19884    private/local
unix  2      [ ACC ]     STREAM     LISTENING     19887    private/virtual
unix  2      [ ACC ]     STREAM     LISTENING     19890    private/lmtp
unix  2      [ ACC ]     STREAM     LISTENING     19893    private/anvil
unix  2      [ ACC ]     STREAM     LISTENING     19896    private/scache
unix  2      [ ACC ]     STREAM     LISTENING     19822    public/pickup
unix  2      [ ACC ]     STREAM     LISTENING     19832    public/cleanup
unix  2      [ ACC ]     STREAM     LISTENING     19835    public/qmgr
unix  2      [ ACC ]     STREAM     LISTENING     19857    public/flush
unix  2      [ ACC ]     STREAM     LISTENING     17836    /var/run/NetworkManager/private-dhcp
unix  2      [ ACC ]     STREAM     LISTENING     19839    private/tlsmgr
unix  2      [ ACC ]     STREAM     LISTENING     8395     /run/systemd/journal/stdout
unix  2      [ ACC ]     STREAM     LISTENING     12794    /run/systemd/private
[yiibai@localhost ~]$

範例9:顯示所有已建立的有效連線

命令:

netstat -n

輸出:

[yiibai@localhost ~]$ netstat -n
Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State
tcp        0     52 192.168.0.197:22        192.168.0.5:54835       ESTABLISHED
Active UNIX domain sockets (w/o servers)
Proto RefCnt Flags       Type       State         I-Node   Path
unix  2      [ ]         DGRAM                    13100    /run/systemd/shutdownd
unix  2      [ ]         DGRAM                    8385     /run/systemd/notify
unix  2      [ ]         DGRAM                    8387     /run/systemd/cgroups-agent
unix  5      [ ]         DGRAM                    8398     /run/systemd/journal/socket
unix  13     [ ]         DGRAM                    8400     /dev/log
unix  3      [ ]         STREAM     CONNECTED     19870
unix  3      [ ]         STREAM     CONNECTED     19850
unix  3      [ ]         STREAM     CONNECTED     19871
unix  3      [ ]         STREAM     CONNECTED     19830
unix  3      [ ]         STREAM     CONNECTED     16422
unix  3      [ ]         STREAM     CONNECTED     19852
unix  3      [ ]         STREAM     CONNECTED     16423    /var/run/dbus/system_bus_socket
unix  3      [ ]         STREAM     CONNECTED     19873
unix  3      [ ]         STREAM     CONNECTED     19874
unix  3      [ ]         STREAM     CONNECTED     15501
unix  3      [ ]         STREAM     CONNECTED     19847
unix  3      [ ]         STREAM     CONNECTED     19821
unix  3      [ ]         STREAM     CONNECTED     19849
unix  3      [ ]         STREAM     CONNECTED     19844
unix  3      [ ]         STREAM     CONNECTED     19820
unix  3      [ ]         STREAM     CONNECTED     19876
unix  3      [ ]         STREAM     CONNECTED     19833
unix  3      [ ]         STREAM     CONNECTED     19877
unix  2      [ ]         DGRAM                    16501
unix  3      [ ]         STREAM     CONNECTED     16576
unix  3      [ ]         STREAM     CONNECTED     19879
unix  3      [ ]         STREAM     CONNECTED     19829
unix  2      [ ]         DGRAM                    80277
unix  3      [ ]         STREAM     CONNECTED     19880
unix  3      [ ]         STREAM     CONNECTED     15803    /run/systemd/journal/stdout
unix  3      [ ]         STREAM     CONNECTED     19882
unix  3      [ ]         STREAM     CONNECTED     19263
unix  3      [ ]         STREAM     CONNECTED     19883
unix  3      [ ]         STREAM     CONNECTED     19853
unix  2      [ ]         DGRAM                    16412
unix  3      [ ]         STREAM     CONNECTED     19855
unix  3      [ ]         STREAM     CONNECTED     15802
unix  3      [ ]         STREAM     CONNECTED     19856
unix  3      [ ]         STREAM     CONNECTED     15502    /run/systemd/journal/stdout
unix  3      [ ]         STREAM     CONNECTED     80280
unix  3      [ ]         STREAM     CONNECTED     19858
unix  3      [ ]         STREAM     CONNECTED     19859
unix  3      [ ]         STREAM     CONNECTED     80281
unix  3      [ ]         STREAM     CONNECTED     19861
unix  3      [ ]         STREAM     CONNECTED     19843
unix  3      [ ]         STREAM     CONNECTED     19862
unix  3      [ ]         STREAM     CONNECTED     15457
unix  3      [ ]         STREAM     CONNECTED     19864
unix  3      [ ]         STREAM     CONNECTED     15613    /var/run/dbus/system_bus_socket
unix  3      [ ]         STREAM     CONNECTED     19865
unix  3      [ ]         STREAM     CONNECTED     19846
unix  2      [ ]         DGRAM                    80112
unix  3      [ ]         STREAM     CONNECTED     19867
unix  3      [ ]         STREAM     CONNECTED     16577    /var/run/dbus/system_bus_socket
unix  3      [ ]         STREAM     CONNECTED     19868
unix  2      [ ]         DGRAM                    15509
unix  3      [ ]         STREAM     CONNECTED     19264    /var/run/dbus/system_bus_socket
unix  3      [ ]         STREAM     CONNECTED     16129    /var/run/dbus/system_bus_socket
unix  3      [ ]         STREAM     CONNECTED     13393
unix  3      [ ]         STREAM     CONNECTED     19837
unix  2      [ ]         DGRAM                    13285
unix  3      [ ]         STREAM     CONNECTED     13530    /run/systemd/journal/stdout
unix  2      [ ]         DGRAM                    19784
unix  3      [ ]         STREAM     CONNECTED     16379    /run/systemd/journal/stdout
unix  3      [ ]         STREAM     CONNECTED     16160
unix  3      [ ]         STREAM     CONNECTED     13796    /run/systemd/journal/stdout
unix  3      [ ]         STREAM     CONNECTED     15732    /run/systemd/journal/stdout
unix  3      [ ]         STREAM     CONNECTED     15611
unix  3      [ ]         STREAM     CONNECTED     13777
unix  2      [ ]         DGRAM                    20011
unix  3      [ ]         STREAM     CONNECTED     16106    /run/systemd/journal/stdout
unix  3      [ ]         STREAM     CONNECTED     19840
unix  2      [ ]         DGRAM                    16231
unix  3      [ ]         STREAM     CONNECTED     16105
unix  3      [ ]         STREAM     CONNECTED     16320
unix  3      [ ]         STREAM     CONNECTED     19885
unix  3      [ ]         DGRAM                    13816
unix  3      [ ]         STREAM     CONNECTED     18433    /run/systemd/journal/stdout
unix  3      [ ]         STREAM     CONNECTED     16321    /var/run/dbus/system_bus_socket
unix  3      [ ]         STREAM     CONNECTED     19886
unix  3      [ ]         STREAM     CONNECTED     16161    /var/run/dbus/system_bus_socket
unix  3      [ ]         STREAM     CONNECTED     16378
unix  3      [ ]         STREAM     CONNECTED     19888
unix  3      [ ]         STREAM     CONNECTED     15612
unix  3      [ ]         STREAM     CONNECTED     18432
unix  3      [ ]         STREAM     CONNECTED     19889
unix  2      [ ]         DGRAM                    16204
unix  3      [ ]         STREAM     CONNECTED     15731
unix  3      [ ]         STREAM     CONNECTED     19834
unix  3      [ ]         STREAM     CONNECTED     19891
unix  3      [ ]         STREAM     CONNECTED     19841
unix  3      [ ]         STREAM     CONNECTED     19836
unix  2      [ ]         DGRAM                    79889
unix  3      [ ]         STREAM     CONNECTED     19892
unix  2      [ ]         DGRAM                    16133
unix  3      [ ]         STREAM     CONNECTED     15152
unix  3      [ ]         STREAM     CONNECTED     16128
unix  3      [ ]         STREAM     CONNECTED     19894
unix  3      [ ]         STREAM     CONNECTED     19895
unix  3      [ ]         STREAM     CONNECTED     15153
unix  3      [ ]         STREAM     CONNECTED     19897
unix  3      [ ]         STREAM     CONNECTED     19898
unix  3      [ ]         DGRAM                    13817
unix  2      [ ]         DGRAM                    13797
unix  2      [ ]         DGRAM                    15143
[yiibai@localhost ~]$

範例10:顯示關於乙太網的統計資料

命令:

netstat -e

輸出:

[yiibai@localhost ~]$ netstat -e
Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       User       Inode
tcp        0     52 localhost.localdoma:ssh 192.168.0.5:54835       ESTABLISHED root       80161
Active UNIX domain sockets (w/o servers)
Proto RefCnt Flags       Type       State         I-Node   Path
unix  2      [ ]         DGRAM                    13100    /run/systemd/shutdownd
unix  2      [ ]         DGRAM                    8385     /run/systemd/notify
unix  2      [ ]         DGRAM                    8387     /run/systemd/cgroups-agent
unix  5      [ ]         DGRAM                    8398     /run/systemd/journal/socket
unix  13     [ ]         DGRAM                    8400     /dev/log
unix  3      [ ]         STREAM     CONNECTED     19870
unix  3      [ ]         STREAM     CONNECTED     19850
unix  3      [ ]         STREAM     CONNECTED     19871
unix  3      [ ]         STREAM     CONNECTED     19830
unix  3      [ ]         STREAM     CONNECTED     16422
unix  3      [ ]         STREAM     CONNECTED     19852
unix  3      [ ]         STREAM     CONNECTED     16423    /var/run/dbus/system_bus_socket
unix  3      [ ]         STREAM     CONNECTED     19873
unix  3      [ ]         STREAM     CONNECTED     19874
unix  3      [ ]         STREAM     CONNECTED     15501
unix  3      [ ]         STREAM     CONNECTED     19847
unix  3      [ ]         STREAM     CONNECTED     19821
unix  3      [ ]         STREAM     CONNECTED     19849
unix  3      [ ]         STREAM     CONNECTED     19844
unix  3      [ ]         STREAM     CONNECTED     19820
unix  3      [ ]         STREAM     CONNECTED     19876
unix  3      [ ]         STREAM     CONNECTED     19833
unix  3      [ ]         STREAM     CONNECTED     19877
unix  2      [ ]         DGRAM                    16501
unix  3      [ ]         STREAM     CONNECTED     16576
unix  3      [ ]         STREAM     CONNECTED     19879
unix  3      [ ]         STREAM     CONNECTED     19829
unix  2      [ ]         DGRAM                    80277
unix  3      [ ]         STREAM     CONNECTED     19880
unix  3      [ ]         STREAM     CONNECTED     15803    /run/systemd/journal/stdout
unix  3      [ ]         STREAM     CONNECTED     19882
unix  3      [ ]         STREAM     CONNECTED     19263
unix  3      [ ]         STREAM     CONNECTED     19883
unix  3      [ ]         STREAM     CONNECTED     19853
unix  2      [ ]         DGRAM                    16412
unix  3      [ ]         STREAM     CONNECTED     19855
unix  3      [ ]         STREAM     CONNECTED     15802
unix  3      [ ]         STREAM     CONNECTED     19856
unix  3      [ ]         STREAM     CONNECTED     15502    /run/systemd/journal/stdout
unix  3      [ ]         STREAM     CONNECTED     80280
unix  3      [ ]         STREAM     CONNECTED     19858
unix  3      [ ]         STREAM     CONNECTED     19859
unix  3      [ ]         STREAM     CONNECTED     80281
unix  3      [ ]         STREAM     CONNECTED     19861
unix  3      [ ]         STREAM     CONNECTED     19843
unix  3      [ ]         STREAM     CONNECTED     19862
unix  3      [ ]         STREAM     CONNECTED     15457
unix  3      [ ]         STREAM     CONNECTED     19864
unix  3      [ ]         STREAM     CONNECTED     15613    /var/run/dbus/system_bus_socket
unix  3      [ ]         STREAM     CONNECTED     19865
unix  3      [ ]         STREAM     CONNECTED     19846
unix  2      [ ]         DGRAM                    80112
unix  3      [ ]         STREAM     CONNECTED     19867
unix  3      [ ]         STREAM     CONNECTED     16577    /var/run/dbus/system_bus_socket
unix  3      [ ]         STREAM     CONNECTED     19868
unix  2      [ ]         DGRAM                    15509
unix  3      [ ]         STREAM     CONNECTED     19264    /var/run/dbus/system_bus_socket
unix  3      [ ]         STREAM     CONNECTED     16129    /var/run/dbus/system_bus_socket
unix  3      [ ]         STREAM     CONNECTED     13393
unix  3      [ ]         STREAM     CONNECTED     19837
unix  2      [ ]         DGRAM                    13285
unix  3      [ ]         STREAM     CONNECTED     13530    /run/systemd/journal/stdout
unix  2      [ ]         DGRAM                    19784
unix  3      [ ]         STREAM     CONNECTED     16379    /run/systemd/journal/stdout
unix  3      [ ]         STREAM     CONNECTED     16160
unix  3      [ ]         STREAM     CONNECTED     13796    /run/systemd/journal/stdout
unix  3      [ ]         STREAM     CONNECTED     15732    /run/systemd/journal/stdout
unix  3      [ ]         STREAM     CONNECTED     15611
unix  3      [ ]         STREAM     CONNECTED     13777
unix  2      [ ]         DGRAM                    20011
unix  3      [ ]         STREAM     CONNECTED     16106    /run/systemd/journal/stdout
unix  3      [ ]         STREAM     CONNECTED     19840
unix  2      [ ]         DGRAM                    16231
unix  3      [ ]         STREAM     CONNECTED     16105
unix  3      [ ]         STREAM     CONNECTED     16320
unix  3      [ ]         STREAM     CONNECTED     19885
unix  3      [ ]         DGRAM                    13816
unix  3      [ ]         STREAM     CONNECTED     18433    /run/systemd/journal/stdout
unix  3      [ ]         STREAM     CONNECTED     16321    /var/run/dbus/system_bus_socket
unix  3      [ ]         STREAM     CONNECTED     19886
unix  3      [ ]         STREAM     CONNECTED     16161    /var/run/dbus/system_bus_socket
unix  3      [ ]         STREAM     CONNECTED     16378
unix  3      [ ]         STREAM     CONNECTED     19888
unix  3      [ ]         STREAM     CONNECTED     15612
unix  3      [ ]         STREAM     CONNECTED     18432
unix  3      [ ]         STREAM     CONNECTED     19889
unix  2      [ ]         DGRAM                    16204
unix  3      [ ]         STREAM     CONNECTED     15731
unix  3      [ ]         STREAM     CONNECTED     19834
unix  3      [ ]         STREAM     CONNECTED     19891
unix  3      [ ]         STREAM     CONNECTED     19841
unix  3      [ ]         STREAM     CONNECTED     19836
unix  2      [ ]         DGRAM                    79889
unix  3      [ ]         STREAM     CONNECTED     19892
unix  2      [ ]         DGRAM                    16133
unix  3      [ ]         STREAM     CONNECTED     15152
unix  3      [ ]         STREAM     CONNECTED     16128
unix  3      [ ]         STREAM     CONNECTED     19894
unix  3      [ ]         STREAM     CONNECTED     19895
unix  3      [ ]         STREAM     CONNECTED     15153
unix  3      [ ]         STREAM     CONNECTED     19897
unix  3      [ ]         STREAM     CONNECTED     19898
unix  3      [ ]         DGRAM                    13817
unix  2      [ ]         DGRAM                    13797
unix  2      [ ]         DGRAM                    15143
[yiibai@localhost ~]$

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

範例11:顯示關於路由表的資訊

命令:

netstat -r

輸出:

[yiibai@localhost ~]$ netstat -r
Kernel IP routing table
Destination     Gateway         Genmask         Flags   MSS Window  irtt Iface
default         gateway         0.0.0.0         UG        0 0          0 ens33
192.168.0.0     0.0.0.0         255.255.255.0   U         0 0          0 ens33
[yiibai@localhost ~]$

範例12:列出所有 tcp 埠

命令:

netstat -at

輸出:

[yiibai@localhost ~]$ netstat -at
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State
tcp        0      0 0.0.0.0:ssh             0.0.0.0:*               LISTEN
tcp        0      0 localhost:smtp          0.0.0.0:*               LISTEN
tcp        0     52 localhost.localdoma:ssh 192.168.0.5:54835       ESTABLISHED
tcp6       0      0 [::]:ssh                [::]:*                  LISTEN
tcp6       0      0 localhost:smtp          [::]:*                  LISTEN
[yiibai@localhost ~]$

範例13:統計機器中網路連線各個狀態個數

命令:

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

輸出:

[yiibai@localhost ~]$ netstat -a | awk '/^tcp/ {++S[$NF]} END {for(a in S) print a, S[a]}'
LISTEN 4
ESTABLISHED 1
[yiibai@localhost ~]$

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

命令:

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

輸出:

範例15:檢視連線某伺服器端口最多的的IP地址

命令:

netstat -nat | grep "192.168.120.20:16067" |awk '{print $5}'|awk -F: '{print $4}'|sort|uniq -c|sort -nr|head -20

輸出:

[yiibai@localhost ~]$ netstat -nat |awk '{print $6}'|sort|uniq -c
      1 established)
      1 ESTABLISHED
      1 Foreign
      4 LISTEN
[yiibai@localhost ~]$

範例16:找出程式執行的埠

命令:

netstat -ap | grep ssh

輸出:

[yiibai@localhost ~]$ netstat -ap | grep ssh
(No info could be read for "-p": geteuid()=1000 but you should be root.)
tcp        0      0 0.0.0.0:ssh             0.0.0.0:*               LISTEN      -
tcp        0     52 localhost.localdoma:ssh 192.168.0.5:54835       ESTABLISHED -
tcp6       0      0 [::]:ssh                [::]:*                  LISTEN      -
[yiibai@localhost ~]$

範例17:在 netstat 輸出中顯示 PID 和進程名稱

命令:

netstat -pt

輸出:

[yiibai@localhost ~]$ netstat -pt
(No info could be read for "-p": geteuid()=1000 but you should be root.)
Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0     52 localhost.localdoma:ssh 192.168.0.5:54835       ESTABLISHED -
[yiibai@localhost ~]$

說明:
netstat -p可以與其它開關一起使用,就可以新增 「PID/進程名稱」 到 netstat 輸出中,這樣 debugging 的時候可以很方便的發現特定埠執行的程式。

範例18:找出執行在指定埠的進程

命令:

netstat -anpt | grep ':16160'

輸出:

[root@localhost yiibai]# netstat -anpt | grep ':16160'
[root@localhost yiibai]# netstat -anpt | grep ':22'
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1032/sshd
tcp        0     52 192.168.0.197:22        192.168.0.5:54835       ESTABLISHED 4039/sshd: yiibai [
tcp6       0      0 :::22                   :::*                    LISTEN      1032/sshd
[root@localhost yiibai]#

說明:
執行在埠22的進程id1032,再通過ps命令就可以找到具體的應用程式了。