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

Linux TLDR
Last Updated:
Reading time: 4 minutes

A newbie user often struggles to identify the process behind a specific listening port. Indeed, it’s not all their fault, as some listening ports are started and managed by the OS. However, they may forget the name or struggle to find the process ID of the service they manually started.

The running (or unresponsive) process must be stopped to free the occupied port and make it available for other processes. Let’s assume you are running an Apache server that uses ports 80 (for HTTP) and 443 (for HTTPS). You won’t be able to launch an Nginx server that shares these common ports until the Apache server is stopped.

It’s one of the many scenarios, and listening ports are often overlooked by users until a process fails to launch due to port unavailability. Hence, in this quick guide, I’ll show you how to identify and kill a process running on a specific port in Linux.

How to Kill a Process Running on a Specific Port in Linux

There are many ways to find and terminate processes running on a certain port. However, IT Guy, SysAdmin, or network engineers often favor using the CLI tool for this job. In such cases, you can use the “killport“, “fuser“, “lsof“, “netstat“, and “ss” commands as detailed in the following sections.

Method 1: Kill a Process Running on a Specific Port Using killport

Killport is a fantastic CLI tool for killing a process running on a specific port by using only the port number, without needing a service name or process ID. The only inconvenience is that it’s an external tool, but you can quickly install it on your Linux system by following our installation guide.

Once you have it installed, you can quickly terminate the process running on a certain port. Let’s assume you have an Apache server running on port 80. To stop it, simply execute this command:

$ sudo killport 80

Output:

kill process running on a specific port using killport

Well, ignore the last “No such process” message—it’s simply the response to the last kill signal sent to the process. The key point is that the port is now available for use by any other process.

Method 2: Kill a Process Running on a Specific Port Using fuser

Fuser is another great tool used for identifying processes using specific files, file systems, or sockets. Despite using it to identify processes running on specific sockets (or ports), you can use it to troubleshoot issues related to file locking, process management, and system resources.

It comes preinstalled on some popular Linux distributions like Ubuntu, Fedora, and Manjaro, but if it’s not available on your system, you can install the “psmisc” package that contains “fuser” and other command-line utilities.

# On Debian, Ubuntu, Kali Linux, Linux Mint, Zorin OS, Pop!_OS, etc.
$ sudo apt install psmisc

# On Red Hat, Fedora, CentOS, Rocky Linux, AlmaLinux, etc.
$ sudo dnf install psmisc

# On Arch Linux, Manjaro, BlackArch, Garuda, etc.
$ sudo pacman -S psmisc

# On OpenSUSE system
$ sudo zypper install psmisc

To find out the process running on a specific port, you can specify the port number and its TCP or UDP protocol in the “fuser” command.

$ sudo fuser 80/tcp

The above command will return the process ID in charge of handling the specified port.

finding out which process is running on a particular port

Instead of printing the running process ID, you can use the “-k” option with the above command to terminate the process associated with that process ID.

$ sudo fuser -k 80/tcp

Output:

killing the process running on a specific port

Once you terminate the process with this method, you may need to wait a 60-second delay before the process fully shuts down. This is implemented as a security measure to avoid potential data corruption or conflicts. If you want to immediately stop the running process, you can specify the process ID in the “sudo kill -9 <PID>” command.

Method 3: Kill a Process Running on a Specific Port Using lsof

Lsof is another powerful tool used to identify the process responsible for managing specific files, directories, network sockets, and other system resources on the active system. It comes pre-installed with nearly all Linux distributions, requiring no additional installation.

To identify the process name and ID associated with a specific port, use the following command, followed by the port number you wish to check:

$ sudo lsof -i :80

The above command will return the output in multiple columns, with your focus areas being solely the “COMMAND” and “PID” columns.

list process name and PID of particular port

Once you have the process ID, you can use the “kill” command to terminate the process.

$ sudo kill -9 36749 36751 36752

Output:

killing the process running for a specific port

The “-9” option sends the “SIGKILL” signal to aggressively terminate the process, while you can alternatively use the “-1” option to hang up the process (less secure) and the “-15” option to gently kill the process (default).

Method 4: Kill a Process Running on a Specific Port Using netstat and ss

Netstat and ss are among the most widely used tools for SysAdmins to quickly pinpoint a process name and process ID associated with a specific port. However, netstat is considered depricated, and some major Linux distributions have removed it, requiring the installation of the “net-tools” package for usage.

The ss command can be found in most Linux systems, and it’s basically an improved version of netstat. Both tools use almost identical command syntaxes, with the “-tnlp” option being the most common to identify the listening port’s process name and process ID, where each option follows.

  • -t“: Shows the TCP sockets
  • -n“: Avoid resolving the service names
  • -l“: Show the listening sockets
  • -p“: Show the process ID and name

To find out the process name or ID of port 80, you can use either the netstat or ss command with the “-tnlp” option, along with the grep command, to filter out the data for only the specified port number.

$ sudo netstat -tnlp | grep -i :80
$ sudo ss -tnlp | grep -i :80

Output:

find process name and id using the port number

Instead of specifying the port number in the grep command, you can also use the service name to identify its process ID and listening port.

$ sudo netstat -tnlp | grep -i apache
$ sudo ss -tnlp | grep -i apache

Output:

find process name and id using the service name

Finally, to kill the corresponding process, you can specify its process ID with the following command:

$ sudo kill -9 41005

Output:

terminating process listening to specific port

When terminating the process using the “kill -p” command, ensure that the service is not actively being used by any other process, as forcefully terminating it could lead to data corruption or loss.

Final Word

In this article, you learned different ways to terminate a process running on a specific port that would work for almost all major Linux distributions, such as Debian, Ubuntu, Red Hat, Fedora, Arch, Manjaro, etc. Well, if you have any questions or queries, feel free to tell us in the comment section.

Till then, peace!

Join The Conversation

Users are always welcome to leave comments about the articles, whether they are questions, comments, constructive criticism, old information, or notices of typos. Please keep in mind that all comments are moderated according to our comment policy.