How to find process running on port in linux

Often we need to find out a process or a service running on some port. This is because we have to start a new instance of the same or different service on that port but we cannot until the current process is terminated.
Thus, it is required to find out the PID of a process using its port so that the process can be terminated.
There are many different ways in which a process can be checked using its port and this article will discuss them in detail with example.

Method 1: Using lsof command
lsof stands for List of open files and can be used to retrieve details of open files on linux. Since a process running on linux is also a file, this command can be used to get the information about a process.
lsof has an -i flag which lists out all the files that are opened by network connections or listening on different ports and returns the name of process, its PID, user who started the process and port number on which the process is running along with other details.
This flag should be followed by a colon and the port number for which you want the process details.
Thus, for finding a process running on port 9090, use the below command

lsof -i:9090

Notice that there should be no space between -i, : and 9090.
It displays the below output

COMMAND    PID     USER   FD   TYPE   DEVICE    SIZE/OFF NODE NAME
app        26549   root   4u   IPv6   2427316   0t0 TCP *:9090 (LISTEN)

If lsof is not already installed on your system, then install it using apt or yum as shown below

# use apt
sudo apt install lsof

# or use yum
sudo yum install lsof

Method 2: Using netstat command
netstat stands for Network Statistics and is a utility for displaying information about network and related details over TCP/IP protocol. This utility is available in Windows, Linux and Mac operating systems.
In order to view a process running on a particular port, use this command with -l and -p flags along with grep command to filter out unwanted port details.
Here -l flag is used to list out all Listening ports and -p is required for displaying the PID of the process. If you do not want the PID of the processes then omit the -p flag.
Thus, to list out the process details running on port 9090, use the following command

netstat -lp | grep 9090

which prints the below result

tcp6 0 0 [::]:9090 [::]:* LISTEN 26549/app

where 26549 is the PID of the process running on port 9090.
If you omit the -p flag, the modified output will be

tcp6 0 0 [::]:9090 [::]:* LISTEN

Note that now there is no PID.
If netstat is not already installed on your system, then install it using apt or yum as shown below

# use apt
sudo apt install net-tools

# or use yum
sudo apt install net-tools

Method 3: Using fuser command
fuser command is used to display details of a process that is using a particular file, folder or socket. As stated earlier, a process in linux is also a file and if you know anything about that process such as its port number(or socket), then fuser can be used to display its details.
To display the details of a process running on port 9090, use the below command.

sudo fuser -v 9090/tcp

where tcp is the protocol on which the process is running and -v flag stands for verbose and it is used to display a detailed output.
Thus, above command will output

           USER     PID     ACCESS    COMMAND
9090/tcp:  root     26549   F….     app

If used without -v flag, fuser will only display the PID of the process as shown below

9090/tcp: 26549

This command also has a -k flag which can be directly used to kill the process that is returned by the command output. Thus, you do not need to explicitly use kill for terminating a process as with other methods stated above.
If fuser is not already installed on your system, then install it using apt or yum as shown below

# use apt
sudo apt install psmisc

# or use yum
sudo yum install psmisc

Method 4: Using ss command
ss command can also be used to display details about a process. To find out details of a process running on a port, pipe its pipe its output to grep command to show the results of a specific port.
Below command will display the processes listening on a specific port.

sudo ss -ltp | grep 9090

where -l will display listening processes, -t will show only tcp processes and -p will also display the PID of the processes. Output of above command will be

LISTEN 0 128 *:9090 *:* users:((“app”,pid=26549,fd=4))
Remember that if you are logged in with root user, then you do not need to add sudo before the command.
Hope this article was useful for you, click the clap below to promote it.

Leave a Reply