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:
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.
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.
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:
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:
The “-k” option is to kill the processes. Replace the “port_number” and “tcp” with the actual port number and the protocol.
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.