netstat in Ubuntu
To find your way around network administration more easily, namely such concepts as IP address types, masks, subnets, etc., I recommend studying the article
«Computer Networking Basics»
Introduction
Configuring a network connection is the most important step when working with Ubuntu, and it can raise
many questions for a beginner.
In this article you can find answers to some of them.
Listening ports
lsof
The lsof utility lets you view
all
the connections open in the system.
To examine specifically network connections, use the option
i
To display ports rather than network service names, apply the option
P
lsof -i -P
ss
The ss utility can also answer the question of how to view the list of open ports
ss -lntup
If you need to check whether a certain port is in use, for example 5000
ss -na | grep :5000
If the output is empty, the port is not in use
To find out which program is listening on a certain port, you first need to install one of the
tools:
netstat, fuser or lsof
Let us start with netstat
Netstat
sudo apt install net-tools
With the command netstat you can view the list of active network connections
netstat
netstat is usually used with the set of options tulpn, which lets you understand which program is listening on which port.
netstat -tulpn
(Not all processes could be identified, non-owned process info will not be shown, you would have to be root to see it all.) 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 - tcp 0 0 127.0.0.1:631 0.0.0.0:* LISTEN - tcp 0 0 127.0.0.1:6010 0.0.0.0:* LISTEN - tcp 0 0 127.0.0.1:6011 0.0.0.0:* LISTEN - tcp6 0 0 :::22 :::* LISTEN - tcp6 0 0 ::1:631 :::* LISTEN - tcp6 0 0 ::1:6010 :::* LISTEN - tcp6 0 0 ::1:6011 :::* LISTEN - tcp6 0 0 :::80 :::* LISTEN - udp 0 0 0.0.0.0:52756 0.0.0.0:* - udp 0 0 0.0.0.0:68 0.0.0.0:* - udp 0 0 0.0.0.0:631 0.0.0.0:* - udp 0 0 0.0.0.0:5353 0.0.0.0:* - udp6 0 0 :::45815 :::* - udp6 0 0 :::5353 :::* -
Article author: Andrei Olegovich