linux查看网络命令netstat详解

1.功能与说明

netstat 用于显示linux中各种网络相关信息。如网络链接 路由表 接口状态链接 多播成员等等。

2.参数含义介绍

-a (all)显示所有选项,默认不显示LISTEN相关
-t (tcp)仅显示tcp相关选项
-u (udp)仅显示udp相关选项
-n 拒绝显示别名,能显示数字的全部转化成数字。
-l 仅列出有在 Listen (监听) 的服務状态

-p 显示建立相关链接的程序名
-r 显示路由信息,路由表
-e 显示扩展信息,例如uid等
-s 按各个协议进行统计
-c 每隔一个固定时间,执行该netstat命令。

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

3.常用实例

3.1 列出所有端口: netstat -a

[minping@localhost 2]$ netstat -a|more
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State      
tcp        0      0 localhost:cslistener    0.0.0.0:*               LISTEN     
tcp        0      0 0.0.0.0:mysql           0.0.0.0:*               LISTEN        
tcp        0      0 bogon:domain            0.0.0.0:*               LISTEN     
tcp        0      0 0.0.0.0:ssh             0.0.0.0:*               LISTEN        
tcp        0      0 localhost:40833         0.0.0.0:*               LISTEN     
tcp        0      0 0.0.0.0:50020           0.0.0.0:*               LISTEN     
tcp        0     52 localhost:ssh           bogon:63791             ESTABLISHED
tcp        0      0 localhost:42844         localhost:cslistener    TIME_WAIT  
tcp        0      0 localhost:cslistener    localhost:42846         ESTABLISHED
tcp        0      0 localhost:42846         localhost:cslistener    ESTABLISHED
tcp6       0      0 [::]:ampify             [::]:*                  LISTEN     
--More--

3.2 列出所有tcp端口: netstat -at

[minping@localhost 2]$ netstat -at|more
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State      
tcp        0      0 localhost:cslistener    0.0.0.0:*               LISTEN     
tcp        0      0 0.0.0.0:mysql           0.0.0.0:*               LISTEN      
tcp        0      0 0.0.0.0:http            0.0.0.0:*               LISTEN     
tcp        0      0 bogon:domain            0.0.0.0:*               LISTEN     
tcp        0      0 0.0.0.0:ssh             0.0.0.0:*               LISTEN       
tcp        0      0 0.0.0.0:50020           0.0.0.0:*               LISTEN     
tcp        0     52 localhost:ssh           bogon:63791             ESTABLISHED
tcp        0      0 localhost:42850         localhost:cslistener    ESTABLISHED
tcp        0      0 localhost:42848         localhost:cslistener    TIME_WAIT  
tcp        0      0 localhost:cslistener    localhost:42850         ESTABLISHED
tcp6       0      0 [::]:ampify             [::]:*                  LISTEN     
--More--

3.3 列出所有udp端口: netstat -au

[minping@localhost 2]$ netstat -au|more
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State      
udp        0      0 bogon:domain            0.0.0.0:*                          
udp        0      0 bogon:domain            0.0.0.0:*                          
udp        0      0 0.0.0.0:bootps          0.0.0.0:*                          
udp        0      0 0.0.0.0:bootps          0.0.0.0:*                          
udp        0      0 0.0.0.0:mdns            0.0.0.0:*                          
udp        0      0 0.0.0.0:55723           0.0.0.0:*                          
[minping@localhost 2]$ 

3.4 只显示监听端口: netstat -l

[minping@localhost 2]$ netstat -l|more
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State      
tcp        0      0 localhost:cslistener    0.0.0.0:*               LISTEN     
tcp        0      0 0.0.0.0:mysql           0.0.0.0:*               LISTEN      
tcp        0      0 0.0.0.0:http            0.0.0.0:*               LISTEN     
tcp        0      0 bogon:domain            0.0.0.0:*               LISTEN     
tcp        0      0 0.0.0.0:ssh             0.0.0.0:*               LISTEN     
tcp        0      0 0.0.0.0:50070           0.0.0.0:*               LISTEN      
tcp        0      0 localhost:40833         0.0.0.0:*               LISTEN     
tcp        0      0 0.0.0.0:50020           0.0.0.0:*               LISTEN     
tcp6       0      0 [::]:ampify             [::]:*                  LISTEN         
tcp6       0      0 localhost:smtp          [::]:*                  LISTEN     
--More--

3.5只显示监听的tcp端口:netstat -lt

[minping@localhost 2]$ netstat -lt|more
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State      
tcp        0      0 localhost:cslistener    0.0.0.0:*               LISTEN     
tcp        0      0 0.0.0.0:mysql           0.0.0.0:*               LISTEN     
tcp        0      0 0.0.0.0:http            0.0.0.0:*               LISTEN     
tcp        0      0 bogon:domain            0.0.0.0:*               LISTEN     
tcp        0      0 0.0.0.0:ssh             0.0.0.0:*               LISTEN     
tcp        0      0 0.0.0.0:50070           0.0.0.0:*               LISTEN      
tcp6       0      0 localhost:ipp           [::]:*                  LISTEN     
tcp6       0      0 [::]:radan-http         [::]:*                  LISTEN     
tcp6       0      0 localhost:smtp          [::]:*                  LISTEN     
--More--

