如何在Linux下实时监视TCP和UDP端口

在计算机网络中,网络服务在属于网络一部分的每台计算机中运行一个软件。在Linux系统中,此网络软件使用称为TCP或UDP的协议以及端口号。TCP被称为传输控制协议,而UDP被称为用户数据报协议。在本文中,我们将看到如何观察端口是基于此协议实时运行的套接字。

开放端口列表

第一步,我们寻找系统中可用的开放端口。再见,我们意味着这些端口可用于侦听,这也意味着接受其他系统的请求。以下命令用于列出打开的端口。

$ sudo netstat –tulpn
The meaning of various flags is as below.
t - enable listing of TCP ports.
u - enables listing of UDP ports.
l - print only the open sockets.
p - print the program name
n – print the port number

运行上面的代码给我们以下结果-

Active Internet connections (only
Proto   Recv-Q       Send-Q Local Address      Foreign Address    State   PM/Program name
tcp                  127.0.1.1:53               0.0.0.0:*         LISTEN   966/dnsmasq
tcp                  0.0.0.0:22                 0.0.0.0:*         LISTEN   941/sshd
tcp                  127.0.0.1:631              0.0.0.0:*         LISTEN   11450/cupsd
tcp6                  :::22                                       LISTEN    941/sshd
tcp6                  ::1:631                                     LISTEN   11450/cupsd
udp                 0.0.0.0:50228              0.0.0.0:*                   792/avahi-daemon: r
udp                 0.0.0.0:5353               0.0.0.0:*                   792/avahi-daemon: r
udp                 127.0.1.1:53               0.0.0.0:*                   966/dnsmasq
udp                 0.0.0.0:68                 0.0.0.0:*                   949/dhclient
udp                0.0.0.0:51324               0.0.0.0:*                   966/dnsmasq
udp                0.0.0.0:631                 0.0.0.0:*                   11452/cups-browsed
udp6               :::5353                                                 792/avahi-daemon: r
ud.6               :::50929                                                792 avahi-daemon: r

实时打开端口

但是,如果要实时监视端口,以便可以监视已发送和已接收的字节数,则必须添加监视标志。

$ sudo watch netstat –tulpn

运行上面的代码给我们以下结果-

Active Internet connections (only servers)
Proto         Recv-Q         Send-QLocal Address      Foreign Address   State        PID/Program name
tcp                           127.0.1.1:53               0.0.0.0:*      LISTEN       966/dnsmasq
tcp                           0.0.0.0:22                 0.0.0.0:*      LISTEN       941/sshd
tcp                           127.0.0.1:631              0.0.0.0:*      LISTEN       11450/cupsd
tcp6            63            :::22                      ••LISTEN                    941/sshd
tcp6                          ::1:631                                    LISTEN      11450/cupsd
udp                           0.0.0.0:50228              0.0.0.0:*                   792/avahi-daemon: r
udp                           0.0.0.0:5353               0.0.0.0:*                   792/avahi-daemon: r
udp                           127.0.1.1:53               0.0.0.0:*                   966/dnsmasq
udp                           0.0.0.0:68                 0.0.0.0:*                    949/dhclient
udp                           0.0.0.0:51324              0.0.0.0:*                    966/dnsmasq
udp                           0.0.0.0:631                0.0.0.0:*                    11452/cups-browsed
udp6                          :::5353                     • • •                       792/avahi-daemon: r
udp6                          :::50929                                                792/avahi-daenon: r