When you start a service and it fails with address already in use, something else is already holding the port. On a Linux server you can identify that process and stop it in three short steps. This guide uses ss and kill on Ubuntu 22.04, but the approach works on any modern distribution.
Step 1 - Find What Is Listening on the Port
Use ss, the modern replacement for netstat, to list the process bound to a port — here, port 8080:
sudo ss -ltnp 'sport = :8080'
The flags read as -l listening sockets, -t TCP, -n numeric ports (don't resolve names), and -p show the owning process. The output ends with a users:(...) field naming the program and its process ID (PID):
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 511 0.0.0.0:8080 0.0.0.0:* users:(("nginx",pid=1432,fd=6))
Here the PID is 1432. The sudo matters: without it, ss hides process details for sockets you don't own.
Step 2 - Confirm the Process Before You Touch It
Never kill a PID you haven't looked at. Check what it actually is:
ps -p 1432 -o pid,user,cmd
This prints the full command line and owning user, so you can be sure you're stopping the right thing and not a system service you depend on:
PID USER CMD
1432 www-data /usr/sbin/nginx -g daemon on; master_process on;
Step 3 - Stop It Gracefully, Then Forcefully
Ask the process to shut down cleanly first with a TERM signal (the default), which lets it close connections and flush state:
sudo kill 1432
Wait a second or two, then re-run the Step 1 command. If the port is free, you're done. If the process ignored TERM and is still listening, escalate to KILL, which the process cannot trap or ignore:
sudo kill -9 1432
Reserve kill -9 for stuck processes only — it gives the program no chance to clean up, which can leave temporary files or stale sockets behind.
Conclusion
You located the process bound to a port with ss -ltnp, verified it with ps, and stopped it with an escalating kill. Saving sudo ss -ltnp 'sport = :PORT' as a shell alias makes the next "address already in use" error a ten-second fix.
Top comments (1)
good, easy to understand