linux port monitor Netstat commonly used commands

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/wsp_1138886114/article/details/100049348

A, Netstat

Netstat is a command-line tool can be used on all of the listed network socket connection system, comprising tcp, udp and unix socket, while it also listed in the listening state (i.e., waiting for access requests) socket.

Listening port (list all ports, to see whether the service is running)

$ netstat -lntp         # 查看系统的监听端口(最常用)

Two, Netstat commonly used commands

$ netstat -a            # 列出所有连接
$ netstat -at           # 列出 TCP协议的连接
$ netstat -au           # 列出 UDP 协议的连接
$ netstat -ant          # 禁用反向域名解析,加快查询速度(列出所有 TCP 协议的连接,禁用域名解析技术)
$ sudo netstat -nlpt    # 获取进程名、进程号
$ sudo netstat -ltpe    # 获取进程名、进程号以及用户 ID
$ netstat -s            # netstat 可以打印出网络统计数据,包括某个协议下的收发包数量。
$ netstat -rn           # 显示内核路由信息
$ netstat -i            # 打印网络接口
$ netstat -ct           # 持续输出
$ netstat -g            # 输出 IPv4 和 IPv6 的多播组信息。
Viewer 2.1 Linux port usage
netstat –apn                //查看所有的进程和端口使用情况
netstat -ntlp               //查看当前所有tcp端口·
netstat -ntulp |grep 80     //查看所有80端口使用情况,    kill -9 {PID}
netstat -an | grep 3306     //查看所有3306端口使用情况·


# 打印 active 状态的连接,使用watch 命令监视 active 状态的连接
$ netstat -atnp | grep ESTA
$ watch -d -n0 "netstat -atnp | grep ESTA"   

# 查看服务是否在运行(指定服务名)
$ sudo netstat -aple | grep ntp
udp      0     0 enlightened.local:ntp   *:*           root      17430      1789/ntpd       
udp      0     0 localhost:ntp           *:*           root      17429      1789/ntpd       
udp      0     0 *:ntp                   *:*           root      17422      1789/ntpd       
udp6     0     0 fe80::216:36ff:fef8:ntp [::]:*        root      17432      1789/ntpd       
udp6     0     0 ip6-localhost:ntp       [::]:*        root      17431      1789/ntpd       
udp6     0     0 [::]:ntp                [::]:*        root      17423      1789/ntpd       
Viewer 2.2 Linux port occupancy
ps -aux | grep tomcat      # 查看特定进程和端口使用情况
ps -aux | grep java        # `ps -aux | grep pid`同效

netstat -nat | grep -iw "8080" | wc -l   # 查看8080端口被占用的数量
netstat -nat | grep -iw "8080"           # 当前8080端口被哪些进程占用
lsof -i:8080                             # 查看8080端口PID、FD、USER等信息

About kill the process, please click: https: //blog.csdn.net/wsp_1138886114/article/details/98207128

Three, Netstat command details

Command:netstat -a , -tlists the TCP protocol, -ulists the UDP protocol

$ netstat -a          # 列出所有连接
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State      
tcp        0      0 enlightened:domain      *:*                     LISTEN     
tcp        0      0 localhost:ipp           *:*                     LISTEN     
tcp        0      0 enlightened.local:54750 li240-5.members.li:http ESTABLISHED
tcp6       0      0 ip6-localhost:ipp       [::]:*                  LISTEN     
udp        0      0 enlightened:domain      *:*                                
udp        0      0 *:bootpc                *:*                                                    
udp6       0      0 [::]:54952              [::]:*                             
Active UNIX domain sockets (servers and established)
Proto RefCnt Flags       Type       State         I-Node   Path
unix  2      [ ACC ]     STREAM     LISTENING     12403    @/tmp/dbus-IDgfj3UGXX
unix  2      [ ACC ]     STREAM     LISTENING     40202    @/dbus-vfs-daemon/socket-6nUC6CCx

$ netstat -at         # 列出 TCP协议的连接
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State      
tcp        0      0 enlightened:domain      *:*                     LISTEN     
tcp        0      0 localhost:ipp           *:*                     LISTEN     
tcp        0      0 enlightened.local:37892 ABTS-North-Static-:http ESTABLISHED
.....

