Jan 10, 2023 2 min read

Kill Process Running on a Specific Port in Linux

Table of Contents

Killing a process in Linux usually involves using the process ID with the kill command. You can also use the process name with killall command.

This works when you know the process ID or name. But in some cases, you may not know it or you just don't necessarily need to know it.

For example, if you want to kill the processes running on specific ports, you may not need to know the process ID or name at all. You can terminate a process based on the port number it is using.

Let me show you how to do that.

Terminating processes based on their port numbers

The fuser command combined with the -k (kill) option will end all associated processes that are listening on a TCP or UDP port. Simply provide the port number and type (TCP or UDP) in the fuser command.

For instance, to end a process on UDP port 81, use the fuser command as:

sudo fuser -k 81/udp
Killing processes on a specific port

Similarly, use the fuser command to terminate a process on TCP port 3306:

sudo fuser -k 3306/tcp

You can use the lsof command to verify that processes are no longer running on the target port.

The classic way of killing processes using certain ports

For those who would rather not use fuser, lsof may be used to determine which processes are using a certain port and then use this information with the kill command.

As an instance, to kill all the process running on TCP port 3306, use the below command to detect the process id:

sudo lsof -i TCP:3306

And now kill it with its pid:

 kill -9 <pid_value>

One thing to note here is that certain processes like mysqld and apache2 might restart after you have killed them using the above commands. Even if you use the killall command, they will still appear after some time.

In such cases, I advise you to use application specific commands to stop a service. For example, to kill the Apache process on Ubuntu, use the command:

sudo systemctl stop apache2
Killing processes in standar procedure

This will completely kill the particular process.

Wrapping Up

This article covered how to terminate a process running on a specific port. With the fuser command, you don't need to know the process details. Otherwise, the classic method of getting the process ID associated with port first and then killing it works as well.

Team LHB
Team LHB indicates the effort of a single or multiple members of the core Linux Handbook team.
Great! You’ve successfully signed up.
Welcome back! You've successfully signed in.
You've successfully subscribed to Linux Handbook.
Your link has expired.
Success! Check your email for magic link to sign-in.
Success! Your billing info has been updated.
Your billing was not updated.