Linux: Find Out Which Port Number a Process is Listening on

Linux Listen port

As Linux users, we sometimes need to know what port number a particular process is listening on. All ports are associated with a process ID or service in an operating system. So how do we find this port? In this article, we will present three different methods that you can use to find out which port a process is listening on.

We have run the commands and procedures described in this article on an Ubuntu 22.04 LTS system.

Method 1: Using the netstat command

Netstat, the network statistics utility, is used to display information about the network connections. This includes information about interface statistics, routing tables and much more. This utility is available on most Linux systems, so we use it to find out which ports certain processes on the system are using.

To use the netstat command, you must install the net-tools utility, if it is not already installed on your system, using the following command:

$ sudo apt install net-tools

Install net-tools

Then run the following command:

$ sudo netstat -ltnp

Run netstat command

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.

Example:

$ sudo netstat -ltnp | grep -w ':80'

This command will tell you specifically which process is running on port number 80.

Check which program listens on port 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

Install lsof tool

Let us use lsof to view the service listening on a specific port.

Example:

$ sudo lsof -i :80

This command will list all processes using TCP port number 80.

Check which application uses port 80 with lsof

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

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.

Use fuser command

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:

Syntax:

$ ps -p [processID] -o comm=

In our case:

$ ps -p [975] -o comm=

Check port of a specific process ID

The output shows that process ID 975 corresponds to the program name 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.