Find and Kill the Process Using a Port on Linux (One Command)
One ss command shows the PID holding any port in a second. Copy-paste ss, lsof and fuser examples, and how to kill it safely with fuser -k 8080/tcp.
By Shanky Gupta, Director of Softuvo Solutions (LinkedIn) · Published · Updated
Your app will not start and the log says "Address already in use". Something is already holding the port. Before you can fix it, you need to know what that something is and whether it is safe to stop. On a production server, that second question matters more than the first.
Step 1: find what is using the port
Run ss with the port you care about. Replace 8080 with your port:
sudo ss -ltnp | grep :8080-l shows listening sockets, -t TCP, -n numeric ports and -p the process. You need sudo, or the process column stays empty for services owned by other users. A typical line looks like this:
LISTEN 0 511 0.0.0.0:8080 0.0.0.0:* users:(("node",pid=14321,fd=22))That means a process called node, PID 14321, is listening on port 8080 on every interface. If nothing prints, nothing is listening on that TCP port. For a UDP port, use -u instead of -t.
Two other tools give the same answer if ss is not available:
sudo lsof -i :8080 -s TCP:LISTEN
sudo fuser 8080/tcpStep 2: find out what the process is
A PID and a name like node or python do not tell you much. Check the full command line and the service that started it:
ps -o pid,user,etime,cmd -p 14321
systemctl status 14321systemctl status with a PID tells you which systemd unit owns the process, if any. etime shows how long it has been running. A process that started two minutes ago during a failed deploy is a very different case from one that has been up for 40 days.
To see how the process is behaving, open htop, press F3 and type the process name, or start it filtered to the PID:
htop -p 14321htop shows CPU and memory per process, per core, in a view that is much easier to read than top. If htop is not installed, it is in every major distro's repositories (sudo apt install htop or sudo dnf install htop).
Step 3: decide whether it is safe to stop
Ask these questions before you stop anything:
- Is it the old copy of the same app? A deploy that did not stop the previous version is the most common cause. Stopping the old copy is usually safe.
- Is it a database, queue or cache (mysqld, postgres, redis-server, rabbitmq)? Stopping it cuts off everything that depends on it and can lose data that was not yet written. Change your app's port instead, or plan the stop.
- Is it something you do not recognise? Do not kill it yet. Check the binary with ls -l /proc/14321/exe. An unknown process listening on a public port can be a sign of compromise, and killing it destroys the evidence.
- Is it the right service on the wrong port? Fix the port in its config rather than killing it, or it will take the port again after the next restart.
Step 4: stop it, gently first
If it belongs to a systemd service, stop the service. Otherwise systemd may restart the process a few seconds after you kill it:
sudo systemctl stop myapp.serviceIf it is not a service, send a normal stop signal. This lets the process close connections and flush data:
sudo kill 14321
sleep 5
sudo ss -ltnp | grep :8080Only if the port is still taken after that, force it:
sudo kill -9 14321kill -9 is the last option, not the first. The process gets no chance to clean up, so files can be left half-written and lock files left behind. fuser -k 8080/tcp does the same thing in one step: it sends SIGKILL to whatever holds the port, so use it only when you already know what is there.
fuser -k 8080/tcp: kill whatever holds a port in one command
fuser finds every process with a socket on a port and, with -k, kills them. It is in the psmisc package, installed by default on Ubuntu and Debian (sudo dnf install psmisc on RHEL, Rocky or Alma if it is missing). First look without killing anything:
sudo fuser -v 8080/tcp-v lists the user, PID and command for each process on the port. Without sudo you only see your own processes. 8080/tcp is short for -n tcp 8080; use 8080/udp for a UDP port, and -4 or -6 to match only IPv4 or IPv6 sockets.
Then stop it. Ask fuser for a normal stop signal first, and only force it if the port is still taken:
sudo fuser -k -TERM 8080/tcp
sleep 5
sudo fuser -v 8080/tcp
sudo fuser -k 8080/tcp- Plain fuser -k sends SIGKILL (the same as kill -9). -TERM, or any signal from fuser -l, sends that signal instead.
- Check the -v list before you kill. If systemd (PID 1) is listed, the port uses socket activation: stop both units (sudo systemctl stop myapp.socket myapp.service), never fuser -k, which would try to kill PID 1.
- It kills every process on the port, not just one: an nginx master and all its workers, or every Gunicorn worker. Add -i to be asked before each one.
- If a systemd service owns the process, systemd starts it again a few seconds later and the port is taken again. Stop the service with systemctl stop instead.
- If fuser prints nothing and exits with status 1, nothing on this machine holds the port. Inside Docker, the port may belong to docker-proxy; stop the container with docker stop, not fuser.
The same kill without fuser, using lsof to print only the PIDs on the port:
sudo kill $(sudo lsof -t -i tcp:8080 -s TCP:LISTEN)Check every listening port on the server
After a new deploy or an incident, it is worth seeing everything that is listening:
sudo ss -ltnp
sudo ss -lunpAnything bound to 0.0.0.0 or [::] is reachable from outside unless the firewall blocks it. Compare the list with sudo ufw status or sudo iptables -L -n. Redis or MySQL listening on 0.0.0.0 with an open firewall is a common finding.
Making this less manual
You end up running these commands every time a deploy fails or a service will not start. Opservo maps every listening port to its process on each server, so you can see what took a port, and when, without logging in.
Read next in the application and service troubleshooting series: Linux server slow? Check load, memory, I/O wait and swap in this order and What to Do When a Linux Server Load Average Is High. The whole series is at Application and service troubleshooting.