Linux Commands

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

Sometimes, while working with your Linux system, you might encounter a situation in which you want to kill an already running process. Let us assume that you do not know the process ID of that process but you do have the knowledge of the specific port on which that process is running. Therefore, in this guide, we will share with you the methods of killing a process that is running on a particular port on a Linux Mint 21 system.

Methods of Killing a Process Running on a Particular Port in Linux Mint 21

To kill a process that is running on a specific port on a Linux Mint 21 system, you can follow any of the following two methods:

Method 1: Using the Kill Command

In this method, we will make use of the kill command to kill a process that is running on a specific port as follows:

Step 1: Finding the Process ID of the Process to be Killed

We first need to find the process ID of the process that we wish to kill. We can find this out by running the following command:

$ sudo lsof –t –i:48744

This command simply lists down the process IDs of all the processes that are running on the specified port. You can replace “48744” with a port number of your choice on which your target process is running.

After executing this command, you will be able to see the process ID of the process[s] that are running on the specified port as shown in the following image:

Step 2: Killing the Target Process

Now, by making use of this process ID, we will kill this process in the following manner:

$ sudo kill -9 561

You can replace “561” with the exact process ID of the process that you wish to kill. After executing this command, the specified process is killed instantly.

Shortcut of Method 1:

If you want to merge the two steps used in the first method and make it even more efficient, you can simply use the following command to kill a process that is running on a specific port:

$ sudo kill -9 $(sudo lsof –t –i:48744)

Again, you can replace “48744” with some other port number on which your process to be killed is running. This command performs the exact same task that we intend to do using the given two-step method.

Method 2: Using the Fuser Command

You can also use the fuser command to kill a process that is running on a specific port in the following manner:

$ fuser –k –n tcp 48744

Here, “tcp” represents the protocol that the specified port uses. You can even replace it with “udp” if it is followed by the port number of a UDP port.

Shortcut of Method 2:

However, if you find the previously mentioned command a bit too lengthy, you can easily shorten it in the following manner and it serves the exact same purpose as in the previous method:

$ fuser –k 48744/tcp

Conclusion

In this way, by simply knowing the port number of the port on which a particular process is running, you can easily kill that process. You can pick either of the two methods that are previously discussed to achieve this goal.

Similar Posts