3.6 只显示所有监听udp端口 netstat -lu

[minping@localhost 2]$ netstat -lu|more
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State      
udp        0      0 bogon:domain            0.0.0.0:*                          
udp        0      0 bogon:domain            0.0.0.0:*                          
udp        0      0 0.0.0.0:bootps          0.0.0.0:*                          
udp        0      0 0.0.0.0:bootps          0.0.0.0:*                          
udp        0      0 0.0.0.0:mdns            0.0.0.0:*                          
udp        0      0 0.0.0.0:55723           0.0.0.0:*                          
[minping@localhost 2]$ 

3.7 只显示所有监听unix端口:

[minping@localhost 2]$ netstat -lx|more
Active UNIX domain sockets (only servers)
Proto RefCnt Flags       Type       State         I-Node   Path
unix  2      [ ACC ]     STREAM     LISTENING     20992    /var/run/lsm/ipc/sim
unix  2      [ ACC ]     STREAM     LISTENING     30534    @/tmp/.ICE-unix/1403
unix  2      [ ACC ]     STREAM     LISTENING     3941064  /var/lib/mysql/mysql.sock
unix  2      [ ACC ]     STREAM     LISTENING     28751    @/tmp/.X11-unix/X0
unix  2      [ ACC ]     STREAM     LISTENING     3930147  /var/run/docker.sock
unix  2      [ ACC ]     STREAM     LISTENING     28630    public/pickup
unix  2      [ ACC ]     STREAM     LISTENING     28640    public/cleanup
unix  2      [ ACC ]     STREAM     LISTENING     28643    public/qmgr
unix  2      [ ACC ]     STREAM     LISTENING     28673    private/proxywrite
--More--

3.8显示所有端口的统计信息 netstat -s

[minping@localhost 2]$ netstat -s
Ip:
    33556235 total packets received
    0 forwarded
    0 incoming packets discarded
    33491530 incoming packets delivered
    35625644 requests sent out
Tcp:
    95737 active connections openings
    94441 passive connection openings
    1669 failed connection attempts
    393 connection resets received
    5 connections established
    32049989 segments received
    31921579 segments send out
    5629 segments retransmited
    3 bad segments received.
    31858 resets sent
Udp:
    10401 packets received
    151 packets to unknown port received.
    3 packet receive errors
    803 packets sent
    0 receive buffer errors
    0 send buffer errors
    InCsumErrors: 3

3.9 显示所有tcp(netstat -st)或udp(netstat -su)的统计信息
3.10 显示pid/进程名称 netstat -p,netstat -p也可以与其他参数一起使用,比如netstat -pt表示显示tcp端口的pid/进程名称:

[root@localhost 2]# netstat -pt|more
Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0     52 localhost:ssh           bogon:63791             ESTABLISHED 2118/sshd: minping  
tcp        0      0 localhost:43370         localhost:cslistener    TIME_WAIT   -                   
tcp        0      0 localhost:cslistener    localhost:49442         ESTABLISHED 2440/java           
tcp        0      0 localhost:49442         localhost:cslistener    ESTABLISHED 2573/java           
tcp6       0      0 localhost:8031          localhost:58750         ESTABLISHED 2970/java           
tcp6       0      0 localhost:58750         localhost:8031          ESTABLISHED 3097/java           
[root@localhost 2]# 

3.11 显示路由信息

[root@localhost 2]# netstat -r
Kernel IP routing table
Destination     Gateway         Genmask         Flags   MSS Window  irtt Iface
default         gateway         0.0.0.0         UG        0 0          0 eno16780032
10.95.44.0      0.0.0.0         255.255.254.0   U         0 0          0 eno16780032
172.17.0.0      0.0.0.0         255.255.0.0     U         0 0          0 docker0
192.168.122.0   0.0.0.0         255.255.255.0   U         0 0          0 virbr0
[root@localhost 2]# 

3.12 显示网络接口列表: netstat -i

[root@localhost 2]# netstat -i
Kernel Interface table
Iface      MTU    RX-OK RX-ERR RX-DRP RX-OVR    TX-OK TX-ERR TX-DRP TX-OVR Flg
docker0   1500        0      0      0 0             0      0      0      0 BMU
eno16780  1500  7724564      0  13269 0       4674919      0      0      0 BMRU
lo       65536 31157983      0      0 0      31157983      0      0      0 LRU
virbr0    1500        0      0      0 0             0      0      0      0 BMU
[root@localhost 2]# 

reference

你可能感兴趣的:(linux查看网络命令netstat详解)