Linux netstat命令詳解


 

簡介

Netstat 命令用於顯示各種網絡相關信息,如網絡連接,路由表,接口狀態 (Interface Statistics),masquerade 連接,多播成員 (Multicast Memberships) 等等。

輸出信息含義

執行netstat后,其輸出結果為

[vagrant@centos6 ~]$ netstat|more
Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address               Foreign Address             State
tcp        0      0 10.0.2.15:ssh               10.0.2.2:52091              ESTABLISHED
Active UNIX domain sockets (w/o servers)
Proto RefCnt Flags       Type       State         I-Node Path
unix  11     [ ]         DGRAM                    11021  /dev/log
unix  2      [ ]         DGRAM                    11556  @/org/freedesktop/hal/udev_event
unix  2      [ ]         DGRAM                    8986   @/org/kernel/udev/udevd
unix  3      [ ]         STREAM     CONNECTED     17777
unix  3      [ ]         STREAM     CONNECTED     17776

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

OUTPUT
   Active Internet connections (TCP, UDP, raw)
   Proto
       The protocol (tcp, udp, raw) used by the socket.

   Recv-Q
       The count of bytes not copied by the user program connected to this socket.

   Send-Q
       The count of bytes not acknowledged by the remote host.

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

Active UNIX domain Sockets
   Proto
       The protocol (usually unix) used by the socket.

   RefCnt
       The reference count (i.e. attached processes via this socket).

   Flags
       The  flags  displayed  is  SO_ACCEPTON  (displayed as ACC), SO_WAITDATA (W) or SO_NOSPACE (N).  SO_ACCECPTON is used on
       unconnected sockets if their corresponding processes are waiting for a connect request. The other flags are not of nor-
       mal interest.

   Type
       There are several types of socket access:

       SOCK_DGRAM
              The socket is used in Datagram (connectionless) mode.

       SOCK_STREAM
              This is a stream (connection) socket.

       SOCK_RAW
              The socket is used as a raw socket.

另一個是Active UNIX domain sockets,稱為有源Unix域套接口(和網絡套接字一樣,但是只能用於本機通信,性能可以提高一倍)。

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

常見參數

-a (all)顯示所有選項,netstat默認不顯示LISTEN相關 -t (tcp)僅顯示tcp相關選項
-u (udp)僅顯示udp相關選項
-n 拒絕顯示別名,能顯示數字的全部轉化成數字。(重要)
-l 僅列出有在 Listen (監聽) 的服務狀態

-p 顯示建立相關鏈接的程序名(macOS中表示協議 -p protocol)
-r 顯示路由信息,路由表
-e 顯示擴展信息,例如uid等
-s 按各個協議進行統計 (重要)
-c 每隔一個固定時間,執行該netstat命令。

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

實用命令實例

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

列出所有端口:     netstat -a
列出所有tcp端口:  netstat -at
列出所有udp端口:  netstat -au

2. 列出所有處於監聽狀態的 Sockets

只顯示監聽端口:          netstat -l
只列出所有監聽tcp端口:   netstat -lt
只列出所有監聽udp端口:   netstat -lu
只列出所有監聽UNIX端口:  netstat -lx

3. 顯示每個協議的統計信息

顯示所有端口的統計信息 netstat -s

[vagrant@centos6 ~]$ netstat -s
Ip:
total packets received
with invalid addresses
forwarded
incoming packets discarded
incoming packets delivered
requests sent out
Icmp:
ICMP messages received
input ICMP message failed.
    ICMP input histogram:
ICMP messages sent
ICMP messages failed
    ICMP output histogram:
Tcp:
active connections openings
passive connection openings
failed connection attempts
connection resets received
connections established
segments received
segments send out
segments retransmited
bad segments received.
resets sent
Udp:
packets received
packets to unknown port received.
packet receive errors
packets sent
UdpLite:
TcpExt:
delayed acks sent
packets directly queued to recvmsg prequeue.
packets header predicted
acknowledgments not containing data received
predicted acknowledgments
TCP data loss events
IpExt:
    InOctets: 146500
    OutOctets: 132340

 

顯示 TCP 或 UDP 端口的統計信息 netstat -st 或 -su

# netstat -st 
# netstat -su

4. 顯示 PID 和進程名稱

