Home Linux Commandline Tools How to Kill Running Linux Process on Particular Port

How to Kill Running Linux Process on Particular Port

In Linux, processes and ports have a symbiotic relationship. One cannot exist nor function without the other. Processes tend to share ports and some processes rely on a specific port to meet an operating system objective.

This article guide gives you a walkthrough on how to free a port used by one process in order to use it on another process.

How to List a Port with its Associated Process

If you can list a targeted port and a process PID, you can kill the process associated with it. There are several tools you can use to list actively used ports in Linux.

A more flexible recommendation or approach is through the network statistics (netstat) command or ss command (faster than netstat). It is a default installation in most Linux distributions but you can install it with the following command if it’s not present.

Install Netstat and SS Command in Linux

$ sudo apt-get install net-tools iproute   [On Debian/Ubuntu & Mint] 
$ sudo dnf install net-tools iproute       [On CentOS/RHEL/Fedora and Rocky Linux/AlmaLinux]
$ pacman -S netstat-nat iproute            [On Arch Linux]
$ emerge sys-apps/net-tools iproute        [On Gentoo]
$ sudo dnf install net-tools iproute       [On Fedora]
$ sudo zypper install net-tools iproute    [On openSUSE]

To list a port and find out if it is locked to a particular process or service, adhere to the following syntax rule.

$ sudo netstat -ltnp | grep -w ':[targeted_port]'
OR
$ ss -ltnp | grep -w ':[targeted_port]'

For example, processes or services listening to port 80 can be listed in the following manner.

$ sudo netstat -ltnp | grep -w ':80'
OR
$ ss -ltnp | grep -w ':80'
List Port with Process ID
List Port with Process ID

Under the command string “netstat -ltnp”, the option -p points to 1520 which is the PID of the process you wish to kill. Take note of it.

How to Create Dummy Process in Linux

So as not to mess with the ecosystem of your Linux operating system, we will create some dummy processes and use them to demonstrate the objective of this article. We will use the “socat” command-line utility to create our demo processes.

Let’s have three processes under SCTP, TCP, and UDP protocols respectively. We are going to bind these new processes to a random port like 9999.

The following command will generate a temporary process with a PID (Process ID) of 8695.

$ socat sctp-listen:9999,bind=127.0.0.1 stdout &
Bind Process to Port
Bind Process to Port

The following command will generate a temporary process with a PID (Process ID) of 9080.

$ socat tcp-listen:9999,bind=127.0.0.1 stdout &
Create Temporary Process with PID
Create Temporary Process with PID

The following command will generate a temporary process with a PID (Process ID) of 9270.

$ socat udp-listen:9999,bind=127.0.0.1 stdout &
Create Temporary UDP Process with PID
Create Temporary UDP Process with PID

How to Kill a Process with its PID (Process ID)

Using the "kill -9" command, we can get rid of a process that is holding hostage a port number we want to use on another prioritized process. In this case, we created three processes with PIDs 8695, 9080, and 9270.

To kill the process under PID 8695, I ran the following command:

$ kill -9 8695

We can even kill more than one process at a go. To kill the created processes with PIDs 9080 and 9270, use a command similar to this one:

$ kill -9 9080 9270

Whether you are killing one process or more than one process at the same time, the execution of the kill command should lead to an empty terminal instance as depicted above.

To confirm that the processes are no longer active, we will try to re-run the kill command:

$ kill -9 8695
Kill Linux Process with PID
Kill Linux Process with PID

An error message will indicate that the process is no longer active. Also, the two recently killed processes [2] 9080 and [3] 9270 are showcased as no longer active.

Final Note

The rules of killing a running process are simple. First, identify the port you want to use through the “netstat” or “ss” command and then take note of the PID attached to the process. Finally, Use the “kill -9” command together with the identified PID to get rid of that process.

Ravi Saive
I am an Experienced GNU/Linux expert and a full-stack software developer with over a decade in the field of Linux and Open Source technologies. Founder of TecMint.com, LinuxShellTips.com, and Fossmint.com. Over 150+ million people visited my websites.

Each tutorial at UbuntuMint is created by a team of experienced writers so that it meets our high-quality standards.

Was this article helpful? Please add a comment to show your appreciation and support.

Got something to say? Join the discussion.

Thanks for choosing to leave a comment. Please keep in mind that all comments are moderated according to our comment policy, and your email address will NOT be published or shared. Please Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.