In Linux, a process is a running instance of a program, and it can communicate with other programs or users through network sockets that are assigned to specific ports. Sometimes, you may need to kill a process that is running on a specific port, either because it is consuming too many resources or because it is interfering with other processes. In this tutorial, you will learn 3 methods on how to kill a process running on a specific port in Linux ubuntu.
How to Kill Process on Specific Port Ubuntu Linux
In some situations, it becomes necessary to kill the process running on that port. By the following methods, you can easily kill process running on a specific port in linux ubuntu:
- Method 1: Using the netstat Command
- Method 2: Using the fuser Command
- Method 3: Using the kill Command
Method 1: Using the netstat Command
The netstat command is a network utility tool used to display network connections and related statistics. It can also be used to identify the process running on a specific port.
Here’s how to use the netstat command:
- Step 1: Identify the Process Running on the Port
- Step 2: Kill the Process
- Step 3: Verify the Process is Terminated
Step 1: Identify the Process Running on the Port
The first step in killing a process running on a specific port is to identify the process ID (PID) of the process. You can do this by using the netstat
command to list all the processes that are using network sockets, and then filter the output to show only the processes that are using the port you want to kill. For example, to find the PID of the process using port 8080, run the following command:
sudo netstat -nlp | grep :8080
The output will show the PID of the process running on port 8080, along with the program name and other information:
tcp6 0 0 :::8080 :::* LISTEN 1234/java
In this example, the PID is 1234, and the program name is java
.
Step 2: Kill the Process
Once you have identified the PID of the process, you can use the kill
command to send a signal to the process to terminate it. The default signal sent by the kill
command is SIGTERM
, which asks the process to terminate gracefully. If the process does not respond to SIGTERM
, you can use the SIGKILL
signal, which forcefully terminates the process.
To terminate the process gracefully, run the following command:
sudo kill PID
Replace PID
with the PID of the process you want to terminate. In our example, the command would be:
sudo kill 1234
If the process does not respond to SIGTERM
, you can send the SIGKILL
signal by adding the -9
option to the kill
command, like this:
sudo kill -9 PID
Again, replace PID
with the PID of the process you want to terminate. In our example, the command would be:
sudo kill -9 1234
Step 3: Verify the Process is Terminated
After you have sent the signal to terminate the process, you can verify that the process has been terminated by running the netstat
command again and checking if the port is still in use. If the port is no longer in use, the process has been successfully terminated.
Method 2: Using the fuser Command
The fuser command is another tool that can be used to identify and kill processes running on a specific port. It shows the PIDs of the processes that are currently using the specified file or socket.
Here’s how to use the fuser command:
- Step 1: Use the fuser command to find the PID
- Step 2: Run kill command
Step 1: Use the fuser command to find the PID
Use the fuser command to find the PID of the process running on the specific port:
sudo fuser -k <port number>/tcp
The -k option tells fuser to kill the processes using the specified port. Replace <port number> with the port number of the process you want to kill.
Step 2: Run kill command
Once you have identified the process ID, you can use the kill command to terminate the process:
sudo kill <PID>
Method 3: Using the kill Command
The kill command is a common method used to terminate a process in Linux. To use this method, we need to know the process ID (PID) of the process running on the specific port. We can use the lsof (list open files) command to obtain this information. The lsof command shows all the open files and processes associated with them.
Here’s how to use the kill command:
- Step 1: Find the process ID
- Step 2: Run kill command to terminate the process
Step 1: Find the process ID
Find the process ID of the process running on the specific port using the lsof command:
sudo lsof -i :<port number>
This command will show all the processes running on the specified port along with their process ID.
Step 2: Run kill command to terminate the process
Once you have identified the process ID, you can use the kill command to terminate the process:
sudo kill <PID>
Replace <PID> with the process ID obtained in the previous step.
Conclusion
Killing a process running on a specific port in Linux is a crucial task that can help resolve issues related to unresponsive or malfunctioning processes. In this tutorial, you have learned three methods to identify and terminate processes running on a specific port in Linux. These methods include using the kill command, fuser command, and netstat command. It is essential to use these commands carefully as killing a process can have severe consequences on the system’s stability. You should only use these commands if we are confident that the process is causing issues and needs to be terminated. Overall, these methods can be extremely useful in troubleshooting and resolving issues related to processes running on specific ports in Linux.