Jump to content

How to Kill a Process Running on Port in Linux


Linux Hint

Recommended Posts

There are precisely 65536 ports in a device, all of which can simultaneously handle one network request. Despite being virtual, they are critical to your device’s functionality. They combine with your IP address and aid in establishing the client-server connections which is the fundamental need for accessing any website or service.

Moreover, Linux applications block a port for themselves whenever they communicate with their servers. It occupies a port for that process. For various reasons, you may sometimes need to terminate the process to save the storage and improve the system performance. However, if you do not know how to keep reading this blog, we will briefly explain how to kill a process that is running on a port in Linux.

How to Kill a Process that Is Running on a Port in Linux

There are two simple yet effective methods that you can use to kill the processes that are running on ports in Linux. We further divided this section into multiple parts to explain both of the methods concisely.

1. Normal Method

Before entering the process termination command, it is a good practice to verify/identify its process ID or PID. Use the following command to check the processes that are queued on a particular port:

sudo lsof -i : port_number

Replace the “port_number” with the port that you want to check for processes. Upon entering this command, it displays an information about the processes like their PID and some other details.

After the verification, let’s kill the process using the “pkill” command.

sudo pkill -f process_name

Replace the “process_name” with the name of the process. The “-f” option searches across the command line to find matches.

Furthermore, if you intend to terminate a process immediately, use the “kill” command.

sudo kill -9 PID

Replace “PID” with the actual PID that you want to kill. The “-9” option sends the killing signal to terminate the targeted process. Remember that using this option forces the process to close and does not allow it to perform any exit or cleanup operations.

2. The “Fuser” Command

Moreover, an alternative method to the previous one is using the “fuser” command. It is another powerful command line tool that you can use to identify the processes that take place on a specific port. Its syntax is as follows:

fuser -v -n tcp port_number

Again, replace the “port_number” with your intended port number. We use “tcp” here to specify the command to list the processes that use the TCP internet protocol. If you want to see for UDP, use “udp” instead.

Upon entering this command, it shows you the processes using the mentioned port number as shown in the previous image. To kill them, use the following command:

sudo fuser -k port_number/tcp

The “-k” option is to kill the processes. Replace the “port_number” and “tcp” with the actual port number and the protocol.

on-Port-in-Linux-1.png

Conclusion

Terminating the processes that run on a specific port is a fundamental task in Linux. Therefore, in this short blog, we briefly explained how to use the commands such as “lsof” to find the PIDs and “pkill” or “kill” to end the process. Alternatively, the “fuser” command can also identify and terminate the processes on a particular port. These commands provide options to effectively manage and stop the processes associated with specific ports in a Linux environment.

View the full article

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...