How to Watch TCP and UDP Ports in Real-time in Linux?


In a computer network, the network service runs a software in each of the computers that are part of the network. In Linux System this network software uses a protocol called TCP or UDP along with port number. TCP is known as transmission control protocol and UDP is known as user datagram protocol. In this article we will see how to watch the ports are sockets which are running on this protocols on a real time basis.

List of Open Ports

As a first step we look for the open ports that are available in the system. Bye open we mean the ports are available for listening which also means accepting request from other systems. The below command is used to list the open ports.

$ 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

Running the above code gives us the following result −

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

Open Ports in Real-Time

But in case you want to watch the ports in real time, so that you can monitor the number of bytes, sent and received, then we have to add the watch flag.

$ sudo watch netstat –tulpn

Running the above code gives us the following result −

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

Updated on: 25-Feb-2020

754 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements