Debug School

rakesh kumar
rakesh kumar

Posted on

How to kill a process listening on a port in linux Port?

reference1
reference2
How to Kill Process on Port?
Applications that are communicating on a specific port can easily be terminated by instructing the computer to do so. This process, however, differs on operating systems and requires administrative privileges. Also, make sure that there isn’t any other application that is using the port. Below the method to kill a process from its port number is listed below, since the method differs on operating systems, we have listed the methods for some of the most popular ones.

Kill Process on Port in Mac and Linux

Open the terminal and make sure you are signed in as the root user

List the processes that are listening on a specific port by typing in the following command and executing it.

lsof -i:(port number)
Enter fullscreen mode Exit fullscreen mode

In order to terminate any process that is using the port number to communicate, type in the following command and execute it.

kill $(lsof -t -i:"Port Number")
Enter fullscreen mode Exit fullscreen mode

The above command might not work on high privilege applications, therefore, execute the following command to immediately terminate any process communicating at a specific port.

kill -9 $(lsof -t -i:"Port Number")
Enter fullscreen mode Exit fullscreen mode

This command will immediately terminate any process communicating through the specified port number.

Kill Process on Port in Windows

Press “Windows” + “R” to open Run prompt.
Type in “cmd” and press “Shift” + “Ctrl” + “Enter” to open in administrative mode.
Enter fullscreen mode Exit fullscreen mode

Typing cmd in the Run Prompt and pressing “Shift”+ “Ctrl” + “Enter”
Type in the following command to list the process communicating on a specific port.

netstat -ano | findstr :<yourPortNumber>
Enter fullscreen mode Exit fullscreen mode

Typing in the command
This will list the process running on a specific port, note the “PID” for the program.
In order to kill all the processes using the specific port, type in the following command and press “Enter” to execute it.

taskkill /PID <typeyourPIDhere> /F
Enter fullscreen mode Exit fullscreen mode

This will immediately terminate the program.

METHOD 2 TO KILL PROCESS

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode
# or use yum
sudo yum install psmisc
Enter fullscreen mode Exit fullscreen mode

Method 3: Using netstat
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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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.

Top comments (0)