This guide will show you how to find and kill a process that uses a particular port on your Linux operating system.
While setting up a new service on your Linux system, you may run into any issue when a port is already in use. While you could just change the port your new software is running on, this isn’t always the best choice.
Luckily, Linux makes it reasonably easy to work out the names and IDs of processes using a specific port.
By working out the processes using a port, you can kill that process and free it up so that another program can operate on it. It can also give you an idea of software you have set up previously and may need to remove.
This whole process is made relatively easy as almost all Linux operating systems have all the tools you need baked into their core. This includes popular distributions such as Ubuntu, Debian, and Fedora.
By the time you have got to the end of this tutorial, you should know how easy it is to work out what process is using a port on your system.
Of course, for all of the steps we cover in the guide, you will need to use the terminal. Many desktop Linux distributions allow you to open the terminal using the CTRL + ALT + T keyboard shortcut.
Using the Terminal to Find and Kill a Process Running on a Port
Over the following sections, we will walk you through a quick process you can follow to find and kill a process consuming a specific port on your Linux machine.
You can skip the first two sections if you prefer a simple one-liner instead of understanding the process. These two first sections show you how to use the “lsof
” command to find a process using a port, and then how to use the kill or pkill commands to free up that port.
Finding a Process Using a Port on Linux
1. To find a process operating on a particular port on Linux you will want to use the lsof command. While designed to check for open files on your system, this same command can also be used to get what ports are being used and by what.
The basic syntax is to use “lsof
” followed by the “-i
” option and then the port you want to check.
lsof -i :<PORT>
The lsof command will only show the processes that are using a port if your user has rights to it. That is, of course, unless you are running as the superuser.
To get a list of processes that are using a port on Linux regardless of the user, make sure you run the command using “sudo
” or running as the root user.
sudo lsof -i :<PORT>
2. For this example, we are going to use the terminal to list the processes that are listening on port 80 of our Linux machine.
All we need to do is use the command we mentioned in the first step. Since we aren’t sure whether this application is running under our current user, we will use “sudo” to elevate to the super user.
sudo lsof -i :80
3. After running the above command, you will be presented with a list of processes running on that given port. You may see multiple processes here, as different ones can operate on the IPv4 or IPv6 interfaces.
The values you will need from this are the first or second columns. Both of these can be used to kill that particular process and free up that port.
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
nginx 6562 root 5u IPv4 329786 0t0 TCP *:http (LISTEN)
nginx 6562 root 6u IPv6 329787 0t0 TCP *:http (LISTEN)
nginx 6564 www-data 5u IPv4 329786 0t0 TCP *:http (LISTEN)
nginx 6564 www-data 6u IPv6 329787 0t0 TCP *:http (LISTEN)
nginx 6565 www-data 5u IPv4 329786 0t0 TCP *:http (LISTEN)
nginx 6565 www-data 6u IPv6 329787 0t0 TCP *:http (LISTEN)
nginx 6566 www-data 5u IPv4 329786 0t0 TCP *:http (LISTEN)
nginx 6566 www-data 6u IPv6 329787 0t0 TCP *:http (LISTEN)
nginx 6567 www-data 5u IPv4 329786 0t0 TCP *:http (LISTEN)
nginx 6567 www-data 6u IPv6 329787 0t0 TCP *:http (LISTEN)
Killing that Process
4. Linux offers you a few different ways that you can use to kill a process that is running on a particular port. Thanks to the previous command giving us both the process IDs and the name, we can use either method.
For the first method, we can manually specify each process that we want to terminate. This is useful when you might not necessarily want to terminate every running process for that program.
kill <PROCESSID>
For example, if we take the list from the previous command, we will run the following to free up port 80 on our Linux machine.
kill 6562 6565 6565 6566 6567
5. By utilizing the pkill command, you can save yourself the hassle of having to type out the ID for every process you want to terminate.
The downside of this is that if you have a program that handles multiple tasks it will terminate it. A good example of where this would be the case is with Docker.
To free up a port using the process name, use the following command in the terminal. All you need to do here, is specify the process name.
pkill <PROCESSNAME>
For example, since we know Nginx is consuming port 80
on our Linux system, and we want to free up that port, we can use the command below to kill all processes called “nginx
.”
pkill nginx
Finding and Killing a Process Using a Process in One Step
6. Of course, like with most things in Linux, it’s completely possible to find and kill a process that uses a port in one single command.
The lsof command we showed you earlier has a mode that allows it to output only the process IDs. This list of IDs can then be passed directly into the kill command. This command will terminate the process and free up access to the given port.
The key part of this command is using the “-t
” option that gets the lsof command to only print out the process IDs.
sudo kill $(sudo lsof -t -i:<PORT>)
7. For this example, let us use this command to kill any process running on our Linux machine that is running on port 80
sudo kill $(sudo lsof -t -i:80)
Conclusion
By this point, you should know how you can identify a process running on a port and kill it on Linux.
Killing the process will allow you to utilize that port for another program. Of course, you may need to stop the service controlling that process.
Please feel free to post a comment below if you have any questions about this guide.
If you found this quick guide to be helpful, we highly recommend that you explore our numerous other Linux tutorials.