How to kill a process running on particular port in Linux?
Categories:
Mastering Process Termination: How to Kill a Process on a Specific Port in Linux

Learn essential Linux commands to identify and terminate processes occupying a particular network port, ensuring smooth operation and resource management.
In the world of Linux system administration and development, it's a common scenario to encounter a situation where a specific network port is already in use, preventing a new application or service from starting. This often happens when a previous instance of a program didn't shut down cleanly, or another service is unexpectedly occupying the port. This article will guide you through the process of identifying which process is listening on a given port and how to gracefully (or forcefully) terminate it, using a series of powerful command-line tools.
Understanding Network Ports and Processes
Before diving into the commands, it's crucial to understand the relationship between network ports and processes. A network port is a communication endpoint specified for a particular type of data transfer. When an application or service starts, it 'listens' on one or more ports, making itself available for incoming connections. Each listening port is associated with a unique process ID (PID). Our goal is to find this PID and then use it to stop the corresponding process.
flowchart TD A[Start Application/Service] --> B{Port Available?} B -- No --> C[Error: Port in Use] C --> D[Identify Process using Port] D --> E[Get Process ID (PID)] E --> F[Terminate Process (Kill PID)] F --> G{Process Terminated?} G -- Yes --> H[Retry Application/Service] G -- No --> I[Troubleshoot/Force Kill] B -- Yes --> H
Workflow for resolving a 'Port in Use' error.
Step 1: Finding the Process Using lsof
or netstat
The first step is to identify which process is currently listening on the problematic port. Linux provides several utilities for this, with lsof
(list open files) and netstat
(network statistics) being the most common and powerful. We'll focus on lsof
as it often provides more detailed information.
sudo lsof -i :<PORT_NUMBER>
Using lsof
to find processes listening on a specific port.
Replace <PORT_NUMBER>
with the actual port you're investigating (e.g., 8080
, 3000
, 443
). The sudo
prefix is often necessary to see all processes, especially those owned by other users or system services. The output will typically show the command, PID, user, and other details. Look for the PID
column.
sudo netstat -tulnp | grep :<PORT_NUMBER>
Using netstat
to find processes listening on a specific port.
Alternatively, netstat
can be used. The options -t
(TCP), -u
(UDP), -l
(listening sockets), -n
(numeric addresses), and -p
(show PID/program name) are commonly combined. The grep
command filters the output for your specific port.
lsof
or netstat
are not installed, you might need to install them using your distribution's package manager. For Debian/Ubuntu: sudo apt install lsof net-tools
. For RHEL/CentOS/Fedora: sudo yum install lsof net-tools
or sudo dnf install lsof net-tools
.Step 2: Terminating the Process Using kill
Once you have identified the PID of the process occupying the port, you can use the kill
command to terminate it. The kill
command sends signals to processes. The default signal is SIGTERM
(15), which requests the process to terminate gracefully. If a process doesn't respond to SIGTERM
, you might need to use SIGKILL
(9), which forces termination immediately.
kill <PID>
Gracefully terminating a process using its PID.
Replace <PID>
with the actual process ID you obtained from lsof
or netstat
. After running this command, it's good practice to re-check the port with lsof
or netstat
to ensure the process has indeed stopped.
kill -9 <PID>
Forcefully terminating a process using its PID.
kill -9
(SIGKILL) with caution. This signal forces a process to terminate immediately without giving it a chance to save its state or clean up resources. This can lead to data corruption or orphaned files if the process was performing critical operations. Always try kill <PID>
first.Combining Commands for Efficiency
For a more streamlined approach, you can combine these commands into a single line, especially when you're confident about the process you want to kill. This involves using awk
or grep
with cut
to extract the PID directly.
sudo lsof -t -i :<PORT_NUMBER> | xargs kill
One-liner to find and gracefully kill a process by port.
In this command:
lsof -t -i :<PORT_NUMBER>
: The-t
option tellslsof
to output only the PIDs, making it suitable for piping.xargs kill
:xargs
takes the PIDs fromlsof
's output and passes them as arguments to thekill
command.
sudo lsof -t -i :<PORT_NUMBER> | xargs kill -9
One-liner to find and forcefully kill a process by port.
lsof
or netstat
before piping it to kill
, especially when using kill -9
. Ensure you are targeting the correct process to avoid unintended system disruptions.Practical Example: Killing a Node.js Server on Port 3000
Let's walk through a common scenario where a Node.js development server is stuck on port 3000.
1. Identify the process
First, use lsof
to find the process listening on port 3000:
sudo lsof -i :3000
Output might look like:
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
node 12345 user1 10u IPv4 67890 0t0 TCP *:3000 (LISTEN)
From this, we identify the PID as 12345
.
2. Gracefully terminate the process
Attempt to kill the process gracefully:
kill 12345
Wait a few seconds and re-check with sudo lsof -i :3000
. If the process is gone, you're done.
3. Forcefully terminate (if necessary)
If the process persists, use the forceful kill command:
kill -9 12345
Re-check the port again. The process should now be terminated, freeing up port 3000.