How to Kill a Process Running on Particular Port in Linux?
Last Updated :
07 Feb, 2025
Have you ever tried to launch a web server, database, or application in Linux, only to be stopped by the frustrating “Address already in use” error? This happens when another process is already occupying the required port, preventing your application from running. Freeing up the port is crucial to ensure smooth system operation, especially when working with Apache, Nginx, Node.js, MySQL, PostgreSQL, Docker, and other services.
In this article, we'll learn how to find and kill a process running on a particular port in Linux using commands like lsof
, ss
, netstat
, and fuser
. We’ll cover:
- How to find the Process ID (PID) associated with a specific port
- Different methods to terminate the process safely and effectively
- How to verify that the port is free after termination
- Troubleshooting common issues like permission errors and auto-restarting services.
Common Scenarios where Port Conflicts Occur
- Running a web server (Apache, Nginx, or Node.js) and finding the port already in use.
- Starting a database (MySQL, PostgreSQL, MongoDB) and encountering a binding error.
- Deploying a containerized application where a process is still running on the host machine.
Identify the Process ID (PID) Using the Port Number
Before terminating a process occupying a specific port, the first step is to identify the Process ID (PID). A PID is a unique identifier assigned by the Linux kernel to each running process. Identifying the PID helps prevent accidental termination of unrelated processes.
1. Using lsof
(List Open Files Command)
The lsof
(List Open Files) command is one of the most powerful utilities to list open files and network connections. Since Linux treats everything as a file (including network sockets), lsof
can help find which process is occupying a specific port.
Syntax:
lsof -i :<port_number>
Here, 35821 is the PID of the process using port 8080.- COMMAND: Name of the process.
- PID: Process ID (needed for termination).
- USER: Owner of the process.
- FD: File descriptor.
- TYPE: Type of connection (IPv4 or IPv6).
- NAME: The associated port.
2. Using ss
(Socket Statistics Command)
ss
(Socket Statistics) is a modern replacement for netstat
, providing faster and more detailed network-related information.
Why Use ss
Instead of netstat
?
- Faster execution speed.
- More detailed output.
- Installed by default in most modern Linux distributions.
ss -tulnp | grep <port_number>
3. Using netstat
(Older Method)
While netstat
is deprecated in some Linux distributions, it can still be useful for finding process details.
ss -tulnp | grep <port_number>
This shows that process 12345 is using port 8080.- LISTEN: The process is actively listening for connections.
- PID: The process ID of the program using the port.
- fd: File descriptor.
- users: The process name (
nc
in this case).
Note: If you want to find the PID of processes running in the foreground or background refer the article Get Process ID of Linux
Methods to Kill the Process Using Its PID
Once you have identified the PID, you can terminate it using one of the following commands.
1. Using kill
(Graceful Termination)
The kill
command is the standard way to terminate a process in Linux. By default, it sends the SIGTERM
(signal 15), which gracefully stops the process, allowing it to save any unsaved data before exiting. For more details read this article How to Kill a Process in Linux
Syntax:
kill <PID> # This method allows the process to shut down properly, saving any necessary data before exiting.
2. Using fuser
to Kill a Process
The fuser
command is a quick and efficient way to find and terminate processes associated with a specific port. Instead of searching for the PID manually, fuser
directly lists and kills the process.
Syntax:
fuser -k -n tcp <port_number>
kill a process on port 8080-k
kills the process associated with the specified port.-n tcp
specifies the TCP protocol (use -n udp
for UDP ports).
When to Use:
- When you want a one-liner command to kill a process using a port.
- When you don’t want to manually search for the PID.
3. Using lsof
to Kill a Process
The lsof
(List Open Files) command is widely used to check open connections and identify processes occupying a specific port. It can also be used in combination with kill
to terminate a process instantly.
Syntax:
kill -9 $(lsof -t -i :<port_number>)
forcefully terminating the process on port 8080lsof -t -i :8080
extracts the PID of the process using port 8080
.kill -9
$(...)
sends a SIGKILL signal to terminate it.
When to Use:
- When you need a quick and efficient method to find and kill a process.
- When you want to avoid manually searching for the PID.
Verify That the Port is Free
Once you have terminated the process occupying a particular port, it’s crucial to verify whether the port has been successfully freed. If another application or process has taken over the port, you may still encounter issues when attempting to start your service.
lsof -i :8080 # If there is no output, the port is now free.
- If there is no output, the port is now free and available for use.
- If a process is still using the port, the output will display the Process ID (PID) and related details.
Troubleshooting Common Issues
Even after following the steps to kill a process, you might encounter errors or unexpected behavior. Below are some common issues and their solutions.
1. Permission Denied: Use sudo
w
hen attempting to terminate a process, you may get a "Permission denied" error. This happens when trying to kill a system process or an application running under another user.
sudo kill -9 <PID>
2. Process Keeps Restarting
Some processes are managed by systemd and will restart automatically even after being killed.
sudo systemctl stop <service_name>
Examples:
Stop Apache:
sudo systemctl stop apache2
Stop Nginx:
sudo systemctl stop nginx
3. Process Not Found
If you try to kill a process, but it’s not listed in lsof
or fuser
, it might have already exited, or another service might be using the port.
Conclusion
Knowing how to find and kill processes using specific ports in Linux is essential for managing server applications, troubleshooting conflicts, and ensuring smooth system operation. Whether you use lsof
, ss
, fuser
, or netstat
, these commands help you quickly identify and terminate unwanted processes, freeing up ports for essential applications.
Similar Reads
How to Kill Processes by Given Partial Names in Linux On a Unix system creates a separate environment for a program when it is executed. Everything the system needs to run the program as if there were no other programs in this environment. In Unix, each command you issue initiates or starts a new process. You initiated a process using the ls command to
4 min read
How to List Running Processes in Linux | ps Command As we all know Linux is a multitasking and multi-user system. So, it allows multiple processes to operate simultaneously without interfering with each other. Process is one of the important fundamental concepts of the Linux OS. A process is an executing instance of a program that carries out differe
10 min read
How to Kill a Process in Linux | Kill Command kill command in Linux (located in /bin/kill), is a built-in command which is used to terminate processes manually. kill command sends a signal to a process that terminates the process. If the user doesn't specify any signal that is to be sent along with the kill command, then a default TERM signal i
6 min read
How to Restart a Service in Linux? In Linux, managing system services is an essential task for maintaining the stability and performance of your system. Whether you're troubleshooting an application, applying configuration changes, or ensuring services run smoothly, knowing how to restart a service in Linux is crucial. With tools, li
3 min read
How to Kill a Detached screen Session in Linux The screen session is like virtual windows, And in Linux it works like a multiplexer terminal where we can create more than one window and work on that. we can create multiple windows inside one session. We called it virtual because the process will continue to run when their window will not be visi
3 min read
How to Manage Process in Linux A process means a program in execution. It generally takes an input, processes it, and gives us the appropriate output. It involves controlling and monitoring all the running programs on the system. This includes managing how much system resources each process gets, deciding when processes can use t
5 min read