Netstat Commands for Linux



netstat的是最基礎的網絡服務調試工具之一,告訴你哪些端口是開放的,任何程序是否監聽端口。
這個工具是非常重要的,非常有用的Linux網絡管理員以及系統管理員監控和解決網絡相關的問題,並確定網絡流量性能。


1.列出TCP和UDP連接的所有偵聽端口

使用 netstat -a 選項列出所有端口( TCP  和UDP )
[root@www ~]# netstat -a | more
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address               Foreign Address             State
tcp        0      0 localhost:smtp              *:*                         LISTEN
tcp        0      0 *:mysql                     *:*                         LISTEN
tcp        0      0 *:ftp                       *:*                         LISTEN
tcp        0      0 *:ssh                       *:*                         LISTEN
tcp        0     64 www.Chi.com:ssh             192.168.64.1:51304          ESTABLISHED
tcp        0      0 localhost:smtp              *:*                         LISTEN
tcp        0      0 *:http                      *:*                         LISTEN
tcp        0      0 *:ssh                       *:*                         LISTEN
udp        0      0 *:bootpc                    *:*
Active UNIX domain sockets (servers and established)
Proto RefCnt Flags       Type       State         I-Node Path
unix  2      [ ]         DGRAM                    11115  /var/run/portreserve/socket
unix  2      [ ACC ]     STREAM     LISTENING     11829  /var/lib/mysql/mysql.sock
unix  2      [ ACC ]     STREAM     LISTENING     12046  public/cleanup
unix  2      [ ACC ]     STREAM     LISTENING     12053  private/tlsmgr
unix  2      [ ACC ]     STREAM     LISTENING     8744   @/com/ubuntu/upstart
unix  2      [ ACC ]     STREAM     LISTENING     12057  private/rewrite
Listing only TCP (Transmission Control Protocol) port connections using netstat -at.
Listing only UDP (User Datagram Protocol ) port connections using netstat -au.

2.列出所有LISTENING連接

使用 netstat -l 列出所有活動偵聽端口連接
[root@www ~]# netstat -l
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address               Foreign Address             State
tcp        0      0 localhost:smtp              *:*                         LISTEN
tcp        0      0 *:mysql                     *:*                         LISTEN
tcp        0      0 *:ftp                       *:*                         LISTEN
tcp        0      0 *:ssh                       *:*                         LISTEN
tcp        0      0 localhost:smtp              *:*                         LISTEN
tcp        0      0 *:http                      *:*                         LISTEN
tcp        0      0 *:ssh                       *:*                         LISTEN
udp        0      0 *:bootpc                    *:*
Active UNIX domain sockets (only servers)
Proto RefCnt Flags       Type       State         I-Node Path
unix  2      [ ACC ]     STREAM     LISTENING     11829  /var/lib/mysql/mysql.sock
unix  2      [ ACC ]     STREAM     LISTENING     12046  public/cleanup
unix  2      [ ACC ]     STREAM     LISTENING     12053  private/tlsmgr
unix  2      [ ACC ]     STREAM     LISTENING     8744   @/com/ubuntu/upstart
unix  2      [ ACC ]     STREAM     LISTENING     12057  private/rewrite
unix  2      [ ACC ]     STREAM     LISTENING     12061  private/bounce
unix  2      [ ACC ]     STREAM     LISTENING     12065  private/defer
Listing all active listening TCP ports by using option netstat -lt.
Listing all active listening UDP ports by using option netstat -lu.

3.藉由Protocol來顯示統計

[root@www ~]# netstat -s
Ip:
    338 total packets received
    3 with invalid addresses
    0 forwarded
    0 incoming packets discarded
    305 incoming packets delivered
    236 requests sent out
Icmp:
    0 ICMP messages received
    0 input ICMP message failed.
    ICMP input histogram:
    0 ICMP messages sent
    0 ICMP messages failed
    ICMP output histogram:
Tcp:
    0 active connections openings
    1 passive connection openings
    0 failed connection attempts
    0 connection resets received
    1 connections established
    302 segments received
    233 segments send out
    0 segments retransmited
    0 bad segments received.
    0 resets sent
