命令 netstat 查看网络状态

netstat 命令用来打印 Linux 中网络系统的状态信息,可让你得知整个 Linux 系统的网络情况。

  • 命令语法
    netstat (参数)

  • 命令选项
    a:=all 显示所有连线中的 socket;
    -l:=listening 显示监控中的服务器的 socket;
    -n:=numeric 直接使用 IP 地址;
    -p:=programs 显示正在使用 socket 的程序识别码和程序名称;
    -t:=tcp 显示 tcp 传输协议的连接状况;


  • 查看监听端口
[root@localhost ~]# netstat -lnp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1467/sshd           
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      2036/master         
tcp6       0      0 :::22                   :::*                    LISTEN      1467/sshd           
tcp6       0      0 ::1:25                  :::*                    LISTEN      2036/master         
udp        0      0 127.0.0.1:323           0.0.0.0:*                           621/chronyd         
udp6       0      0 ::1:323                 :::*                                621/chronyd         
raw6       0      0 :::58                   :::*                    7           671/NetworkManager  
Active UNIX domain sockets (only servers)
Proto RefCnt Flags       Type       State         I-Node   PID/Program name     Path
unix  2      [ ACC ]     STREAM     LISTENING     14377    1/systemd            /var/run/dbus/system_bus_socket
。。。。。。。。。。。。。。。。。。

  • 查看网络连接状况
[root@localhost ~]# netstat -an
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State      
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN     
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN     
tcp        0     52 192.168.152.128:22      192.168.152.1:56504     ESTABLISHED
tcp6       0      0 :::22                   :::*                    LISTEN     
tcp6       0      0 ::1:25                  :::*                    LISTEN     
udp        0      0 127.0.0.1:323           0.0.0.0:*                          
udp        0      0 192.168.152.128:52670   193.228.143.23:123      ESTABLISHED
udp        0      0 192.168.152.128:40023   51.15.41.135:123        ESTABLISHED
udp6       0      0 ::1:323                 :::*                               
raw6       0      0 :::58                   :::*                    7          
Active UNIX domain sockets (servers and established)
Proto RefCnt Flags       Type       State         I-Node   Path
unix  2      [ ACC ]     STREAM     LISTENING     14377    /var/run/dbus/system_bus_socket
unix  2      [ ACC ]     STREAM     LISTENING     18825    public/qmgr
。。。。。。。。。。。。。。。。。

  • 只查看 tcp 协议连接
[root@localhost ~]# netstat -lntp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1467/sshd           
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      2036/master         
tcp6       0      0 :::22                   :::*                    LISTEN      1467/sshd           
tcp6       0      0 ::1:25                  :::*                    LISTEN      2036/master    

  • 查看 tcp 协议状态的命令
[root@localhost ~]# netstat -an | awk '/^tcp/ {++sta[$NF]} END {for(key in sta) print key,"\t",sta[key]}'
LISTEN 	 4
ESTABLISHED 	 1