As Linux users, we sometimes need to know which port number a particular process is listening upon. All ports are associated with a process ID or service in an OS. So how do we find that port? This article presents three different methods for you to find which port number a process is listening on.
We have run the commands and procedures described 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 the network connections. This includes information about interface statistics, routing tables and much more. This utility is available on most Linux systems so let us make use of it to view information about which ports certain processes are using on the system.
For using the netstat command, you need to install the net-tools utility if it is already not installed on your system through the following command:
$ sudo apt install net-tools
Then run the following command:
$ sudo netstat -ltnp
The above command gives netstat information based on the following features:
- l: display only listening sockets
- t: display tcp connection
- n: display addresses in a numerical form
- p: display process ID/ Program name
For example, in the above output of the netstat command, Apache2 program with process ID 950 is running on port number 80.
You can also filter statistics for a specific port by incorporating the grep function into your command.
$ sudo netstat -ltnp | grep -w ':80'
This command will tell you specifically which process is running on port number 80.
Method 2: Using the lsof command
The lsof or the List of Open Files utility helps in listing all the open files on your Linux system. We can use this utility to view all processes open on a specific port.
For using the lsof command, you need to install the lsof utility if it is already not installed on your system through the following command:
$ sudo apt install lsof
Let us use lsof to view 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 displays which process IDs are using the named files, sockets or file systems. We can use this command in order to view process IDs running on a specific TCP port.
For using the fuser command, you need to install the psmisc utility if it is already not installed on your system through the following command:
$ sudo apt install psmisc
Let us view all the process IDs running on TCP port 3306 through the following command:
$ sudo fuser 3306/tcp
You can specify any port number in this command to view its listening processes.
In the above output, you can see that process ID 975 is listening on TCP 3306.
In order to view 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 the process ID 975 corresponds to the program names MySDLd. Thus process ID 975 of the program MySQLd is listening on port number 3306.
Through the three methods you have learned in this article, you can easily view which TCP port a specific process on Linux is listening upon.