Udp:
    3 packets received
    0 packets to unknown port received.
    0 packet receive errors
    3 packets sent
UdpLite:
TcpExt:
    3 delayed acks sent
    97 packets header predicted
    49 acknowledgments not containing data received
    107 predicted acknowledgments
    0 TCP data loss events
IpExt:
    InBcastPkts: 29
    InOctets: 30959
    OutOctets: 40254
    InBcastOctets: 2571
[root@www ~]# netstat -s
Ip:
    346 total packets received
    3 with invalid addresses
    0 forwarded
    0 incoming packets discarded
    313 incoming packets delivered
    242 requests sent out
Icmp:
    0 ICMP messages received
    0 input ICMP message failed.
    ICMP input histogram:
    0 ICMP messages sent
    0 ICMP messages failed
    ICMP output histogram:
Tcp:
    0 active connections openings
    1 passive connection openings
    0 failed connection attempts
    0 connection resets received
    1 connections established
    309 segments received
    238 segments send out
    0 segments retransmited
    0 bad segments received.
    0 resets sent
Udp:
    4 packets received
    0 packets to unknown port received.
    0 packet receive errors
    4 packets sent
UdpLite:
TcpExt:
    3 delayed acks sent
    99 packets header predicted
    51 acknowledgments not containing data received
    108 predicted acknowledgments
    0 TCP data loss events
IpExt:
    InBcastPkts: 29
    InOctets: 31873
    OutOctets: 42126
    InBcastOctets: 2571
Showing statistics of only TCP protocol by using option netstat -st.
Showing statistics of only UDP protocol by using option netstat -su.

4.顯示帶PID的Service name

顯示 PID/Program Name
[root@www ~]# netstat -tp
Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address               Foreign Address             State       PID/Program name      
tcp        0      0 www.Chi.com:ssh             192.168.64.1:51304          ESTABLISHED 1618/sshd             

5.顯示Kernel IP routing

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

6.顯示Network Interface Transactions

顯示 network interface packet transactions
包含 transferring and receiving packets with MTU size

[root@www ~]# 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      862      0      0      0      313      0      0      0 BMRU
lo        65536   0        0      0      0      0        0      0      0      0 LRU

7.顯示 Kernel Interface Table

相似於 ifconfig
[root@www ~]# netstat -ie
Kernel Interface table
eth0      Link encap:Ethernet  HWaddr 00:0C:29:A0:EB:28
          inet addr:192.168.64.148  Bcast:192.168.64.255  Mask:255.255.255.0
          inet6 addr: fe80::20c:29ff:fea0:eb28/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:920 errors:0 dropped:0 overruns:0 frame:0
          TX packets:318 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:73794 (72.0 KiB)  TX bytes:55028 (53.7 KiB)
          Interrupt:19 Base address:0x2000

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)

8.顯示 IPv 4/6 資訊

Displays multicast(多點傳送) group membership information
[root@www ~]# netstat -g
IPv6/IPv4 Group Memberships
Interface       RefCnt Group
--------------- ------ ---------------------
lo              1      all-systems.mcast.net
eth0            1      all-systems.mcast.net
lo              1      ff02::1
eth0            1      ff02::1:ffa0:eb28
eth0            1      ff02::1

9.顯示 RAW Network 統計

[root@www ~]# netstat --statistics --raw
Ip:
    455 total packets received
    3 with invalid addresses
    0 forwarded
    0 incoming packets discarded
    414 incoming packets delivered
    317 requests sent out
Icmp:
    0 ICMP messages received
    0 input ICMP message failed.
    ICMP input histogram:
    0 ICMP messages sent
    0 ICMP messages failed
    ICMP output histogram:
UdpLite:
IpExt:
    InBcastPkts: 37
    InOctets: 41746
    OutOctets: 53554
    InBcastOctets: 3504
參考:
http://www.tecmint.com/
http://linoxide.com/linux-command/netstat-commad-with-all-variant-outputs/

留言

熱門文章