How to Kill the Process Using a Port in Linux
Find which process holds a port and stop it cleanly.
You start your app and get "address already in use". Something is already listening on that port, and in development it is almost always a previous instance of the same program that never shut down. Two steps: identify the owner, then stop it politely.
Step by step
1 Find what holds the port
$ sudo ss -tlnp | grep :3000 The last column shows the process name and PID, like users:(("node",pid=12345,fd=23)). Without sudo you only see your own processes, which is often enough in development.
2 Stop it politely
$ kill 12345 Sends SIGTERM, which asks the program to shut down cleanly: flush buffers, close files, exit. Give it a couple of seconds before escalating.
3 One-liner when you are sure
$ kill $(sudo lsof -t -i:3000) lsof -t prints bare PIDs, which feed straight into kill. Check what it matches first with lsof -i:3000 if the port might belong to something important.
4 Force it only if it ignores you
$ kill -9 12345 SIGKILL cannot be caught: the kernel removes the process immediately, with no chance to save state or clean up. Correct as a last resort, wrong as a habit.
5 If it is a real service, stop it properly
$ sudo systemctl stop nginx Killing a managed service can leave systemd trying to restart it. Use its manager instead — and consider whether your app should use another port.
Tips worth knowing
- A Docker container can hold the port from outside your process list: check `docker ps` before concluding the port is haunted.
- If the port frees up but binding still fails for a minute, that is TIME_WAIT. Enabling SO_REUSEADDR in your server removes the wait.
- Ports below 1024 need root to bind. That is a different error from this one.
- Make the port configurable via an environment variable in development — collisions become a one-word fix instead of an investigation.
Frequently asked questions
ss or lsof — which should I use?
ss is faster and installed by default on modern systems. lsof is more flexible and reads well (lsof -i :3000). Both answer this question fine.
Is it safe to kill the process?
If it is your own leftover instance, yes. If it turns out to be a system service like nginx or postgresql, stop it through systemctl instead.
Nothing shows up but the port is still busy
Try with sudo — the owner may be another user. Also check Docker containers, and remember that IPv6 and IPv4 bindings are listed separately.
If it goes wrong
The commands behind it
See listening ports, active connections and which process owns each socket.
killTerminate processes politely or forcibly — and understand what -9 really does.
psSee what is running, find a process ID, and combine with grep to hunt down a program.
systemctlStart, stop, enable and inspect services — the control panel of modern Linux.
Concepts involved: Port · PID (Process ID)
More how-to guides
See how much space is left, and find out what is using it.
Extract a .tar.gz archiveUnpack tar.gz, tar.bz2, tar.xz and zip archives from the command line.
Find and replace text in filesReplace text in one file or across a whole project, safely.
Make a script executableGive a script permission to run, and understand why it needs it.