Kill Process on Port: How to Do it in Linux

kill process on port
kill process on port

In Linux (and other Unix based operating systems as well), there can be instances where a process runs on a port which prevents other applications from using the same port. When you face situations like these, it is essential to free up that port by killing the process on that port. In this article, let us see the instructions on how to kill a process on port without any complications to any process.

How to Find the Process ID (PID) Using the Port

In Linux, every process is assigned a process ID (PID). To kill a process, we need to identify the process ID of the process. This is the prerequisite before we kill process on port. To get this information, we can use either the lsof command or netstat command.

How to Use the lsof Command

The primary use of the lsof command is to open files and network connections. To know which process is using a specific port, execute this command:

lsof -i :PortNumber

For example, if you would like to know the process using port 443, execute the command:
lsof -i :443

Once this command is executed, the terminal will display the details of the process, and it includes the process ID as well.

How to Use the netstat Command

This method is an alternative to the lsof command we just learnt above. To know the process ID of the process using a port, execute this netstat command in the command terminal:

netstat -tuln | grep :PortNumber

Once this command is executed, the terminal will display the process that is listening on the specified port number.

As an additional information, you can also use the ss command which is more efficient and newer when compared to netstat. To do this, execute this command:

ss -ltnp | grep :PortNumber

How to Kill the Process

In the previous step, we have the process ID of the process running in the port. Once we have the PID, we can kill the process using the “kill” command. For example, if the process ID using the port 443 is 1234, run this command to kill the process.

kill -9 1234

You might have noticed the “-9” argument. That option sends the “SIGKILL” signal, which forces the process to terminate immediately.

How to Check if the Process is Killed Successfully

Sometimes, the process might still run even after we have clearly executed the command for force termination. To check if the task has been killed successfully, you can use the lsof command again. For example, executing the below command should not return any output, if the process using the port has been killed successfully. If the process is still active, repeat the previous step again.

lsof -i :PortNumber

How to Find the Process ID and Kill It in One Step

In Linux, it is possible to create a one-line command to automatically find and kill process on port. For example, to kill process on port 443, execute the command:

kill -9 $(lsof -t -i :443)

This command uses both lsof and kill in one go. Using lsof -t, it finds the process ID and the kill command kills the process ID.

A Practical Use Case

In a work environment, a common use case to kill process on port is when you need a port but that port has already been occupied by some other process. Say a web server like Apache or Nginx is using port 80. The testing team has deployed the test server on the same port. This creates conflicts and prevents both applications from running smoothly. In some cases, this unexpected behavior of both these applications will be attributed to unavailable port after considerable amount of time, wasting a lot of human effort and time. To resolve this issue, the system administrator finds the conflicting processes, finds the process ID, kills it, and restarts the application with higher priority.

Wrapping Up

Ports are the gateways to systems and servers. That is the path via which applications and servers communicate to the outer world. When one application hogs up one port, it makes it difficult for another high priority process to use the same port, resulting in malfunctioning. This is where knowledge on kill process on port comes to your help. Whether you are a developer trying to restart a web server or an SRE managing network resources, knowing how to promptly identify and kill a process occupying a port is essential for efficient system management.

Related Link

Appendix E. Identity Management Server Ports Considerations | Red Hat Product Documentation

Some More Articles that would be of Interest to You