How to Kill a Linux Process Using a Port Number
But be very careful when doing it
Have you ever encountered a situation where you need to stop a process that’s running on a specific port on your Linux machine?
You might be worried that an application you’re running is misbehaving or hogging up your network resources or local CPU power. Such an application could even be a security threat you want to eliminate.
Whatever the reason is, you can use thefuser,lsof, andnetstatcommands from the terminal (along withkill) to help you find and kill any Linux process using a port number. These commands will work on most Linux operating systems, including Ubuntu.
How to Kill a Linux Process Using the fuser Command
One of the easiest ways to kill a process using a port number is to use thefusercommand. ThisLinux terminal commandcan list or kill processes accessing files or sockets. If an app runs with network activity (and thus uses an open port), it’s probably doing one of these two things.
Before you proceed, you’ll need to make sure you’re comfortable using a command line terminal on your Linux PC.
To list the processes that are using a port number usingfuser, you can use the following syntax:
fuser -n protocol port
In this example,protocolcan betcporudp, whileportis the port number you want to check. For example, to see which processes are using TCP port 80, you can run:
fuser -n tcp 80
This will print the process IDs (PIDs) of the processes using that port.
To kill those processes, you can add the-koption tofuser. This will send a SIGTERM signal to each process, asking them to terminate gracefully. For example:
fuser -k -n tcp 80
This will kill all processes using TCP port 80.
If some processes don’t respond to SIGTERM, you can use-KILLinstead of-k. This will send a kill signal (SIGKILL), which can help to force the currently running processes you’ve identified to terminate immediately. However, this may cause data loss or corruption, so use it cautiously. For example:
fuser -KILL -n tcp 80
This will kill all processes using TCP port 80 forcefully. If you run into any issues, run these commands as the super user (using thesudocommand) or by using therootuser instead.
How to Kill a Linux Process Using the lsof Command
Another way to kill a process using a port number on a Linux PC is to use thelsofcommand. This command can list open files and sockets on your system.
To list the processes that are using a specific port number, you can use the following syntax:
lsof -i protocol:port
As before,protocolcan betcporudp, whileportis the port number you want to check. For example, to see which processes are using TCP port 53 (typically used for DNS requests), you can run this command:
lsof -i tcp:53
This will print some information about each process using that port, including its PID.
To kill those processes, you can use the-toption withlsof. This will only print the PIDs of the processes without any other information. You can then pipe this output to thekillcommand with any signal option. For example:
kill $(lsof -t -i tcp:53)
This will send SIGTERM signals (the default) to all processes using TCP port 53.
If some processes don’t respond to SIGTERM signals as before, you can use-9instead of nothing afterkill. This will send SIGKILL signals as before, forcing them to terminate immediately but may also cause data loss or corruption. For example:
kill -9 $(lsof -t -i tcp:53)
This will forcefully send SIGKILL signals (the default) to all processes using TCP port 53.
How to Kill a Linux Process Using the netstat Command
You can also use thenetstatcommand to track down running processes on your Linux PC using active and open network ports. netstat lets you view network connections and statistics on your system, allowing you to pinpoint problematic processes.
To list the processes using a port number along with their PIDs, you need to add two options:-p, which shows PIDs, and-l, which shows listening sockets only.
You also need to specify the protocol (tcp, udp, etc.) and optionally filter by state (LISTEN, etc.). For example, to see which TCP processes are listening on any ports, you can run:
netstat -p tcp -l
This will print information about each TCP socket listening on any ports, including its PID.
To filter by specific ports, you must add another option:-n, which shows numerical addresses instead of names. You also need to specify the exact address format:[protocol][@hostname|hostaddr][:service|port].
For example, to see which TCP processes are listening on port 80, you can run:
netstat -p tcp -l -n 80
This will print information about each TCP socket listening on port 8080, including its PID.
To kill those processes, you can use thekillcommand with any signal option you want and the PIDs you obtained from netstat. For example:
kill 1234 5678
This will send SIGTERM signals (the default) to processes with PIDs1234and5678.
If some processes don’t respond to SIGTERM signals as before, you can use-9instead of nothing afterkill. This will send SIGKILL signals as before, forcing them to terminate immediately but may cause data loss or corruption as before. For example:
kill -9 1234 5678
This will send SIGKILL signals to processes with PIDs1234and5678forcefully, for instance. Replace1234with the correct PID for your running process.
Control Your Linux Applications
Thanks to the steps outlined above, you can quickly kill a running Linux process using a port number using the Linux command line. Remember to use caution when killing processes, especially with SIGKILL signals, as they may cause unwanted side effects—you don’t want to cause system instability, after all.
Want to look more closely at your system performance on Linux? You may want tocheck your memory usage on Linuxnext. If you’re worried about a potential security risk, you may want tochange your password on Linux, too.
Need to switch to a new Linux distribution? If you’re a beginner,consider Linux Minta safe and stable alternative.
Ben Stockton is a freelance technology writer based in the United Kingdom. In a past life, Ben was a college lecturer in the UK, training teens and adults. Since leaving the classroom, Ben has taken his teaching experience and applied it to writing tech how-to guides and tutorials, specialising in Linux, Windows, and Android. He has a degree in History and a postgraduate qualification in Computing.Read Ben’s Full Bio
Welcome to Help Desk Geek- a blog full of tech tips from trusted tech experts. We have thousands of articles and guides to help you troubleshoot any issue. Our articles have been read over 150 million times since we launched in 2008.
HomeAbout UsEditorial StandardsContact UsTerms of Use
Copyright © 2008-2024 Help Desk Geek.com, LLC All Rights Reserved