Hello guy, Today we are going to learn How to Kill a Process on a Port in Ubuntu. In the world of system administration and software development, there are times when you encounter a stubborn process running on a specific port that needs to be terminated. This situation might arise due to various reasons, such as port conflicts or misbehaving applications.
In this tutorial, we’ll guide you through the process of killing a process on a specific port in Ubuntu.
Also Read: Resolving “Unable to Locate Package” Error in Ubuntu
Why Would You Need to Kill a Process on a Port?
Before we delve into the process, let’s understand why you might need to terminate a process on a particular port. Multiple processes might attempt to use the same port, leading to conflicts and rendering the port unusable.
Additionally, certain applications might become unresponsive or hang, necessitating their termination to restore system stability.
Whatever the reason, knowing how to effectively terminate a process on a specific port can be a valuable skill.
Identifying the Process and Port
The first step is to identify the process and port that need to be terminated. To do this, you can use the netstat
or ss
command, which provides information about active network connections and listening ports.
Open a terminal and execute either of the following commands:
sudo netstat -tuln | grep PORT_NUMBER
or
sudo ss -tuln | grep PORT_NUMBER
Replace PORT_NUMBER
with the actual port number you want to investigate. This command will display information about processes associated with the specified port.
Also Read: How to install composer using command line in Ubuntu?
Terminating the Process
Once you’ve identified the process using the desired port, you can terminate it using the kill
command. Here’s how:
- Obtain the Process ID (PID): Note the process ID (PID) of the process you want to terminate. The PID can be found in the second column of the output from the
netstat
orss
command. - Use the
kill
Command: In the terminal, use thekill
command followed by the PID to terminate the process. For example, to terminate a process with PID 1234:
sudo kill 1234
- Forceful Termination: If the process is unresponsive to the regular
kill
command, you can use the-9
option to forcefully terminate it:
sudo kill -9 1234
Checking Termination Status
After terminating the process, you can re-run the netstat
or ss
command to ensure that the process on the specified port has been successfully terminated.
If the process was successfully terminated, the port should no longer appear in the list of active connections or listening ports.
Also Read: How to Install CyberPanel on Ubuntu 18.04 LTS: A Step-by-Step Guide
Conclusion
Terminating a process on a specific port is a useful skill for system administrators and developers working with Ubuntu.
By following these steps, you can identify and terminate processes that might be causing port conflicts or other issues.
Remember to exercise caution when using the kill
command, especially with the -9
option, as forcefully terminating a process can lead to data loss or instability.
Always prioritize graceful termination methods before resorting to a forceful approach.