5 Ways to Kill a Process on a Port in Ubuntu Linux

Learning to manage processes and ports using the command in Linux is quite important if you are planning to use open-source distros, especially for commercial purposes. It helps the Linux administrators to find and end the stubborn process occupying some specific port and hindering in launching of some other service. In this tutorial, we find a few methods that can be used to kill the process running on a particular port in Linux either gracefully or forcefully depending upon the scenario.

You can use this tutorial for Ubuntu 22.04/20.04/19.04, Debian 12,11,10, Linux Mint, RedHat, Rocky Linux, Amazon Linux, AlmaLinux, and more…

Identifying the Process using Top, Netstat, or lsof

Well, if you are planning to kill some process you should at least know its ID, so before knowing the methods to terminate a process let’s learn two commands that we can use in our Ubuntu Linux to identify the running process IDs (PID) associated with listening ports.

Using Top to find Active Process ID (PID):

To find the process along with the services running on it users can use the TOP utility:

top

It will list all the active processes, use the down arrow key to scroll the list and find what you are looking for.

use to TOP to identify process ID

Using netstat

The other most common command is netstat, not only in Linux we can also use it in Windows to find Ports and PID numbers.

To list all active Intenet ports:

sudo netstat -tulpn

or alternatively, you can use:

ss

Whereas, if you already know the port number that process ID you want to know then use:

sudo netstat -tulpn | grep :your_port_number

Replace your_port_number with the exact port number you want to find and the command will reveal the process ID.

Using lsof:

Just like netstat, we can use another command “lsof” to find a particular port’s process ID.

sudo  lsof -i :your_port_number

See the screenshot to get a clear idea.

netstat tp list ports in linux

Way 1: Graceful process Termination with Kill

Now, that you have identified what is the PID of the service or port you want to end, we can use the KILL command to gracefully terminate that process.

Syntax:

kill <PID>

Replace <PID> in the above-given syntax with the actual process ID that you want to end. This command will send a SIGTERM signal to the process, allowing it to perform cleanup operations before termination.

Example:

kill 631
Graceful linux process Termination with kill

Way 2: Forceful Termination with kill

In case even after running the KILL command the process doesn’t end or respond to the SIGTERM signal then we can use the “-9″ option to issue a SIGKILL” signal. It will forcefully end the process.

kill -9 <PID>

Again replace <PID> with the actual process ID, however, this command will not perform the cleanup task instead immediately ends the process, so use it with caution.

Way 3: pkill Command

Pkill is just like kill but available with a wider range of options to use. Even we can use the process names to send kill signals.

Syntax:

sudo pkill -f "<process_name>"

Replace <process_name> with the name of the process you want to terminate. This can be useful when you know the process name but not the PID.

For Example, instead of using process ID, you want to use the name. Let’s say you want to end the mysql process.

sudo pkill -f mysqld

Way 4: fuser Command

So, far we have discussed two command tools to terminate the process using its ID or name. Now, what if you want to end the process by specifying the port number? For that, we have the “fuser” command, which can be used to identify and terminate processes using a specific port.

Syntax:

sudo fuser -k your_port_number/udp

Replace your_port_number with the actual port number. Let’s say you want to kill the service or the process that uses Port number 80 then the command using fuser will be like this:

sudo fuser -k 80/tcp 

This sends a SIGKILL signal to the processes using the specified port.

If the above given command does not kill the process then instead of relying on the default signal, use the -TERM option:

fuser -k -TERM port/udp or fuser -k -TERM port/tcp

example:

sudo fuser -k -TERM 80/tcp

Way 5: Using killall

Another way to end the process is by using the “killall” command which can also terminate processes by name.

Syntax:

sudo killall <process_name>

Replace <process_name> with the name of the process to be terminated. This kills all instances of the specified process.

Example:

sudo killall apache2

Conclusion:

We have seen multiple commands to easily manage and terminate processes properly on Ubuntu Linux using the port number to process ID, and if necessary, forcefully killing processes can also be done. You can choose the method or command that suits your needs and can be easily remembered. However, always first try to end the process using SIGTERM term signal but if the process is not responding then SIGKILL. But exercise caution, especially during forceful termination of the process, to avoid unintended consequences.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.