When you try to start a server in Linux and get “Address already in use,” another process is holding that port. Linux won’t let two processes bind to the same port simultaneously. To fix it, you need the process ID and a way to terminate it. Here’s how to kill a process on a port in Linux using lsof, fuser, kill, and ss.
When Port Conflicts Occur in Linux
The scenario is almost always the same: you stopped a service but it didn’t exit cleanly, leaving a ghost process holding the port. This is common with Apache, Nginx, MySQL, PostgreSQL, and Node.js. Developers who work on running Node.js locally on Linux see it most often on ports 3000, 8080, and 5432.
It also happens after a service crash, an abrupt container stop, or when two instances of the same application start in quick succession. The port stays bound until the OS reclaims it or you kill the process directly.
How to Find the Process ID (PID) Running on a Port in Linux
You need the PID before you can terminate anything. Three commands cover this.
Using lsof to Find What’s Running on a Port
lsof (List Open Files) works because Linux treats network sockets as files. It returns the PID, command name, user, and connection type for any process bound to a port.
lsof -i :<port_number>
The PID appears in the second column. To print only the PID — which is useful when piping to kill:
lsof -t -i :<port_number>
If you need to check active Linux processes by PID more broadly, lsof paired with ps aux gives a complete view of what’s running and what resources each process holds.
Using ss to Check Which Process Is on a Port
ss (socket statistics) replaced netstat on most modern Linux distributions. It’s faster, installed by default, and provides more socket detail.
ss -tulnp | grep <port_number>
The PID appears at the end of the line inside a users:(("process_name",pid=XXXX)) field. The flags break down as: -t TCP, -u UDP, -l listening only, -n numeric addresses, -p process info.
Using netstat (Legacy Method)
netstat is deprecated in many distributions but still works where installed.
netstat -tulnp | grep <port_number>
The PID and process name appear in the last column. For current systems, ss is the better choice — same output format, noticeably faster.
Methods to Kill a Process on a Port in Linux
Four methods cover every situation. The choice comes down to whether you want a clean shutdown or an immediate termination, and whether you want to skip the PID lookup entirely.
Method 1: Graceful Termination with kill
The default kill command sends SIGTERM (signal 15). The process receives the signal and shuts down cleanly — saving state, closing open connections, releasing file handles.
kill <PID>
Always try this first. A well-written application will release the port and exit within a couple of seconds. If it doesn’t respond, step up to SIGKILL.
Method 2: Force Kill with kill -9 on a Port
SIGKILL forces termination immediately. The Linux kernel handles it directly — the process cannot intercept, ignore, or delay it.
kill -9 <PID>
Use this after SIGTERM fails, or when a process is stuck in an uninterruptible state. The trade-off is that unsaved data is lost. Getting comfortable with Linux terminal command basics makes it easier to know which signal type fits which situation.
Method 3: Using fuser to Kill a Process on a Port Directly
fuser skips the PID lookup entirely. It finds and terminates the process bound to a port in a single command — no intermediate steps.
fuser -k -n tcp <port_number>
The -k flag sends SIGKILL. Use -n udp for UDP ports. This is the fastest option when you just need the port cleared without inspecting the process first.
Method 4: One-Liner with lsof and kill -9
This combines the PID lookup and force termination into one command using command substitution:
kill -9 $(lsof -t -i :<port_number>)
lsof -t -i :8080 outputs only the PID. The $(...) shell substitution passes it directly to kill -9. No copy-paste required.
How to Verify the Port is Free After Killing a Process in Linux
After terminating the process, confirm the port is actually available before restarting your service:
lsof -i :<port_number>
No output means the port is free. If a process still appears, either it restarted automatically or a different process claimed the same port. This check matters especially when managing port forwarding in the Linux environment, where an occupied port will silently block the forwarded connection.
Troubleshooting Common Port Kill Errors in Linux
Permission Denied When Killing the Process
This happens when the target process runs as a different user or as a system service. Add sudo:
sudo kill -9 <PID>
Process Keeps Restarting After kill -9
If systemd manages the service, killing the PID won’t stick — systemd detects the process death and immediately restarts it. Stop the service through systemctl instead:
sudo systemctl stop <service_name>
For Apache: sudo systemctl stop apache2. For Nginx: sudo systemctl stop nginx. For MySQL: sudo systemctl stop mysql.
Port Still Shows as Used After Termination
The socket may be in a TIME_WAIT state. This isn’t an active process — it’s a connection winding down in the kernel’s TCP stack. Run ss -tulnp | grep <port> to check the state. TIME_WAIT resolves on its own within 60 to 120 seconds without any intervention.
FAQs
What is the fastest way to kill a process on a port in Linux?
fuser -k -n tcp <port> terminates the process in one command without requiring the PID. For port 8080, run fuser -k -n tcp 8080. No separate lookup needed.
What is the difference between kill and kill -9 in Linux?
kill sends SIGTERM, which lets the process shut down cleanly and save state. kill -9 sends SIGKILL, which forces instant termination. Always try SIGTERM before using SIGKILL.
How do I kill a process on port 8080 in Linux?
Run lsof -t -i :8080 to get the PID, then kill -9 <PID>. Or use the one-liner: kill -9 $(lsof -t -i :8080) to skip the manual PID step.
Why does the process restart after kill -9 in Linux?
Systemd-managed services restart automatically when their process dies. Use sudo systemctl stop <service_name> instead of kill to stop them at the service level.
Can I kill a process on a UDP port the same way?
Yes. For fuser, use fuser -k -n udp <port>. For lsof, run lsof -i udp:<port> to find the PID first, then terminate with kill.
