Kill a Process Running on a Specific Port in Linux (via 4 Methods)

When working with Linux systems, it is sometimes necessary to kill a process running on a specific port. This can happen when the process is not responding, consuming too much resources, or causing other issues on the system. In this article, we will discuss four methods to kill a process running on a specific port in Linux.

Method 1: Using the lsof Command

The lsof command is a powerful tool that allows you to list all open files and processes on a system. To kill a process running on a specific port using the lsof command, you can follow these steps:

  1. Open a terminal window.
  2. Run the following command to list all processes listening on a specific port (replace PORT_NUMBER with the actual port number):
    lsof -i:PORT_NUMBER
  3. Find the process ID (PID) of the process you want to kill in the output.
  4. Run the following command to kill the process (replace PID with the actual process ID):
    kill -9 PID

Method 2: Using the fuser Command

The fuser command is another useful tool for identifying processes using specific files or ports. To kill a process running on a specific port using the fuser command, you can follow these steps:

  1. Open a terminal window.
  2. Run the following command to list all processes using a specific port (replace PORT_NUMBER with the actual port number):
    fuser -k PORT_NUMBER/tcp

Method 3: Using the netstat and kill Commands

The netstat command is used to display network connections and routing tables. You can combine it with the kill command to kill a process running on a specific port. Here’s how you can do it:

  1. Open a terminal window.
  2. Run the following command to list all processes using a specific port (replace PORT_NUMBER with the actual port number):
    netstat -tulnp | grep :PORT_NUMBER
  3. Find the process ID (PID) of the process you want to kill in the output.
  4. Run the following command to kill the process (replace PID with the actual process ID):
    kill -9 PID

Method 4: Using the fuser and kill Commands

You can also combine the fuser and kill commands to kill a process running on a specific port. Here’s how you can do it:

  1. Open a terminal window.
  2. Run the following command to list all processes using a specific port (replace PORT_NUMBER with the actual port number):
    fuser -k -n tcp PORT_NUMBER
  3. Answer yes when prompted to kill the process using the specified port.

In conclusion, killing a process running on a specific port in Linux can be done using various methods such as the lsof, fuser, netstat, and kill commands. It is important to be careful when killing processes, as this can have unintended consequences on the system. It is recommended to only kill processes that are not responding or causing issues on the system.