How to Kill Process Running on a Specific Port in Linux?

28-Apr-2023

.

Admin

How to Kill Process Running on a Specific Port in Linux?

Hello Friends,

In this short tutorial we will cover a how-to kill process running on a specific port in linux. This tutorial will give you a simple example of how to kill a process running on a particular port in linux. We will look at an example of how to kill a process based on the port number in linux. we will help you to give an example of a kill process running on a specific port in linux.

In Linux, a process is a running instance of a program, which can communicate with other programs or users through network sockets assigned to specific ports. Sometimes, it may be necessary to kill a process running on a specific port either because it is consuming too many resources or interfering with other processes.

In this tutorial, you will learn three methods for killing a process running on a specific port in Linux Ubuntu.

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

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 the 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 ls 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

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.

I hope it can help you...

#Ubuntu