How to kill process by port

Processes that utilize the network connection of your Linux system will occupy a port whenever they are uploading or downloading data, or listening for incoming connections. A common example present on many Linux servers would be the SSH protocol, which listens for and accepts incoming connections on port 22 by default. Ordinarily, administrators can kill a process using the PID number, or by specifying the process name. But in some cases, we may want to kill a process according to which port number it is using.

Returning to our previous example, killing a process running on port 22 would end whichever process is utilizing that port – which, by default, would be the SSH daemon. In order to kill a process by port number, we need to combine numerous Linux tools like fuser and the kill command to complete the task. Let’s see just how to do that. In this tutorial, you will learn how to kill a process based on port number in a Linux system.

In this tutorial you will learn:

  • How to kill process by port number with fuser
  • How to use ss and lsof to see processes using a port number
  • How to use socat to create a dummy process for testing
How to kill process by port
How to kill process by port
Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Any Linux distro
Software kill, fuser, xargs, ss, lsof, socat
Other Privileged access to your Linux system as root or via the sudo command.
Conventions # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – requires given linux commands to be executed as a regular non-privileged user

Kill process by port number

Check out some of the different Linux commands that we can use to kill a process by port number below. To make a realistic scenario for testing these commands, refer to the section on socat below.

  1. For processes listening on a TCP or UDP port, the fuser command along with the -k (kill) option will terminate the related processes for you. Just specify the port type (TCP or UDP) and the port number in your command. For example, this would terminate processes utilizing TCP port 80.
    $ fuser -k 8080/tcp
  2. Or to kill a process on UDP port 8080 with fuser:
    $ fuser -k 8080/udp
  3. If you do not want to use fuser, it is possible to find the process IDs that are utilizing a port number via the lsof command and then pass that data to the kill command. For example, this will terminate all processes using TCP port 8080.
    $ lsof -i tcp:8080 | awk '/8080/{print $2}' | xargs kill
  4. To terminate a process using a different protocol such as SCTP, we can use the ss command and pipe the PID to xargs and kill command. For example, the following command would kill all processes using SCTP port 8080.
    $ ss -Slp | grep -Po ':8080\s.*pid=\K\d+(?=,)' | xargs kill

Identify which port a process is using

In the above examples, we used the ss command and lsof command to determine which process ID numbers were utilizing the ports we specified.

After running the commands in the examples above, it is good practice to run the lsof or ss command once more to ensure that the process has terminated as expected. If not, you can try using the -9 (SIGKILL) option with the kill command.

Check port with lsof

To see a list of which ports are being used on our system, the lsof command comes in handy. For example, the following command will list information about which process or processes are utilizing TCP port 80.

$ sudo lsof -i TCP:80

Along with other information, the lsof command gives us the process ID of the processes utilizing the specified port. It will also work on UDP ports.

Check port with ss

Another command that can be used to see which processes are using a particular port is the ss command. Some users may prefer it over lsof, but personally we find lsof a bit easier to use for this situation. However, ss is able to list processes that are using other protocols, such as SCTP ports.

$ ss -Slp

The output from the command above will show all processes and ports being used with SCTP.

Using these two tools will help us to determine which process ID is running on a certain port, and will also come in handy to pass those process IDs over to the kill command as seen in some of the previous examples.

Testing commands with socat

In order to make testing the above commands easier, we can use the socat command to create a dummy process that binds itself to a port of our choosing.

  1. Bind a process to TCP port 8080:
    $ socat tcp-listen:8080,bind= stdout &
  2. Bind a process to UDP port 8080:
    $ socat udp-listen:8080,bind= stdout &
  3. Bind a process to SCTP port 8080:
    $ socat sctp-listen:8080,bind= stdout &

These examples will put your process into the background. Then, we can use the commands seen above to test terminating the processes.

Closing Thoughts

In this tutorial, we saw how to kill a process by the port number that it is listening on in a Linux system. The fuser command is the main tool we would use for this job, but Linux is known for offering users more than one method to accomplish a task. As alternatives, the lsof and ss commands help us ascertain the information we need, and in conjuction with the kill command, can give the same effect as fuser, while also expanding its functionality to accommodate various scenarios.

Comments and Discussions
Linux Forum