Sometimes a program or an application stops responding or you are working on a terminal and want to stop a process. In such cases you need to terminate or kill that process. Linux provides a kill
command that takes the PID or Process Id of the process and terminates it.
Thus, first you must be able to find the process listening on a specific port and then kill it. This post will outline different commands to find and kill a process running on a port in linux.
Method 1: Using lsof
lsof
stands for List of Opened Files and with appropriate options or flags, it can be used to return the PID of a process on some given port. Once you get the PID, use kill
command to stop that process.Below example will terminate the process listening on port 3000,
# get the PID of the process
sudo lsof -i:3000
# use the PID from above command and stop the process
sudo kill PID
Some times the process does not terminate by simply using kill
. Use -9 flag to force stop the process as
sudo kill -9 PID
Above two commands can be merged into a single command as
sudo kill -9 $(lsof -t -i:3000)
This will find the process running on 3000 port and kill it. Notice the -t
option. lsof
used with this option will only return the PID of the process and that is why, it could be chained with kill
.
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 fuser command
fuser
command accepts the port number and protocol as its arguments and returns the details of the process running on that port for the protocol.
It has a -k
flag which is used for killing a process. Thus, with this command you do not need to use or merge two commands as there is a built in option for this. Example,
sudo fuser -k 8080/tcp
This will kill the process running on port 8080 and listening on tcp.
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
netstat
command can also fetch system processes. Its output when chained with grep
command filters it as per the grep expression.Thus, below command will fetch only those processes which contain 3000 or are running on port 3000.
sudo netstat -lp | grep 3000
Here -l
and -p
are the flags of netstat
where -l
finds only listening processes and -p
also fetches the PID of the process.
Copy the PID of the process from the above command and terminate it with kill
as
sudo kill -9 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 4: Using ss command
ss
command can also be used to fetch the details of a running process on linux. Its output when chained with grep
and the required port, will show only the processes running on the given port as shown below.
sudo ss -ltp | grep 3000
Here -l
flag will list only Listening processes, -t
will show the processes that are running over tcp and -p
is required to display the PID of the processes.
Copy the PID from the last command execution and use it to kill the required process as shown below.
sudo kill -9 PID
If you are logged in as a root user which has all the privileges, then you are not required to use sudo
with any of the above commands.
Click the clap below if you liked the article.