$ netstat -au        # 列出 UDP 协议的连接
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State      
udp        0      0 *:34660                 *:*                                
udp        0      0 enlightened:domain      *:*                                                   
udp        0      0 *:ntp                   *:*                                
udp6       0      0 fe80::216:36ff:fef8:ntp [::]:*                             
udp6       0      0 ip6-localhost:ntp       [::]:*                             
udp6       0      0 [::]:ntp                [::]:*


$ netstat -ant       # 列出所有 TCP 协议的连接,没有使用域名解析技术
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State      
tcp        0      0 127.0.1.1:53            0.0.0.0:*               LISTEN     
tcp        0      0 127.0.0.1:631           0.0.0.0:*               LISTEN     
tcp        0      0 192.168.1.2:49058       173.255.230.5:80        ESTABLISHED
tcp        0      0 192.168.1.2:33324       173.194.36.117:443      ESTABLISHED
tcp6       0      0 ::1:631                 :::*                    LISTEN

-l: Displays only listening sockets (sockets is the so-called enabling applications to read and write and send and receive communication protocols (protocol) program and data)
-p: display the process identifier and program name, and each socket / port belongs to a program.
-n: DNS polling is not performed, display IP (speed up operation)

$ sudo netstat -nlpt   # 获取进程名、进程号以及用户 ID
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 127.0.1.1:53            0.0.0.0:*               LISTEN      1144/dnsmasq    
tcp        0      0 127.0.0.1:631           0.0.0.0:*               LISTEN      661/cupsd       
tcp6       0      0 ::1:631                 :::*                    LISTEN      661/cupsd


$ sudo netstat -ltpe    # 同时查看进程名、进程号和用户名。
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address     Foreign Address   State     User     Inode    PID/Program name
tcp       0     0 enlightened:domain  *:*               LISTEN    root     11090    1144/dnsmasq   
tcp       0     0 localhost:ipp       *:*               LISTEN    root     9755     661/cupsd       
tcp6      0     0 ip6-localhost:ipp   [::]:*            LISTEN    root     9754     661/cupsd


$ netstat -s          # netstat 可以打印出网络统计数据,包括某个协议下的收发包数量。
Ip:
    32797 total packets received
    0 forwarded
    0 incoming packets discarded
    32795 incoming packets delivered
    29115 requests sent out
    60 outgoing packets dropped
Icmp:
    125 ICMP messages received
    0 input ICMP message failed.
    ICMP input histogram:
        destination unreachable: 125
    125 ICMP messages sent
    0 ICMP messages failed
    ICMP output histogram:
        destination unreachable: 125
... OUTPUT TRUNCATED ...


$ netstat -rn        # 显示内核路由信息
Kernel IP routing table
Destination     Gateway         Genmask         Flags   MSS Window  irtt Iface
0.0.0.0         192.168.1.1     0.0.0.0         UG        0 0          0 eth0
192.168.1.0     0.0.0.0         255.255.255.0   U         0 0          0 eth0

$ 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     31611      0      0 0         27503      0      0      0 BMRU
lo        65536 0      2913      0      0 0          2913      0      0      0 LRU

$ netstat -ie/ifconfig
Kernel Interface table
eth0      Link encap:Ethernet  HWaddr 00:16:36:f8:b2:64  
          inet addr:192.168.1.2  Bcast:192.168.1.255  Mask:255.255.255.0
          inet6 addr: fe80::216:36ff:fef8:b264/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:31682 errors:0 dropped:0 overruns:0 frame:0
          TX packets:27573 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:29637117 (29.6 MB)  TX bytes:4590583 (4.5 MB)
          Interrupt:18 Memory:da000000-da020000 
 
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:2921 errors:0 dropped:0 overruns:0 frame:0
          TX packets:2921 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:305297 (305.2 KB)  TX bytes:305297 (305.2 KB)



$ netstat -ct   # 持续输出
$ netstat -g    # 输出 IPv4 和 IPv6 的多播组信息。
IPv6/IPv4 Group Memberships
Interface       RefCnt Group
--------------- ------ ---------------------
lo              1      all-systems.mcast.net
eth0            1      224.0.0.251
eth0            1      all-systems.mcast.net
lo              1      ip6-allnodes
lo              1      ff01::1
eth0            1      ff02::fb
eth0            1      ff02::1:fff8:b264
eth0            1      ip6-allnodes
eth0            1      ff01::1
wlan0           1      ip6-allnodes
wlan0           1      ff01::1

Guess you like

Origin blog.csdn.net/wsp_1138886114/article/details/100049348