How to kill process based on the port number in Linux

At any given moment your Linux system is running multiple processes simultaneously. Some of these processes have access to your network if they are being used to upload or download data. These processes typically bind themselves to a particular port number, and this can allow us to kill the process based on a port number.

The kill command is one way that system administrators can stop a process from running. However, the kill command only accepts a process ID as an argument. The pkill and killall commands are two more options, but these accept process names as arguments.

In order to kill a process based on its port number, we will need to use the fuser command, or use other command line tools in conjunction with the usual kill command. In this tutorial, we will show you multiple ways to kill a process based on its port number in Linux.

In this tutorial you will learn:

  • How to kill a process on a TCP or UDP port with fuser
  • How to kill a process on an SCTP port with kill
  • How to view what process a port is using with ss and lsof
  • How to bind a process to a port using socat for testing purposes
How to kill process based on the port number in Linux
How to kill process based on the port number in Linux
Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Any Linux distro
Software fuser, kill, lsof, ss, xargs, 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

How to view which process is using a certain port

As mentioned earlier, a process listening for incoming connections is going to bind itself to a port. Most processes will always use the same port, unless they have been configured to use a non-default one. For example, SSH uses port 22, HTTP uses port 80, and MySQL uses port 3306, etc. With this knowledge, we are able to figure out which port a service is operating on.

lsof command

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. To see more information about how to use lsof, check out our tutorial on Guide to lsof Linux command with examples.

ss command

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. To see more information about how to use ss, check out our tutorial on Using ss command on Linux.

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 in some of the following examples.

Bind a process to a port with socat

In order to make testing the below 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 below to test terminating the processes.

Kill process based on the port number examples

  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

    Remember to use the lsof command afterwards to confirm that no process is using the port.

  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 exampe, the following command would kill all processes using SCTP port 8080.
    $ ss -Slp | grep -Po ':8080\s.*pid=\K\d+(?=,)' | xargs kill

Closing Thoughts

In this tutorial, we saw how to kill a process based on the port number it’s using on 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.

Comments and Discussions
Linux Forum