As Linux users, we sometimes need to know which port number a particular process is listening on. All ports are associated with a process or service ID in the OS. So how do we find this port? This article introduces three different methods to determine the port number that a process is listening on.
We have followed the commands and procedures in this article on an Ubuntu 18.04 LTS system.
Method 1: Using the netstat command
Netstat or the network statistics utility is used to view information related to network connections. This includes information about interface statistics, routing tables, and more. This utility is available on most Linux systems, so let us use it to view information about which ports certain processes are using on the system.
To use the netstat command, you need to install the net-tools utility, if it is not already installed on your system, using the following command:
$ sudo apt install net-tools
Then run the following command:
$ sudo netstat -ltnp
The above command provides netstat information based on the following functions:
- l: display only listening sockets
- t: display TCP connection
- n: display addresses in numeric form
- p: display process ID / program name
For example, in the above output from the netstat command, the Apache2 program with process ID 950 is running on port 80.
You can also filter statistics for a specific port by including the grep function in your command.
$ sudo netstat -ltnp | grep -w ':80'
This command will tell you exactly which process is running on port 80.
Method 2: using the lsof command
The lsof utility or List of Open Files helps you list all open files on your Linux system. We can use this utility to view all processes open on a specific port.
To use the lsof command, you need to install the lsof utility, if it is not already installed on your system, using the following command:
$ sudo apt install lsof
Let’s use lsof to see the service listening on a specific port.
$ sudo lsof -i :80
This command will list all processes using TCP port number 80.
Method 3: Using the fuser Command
The fuser command shows which process ids the named files, sockets, or filesystems are using. We can use this command to view the IDs of the processes running on a specific TCP port.
To use the fuser command, you need to install the psmisc utility, if it is not already installed on your system, using the following command:
$ sudo apt install psmisc
Let’s see all the IDs of the processes running on TCP port 3306 using the following command:
$ sudo fuser 3306/tcp
You can specify any port number in this command to view the listening processes.
In the above output, you can see that process ID 975 is listening on TCP 3306.
To find out which program this process ID corresponds to, run the following command:
$ ps -p [processID] -o comm=
In our case:
$ ps -p  -o comm=
The output shows that process ID 975 matches the program names MySDLd. So the process ID 975 of the MySQLd program is listening on port 3306.
With the three methods you learned in this article, you can easily find out which TCP port a particular process is listening on in Linux.
Linux: find out what port number a process is listening on