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.
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
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
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
where 26549 is the PID of the process running on port 9090.
If you omit the -p
flag, the modified output will be
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
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
9090/tcp: root 26549 F…. app
If used without -v
flag, fuser
will only display the PID of the process as shown below
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
sudo
before the command.Hope this article was useful for you, click the clap below to promote it.