netstat -p 可以與其它開關一起使用,就可以添加 “PID/進程名稱” 到 netstat 輸出中,這樣 debugging 的時候可以很方便的發現特定端口運行的程序。

[root@centos6 ~]# netstat -pt
Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address               Foreign Address             State       PID/Program name
tcp        0      0 10.0.2.15:ssh               10.0.2.2:52964              ESTABLISHED 3008/sshd
tcp        0      0 10.0.2.15:ssh               10.0.2.2:52091              ESTABLISHED 2813/sshd

5. 不顯示主機,端口和用戶名 (host, port or user)

當你不想讓主機,端口和用戶名顯示,使用 netstat -n。將會使用數字代替那些名稱。該參數可以加速output的輸出,因為不用進行比對查詢。

# netstat -an

# 如果只是不想讓這三個名稱中的一個被顯示,使用以下命令
# netsat -a --numeric-ports
# netsat -a --numeric-hosts
# netsat -a --numeric-users

 

6. 持續輸出 netstat 信息

netstat 將每隔一秒輸出網絡信息。

# netstat -t -c 2

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

netstat --verbose

8. 顯示核心路由信息

[root@centos6 ~]# netstat -rn
Kernel IP routing table
Destination     Gateway         Genmask         Flags   MSS Window  irtt Iface
10.0.2.0        0.0.0.0         255.255.255.0   U         0 0          0 eth0
169.254.0.0     0.0.0.0         255.255.0.0     U         0 0          0 eth0
0.0.0.0         10.0.2.2        0.0.0.0         UG        0 0          0 eth0

# 注意: 使用 netstat -rn 顯示數字格式,不查詢主機名稱,效果等價於route -n

9. 找出程序運行的端口

並不是所有的進程都能找到,沒有權限的會不顯示,使用 root 權限查看所有的信息。

[root@centos6 ~]# netstat -apn | grep ssh
tcp        0      0 0.0.0.0:22                  0.0.0.0:*                   LISTEN      1438/sshd
tcp        0      0 10.0.2.15:22                10.0.2.2:52964              ESTABLISHED 3008/sshd
tcp        0      0 10.0.2.15:22                10.0.2.2:52091              ESTABLISHED 2813/sshd
tcp        0      0 :::22                       :::*                        LISTEN      1438/sshd
unix  3      [ ]         STREAM     CONNECTED     18443  3008/sshd
unix  3      [ ]         STREAM     CONNECTED     18442  3011/sshd
unix  2      [ ]         DGRAM                    18439  3008/sshd
unix  3      [ ]         STREAM     CONNECTED     17777  2813/sshd
unix  3      [ ]         STREAM     CONNECTED     17776  2816/sshd
unix  2      [ ]         DGRAM                    17773  2813/sshd

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

[root@centos6 ~]# netstat -an | grep ':22'
tcp        0      0 0.0.0.0:22                  0.0.0.0:*                   LISTEN
tcp        0      0 10.0.2.15:22                10.0.2.2:52964              ESTABLISHED
tcp        0      0 10.0.2.15:22                10.0.2.2:52091              ESTABLISHED
tcp        0      0 :::22                       :::*                        LISTEN

10. 顯示網絡接口列表

[root@centos6 ~]# 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   0    12125      0      0      0     5474      0      0      0 BMRU
lo        65536   0        0      0      0      0        0      0      0      0 LRU

[root@centos6 ~]# netstat -ie
Kernel Interface table
eth0      Link encap:Ethernet  HWaddr 52:54:00:5D:A4:AF
          inet addr:10.0.2.15  Bcast:10.0.2.255  Mask:255.255.255.0
          inet6 addr: fe80::5054:ff:fe5d:a4af/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:12131 errors:0 dropped:0 overruns:0 frame:0
          TX packets:5477 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:7474852 (7.1 MiB)  TX bytes:580343 (566.7 KiB)

lo        Link encap:Local Loopback
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:0 (0.0 b)  TX bytes:0 (0.0 b)

 

11. IP和TCP分析

查看連接某服務端口最多的的IP地址/TCP各種狀態列表:

http://www.cnblogs.com/echo1937/p/6646208.html

 

 

轉 : https://www.cnblogs.com/echo1937/p/6677325.html

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM