<img alt="Linux Listen port" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/04/echo/linux-listen-port-1024×512.jpg" data-ez ezimgfmt="rs rscb10 src ng ngcb10 srcset" height="375" src="data:image/svg xml,” width=”750″>

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

<img alt="Install net-tools" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/04/echo/word-image-80.png62673424cfe8c.jpg" data-ez ezimgfmt="rs rscb10 src ng ngcb10 srcset" height="122" loading="lazy" src="data:image/svg xml,” width=”367″>

Then run the following command:

$ sudo netstat -ltnp

<img alt="Run netstat command" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/04/echo/word-image-81.png62673424e1da2.jpg" data-ez ezimgfmt="rs rscb10 src ng ngcb10 srcset" height="186" loading="lazy" src="data:image/svg xml,” width=”914″>

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.

<img alt="Check which program listens on port 80" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/04/echo/word-image-82.png62673424f1f42.jpg" data-ez ezimgfmt="rs rscb10 src ng ngcb10 srcset" height="38" loading="lazy" src="data:image/svg xml,” width=”860″>

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

<img alt="Install lsof tool" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/04/echo/word-image-83.png626734250f374.jpg" data-ez ezimgfmt="rs rscb10 src ng ngcb10 srcset" height="100" loading="lazy" src="data:image/svg xml,” width=”328″>

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.

<img alt="Check which application uses port 80 with lsof" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/04/echo/word-image-84.png6267342520dfa.jpg" data-ez ezimgfmt="rs rscb10 src ng ngcb10 srcset" height="152" loading="lazy" src="data:image/svg xml,” width=”641″>

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

<img alt="Install psmisc" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/04/echo/word-image-85.png6267342532085.jpg" data-ez ezimgfmt="rs rscb10 src ng ngcb10 srcset" height="99" loading="lazy" src="data:image/svg xml,” width=”365″>

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.

<img alt="Use fuser command" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/04/echo/word-image-86.png62673425418fd.jpg" data-ez ezimgfmt="rs rscb10 src ng ngcb10 srcset" height="41" loading="lazy" src="data:image/svg xml,” width=”308″>

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=

<img alt="Check port of a specific process ID" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/04/echo/word-image-87.png6267342553ced.jpg" data-ez ezimgfmt="rs rscb10 src ng ngcb10 srcset" height="43" loading="lazy" src="data:image/svg xml,” width=”317″>

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.