In the Linux Ubuntu system, to kill a process you have to first find the process, which you can do with sudo lsof -i :<port/pid/name>
command, after that, you have to type sudo kill <PID/port/name>
command on the terminal window or command line to kill process by specific name, pid, port, user, etc.
Methods to kill process running on a specific port, name, pid in Linux Ubuntu via terminal or command line window:
- 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
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: Command to kill the process
Once you have identified the process ID, you can use the sudo kill <PID>
command to terminate the process:
sudo kill <PID>
Replace <PID> with the process ID obtained in the previous step.
Conclusion
In this tutorial, you have learned three methods on how to identify and kill process running on a specific port in Linux ubuntu via terminal window or command line.