How do I find which process is using a port on Linux when my service says Address already in use?
Only one process can listen on a TCP port at a time, so a second one fails with Address already in use. sudo ss -ltnp shows each listening socket with the process that owns it, and systemctl status on that process's service shows where it came from. A stopped service starts again at boot if it is still enabled. In this incident a retired export service came back after a reboot and held port 8080.
What it looks like
- The service fails with Address already in use.
- Requests get answers from the wrong program, such as 404 pages.
- ss shows another process on the port.
- It started after a reboot.
Commands that find the cause
journalctl -u orders-api -n 5 --no-pagerThe service's log, newest last. A server that cannot start says why on its way out, often with an error number from the operating system.
sudo ss -ltnp 'sport = :8080'Lists listening TCP sockets on port 8080 and, with sudo, the process that owns each one. Only one process can listen on a port at a time.
systemctl status orders-api --no-pagerShows whether the service is running. A service that failed to start, or is restarting, shows that state and the reason in its last log lines.
systemctl list-units --type=service --state=running --no-pager | grep -E 'orders|export'The services systemd is running now, filtered to order and export names. A service you thought was retired can still be here.
curl -s -o /dev/null -w '%{http_code}\n' localhost:8080/exportOne request the way the warehouse makes it, printing only the HTTP status. 200 is a good export; anything else came from whatever answers on the port.
What the evidence looked like
Recorded on a real system when this incident was captured for the game.
$ journalctl -u orders-api -n 5 --no-pager
Nov 29 20:25:46 ops-1 python3[3110]: File "/usr/lib/python3.12/socketserver.py", line 473, in server_bind
Nov 29 20:25:46 ops-1 python3[3110]: self.socket.bind(self.server_address)
Nov 29 20:25:46 ops-1 python3[3110]: OSError: [Errno 98] Address already in use
Nov 29 20:25:46 ops-1 systemd[1]: orders-api.service: Main process exited, code=exited, status=1/FAILURE
Nov 29 20:25:46 ops-1 systemd[1]: orders-api.service: Failed with result 'exit-code'.Root cause
The reboot started the still-enabled legacy-export service on port 8080 first; disabling it let orders-api bind.
The fix
Stop and disable the old service, then restart orders-api:
sudo systemctl disable --now legacy-export && sudo systemctl restart orders-apiHow to prevent it
Retire a service with systemctl disable --now so it cannot return at boot, and check the port is free before starting the replacement.
More incident guides
- Linux disk full: no space left on device
- systemd service keeps restarting after a deploy
- Linux name or service not known after a config change
- Too many open files in a systemd service
- CrashLoopBackOff after a config change
- Service returns 503 while every pod is Running
- OOMKilled and exit code 137
- Pods fail to start after a Secret change
- Requests fail after adding a NetworkPolicy
- Healthy pods restarted by a liveness probe
- Slow service with low CPU and no restarts
- Rollout stuck on ImagePullBackOff
- Pods stuck Pending with insufficient CPU
- Docker container exits immediately after a config change
- Docker port mapping not working, empty reply
- Docker container cannot connect to the database host
- Docker volume permission denied
- Docker container exit code 137, killed for memory