By Riya Madaan
Address already in use: who owns this port?
bind: address already in use - find the process holding the port with ss and lsof, tell TIME_WAIT from a live listener, and fix it without rebooting.
bind: address already in use is one error with two very different diagnoses: either a live
process genuinely owns the port, or nobody does and you're fighting the TCP state machine.
The fix for the first is finding the process; the "fix" people apply for the second - killing
random things, rebooting - is unnecessary, because leftover TIME_WAIT connections need a
socket option, not a victim. Two commands split the cases.
| Symptom | Likely cause | First command |
|---|---|---|
address already in use, port shows a listener |
A live process owns the port | ss -ltnp 'sport = :8080' |
| Nothing listening, bind still fails after a restart | Old connections in TIME_WAIT |
ss -tan 'sport = :8080' state time-wait |
0.0.0.0:8080 fails, nothing on the wildcard |
A specific bind (e.g. 127.0.0.1:8080) holds it |
ss -ltnp | grep 8080 |
| Two copies of the service both bound fine | SO_REUSEPORT on the socket |
ss -ltnp 'sport = :8080' |
| Port conflict inside a Kubernetes pod | Sidecar bound the same port in the shared network namespace | kubectl logs <pod> --previous |
Step 1: which process is using the port?
Ask ss for the listener and its owning process:
ss -ltnp 'sport = :8080'
# State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
# LISTEN 0 4096 0.0.0.0:8080 0.0.0.0:* users:(("gunicorn",pid=3112,fd=5))
ss (from iproute2, on every modern box) shows listeners (-l), TCP (-t), numeric ports
(-n), and - the part people forget - the owning process (-p, needs root to see other
users' processes). Alternatives that answer the same question:
lsof -iTCP:8080 -sTCP:LISTEN -n -P
fuser 8080/tcp # terse: just prints the PID
If there's a listener: go to step 2. If ss -ltnp shows nothing listening yet bind still
fails: go to step 3 - you're in TIME_WAIT territory.
Step 2: a live process owns it - identify before you kill
ps -fp 3112
ls -l /proc/3112/cwd /proc/3112/exe # where it runs from, what binary it actually is
The usual suspects, roughly in order of frequency:
- A previous instance of your own service that never died - a deploy that started the new
version before stopping the old, or a supervisor (systemd, a container runtime) restarting
it behind your back. If it's a systemd unit, stop it through systemd
(
systemctl stop app), notkill, or the supervisor will resurrect it while you're still reading the man page. The broader systemd won't-start checklist covers that dance. - systemd socket activation:
Foo.socketholds the port and hands it toFoo.serviceon demand. Inssoutput the owner shows assystemd. Checksystemctl list-sockets. The fix is disabling/stopping the.socketunit, not hunting a phantom daemon. - docker-proxy: a published container port (
-p 8080:80) is held by adocker-proxyprocess.docker ps --format '{{.Names}} {{.Ports}}'finds the container; stop it, not the proxy process. - Something legitimately configured on the same port - two services with the same default. Ask before you kill; in shared environments, deleting someone else's listener to make room for yours is the definition of collateral damage.
Note that inside a Kubernetes pod, two containers share one network namespace - a sidecar binding your port produces this exact error and is a known CrashLoopBackOff cause. The free practice labs are all Kubernetes, and a crash-looping service is one of them, if you want to chase this kind of thing on a cluster that's genuinely broken.
Step 3: why does bind fail when nothing is listening?
Because of TIME_WAIT. After a process exits, connections it owned linger in TIME_WAIT for 60 seconds (Linux's
hardcoded 2×MSL) so late packets from the old connection can't corrupt a new one. See them:
ss -tan 'sport = :8080' state time-wait | head
ss -s # summary: how many sockets in each state
A server that just restarted can have thousands. The correct fix is in code, not ops:
SO_REUSEADDR on the listening socket, set before bind():
int on = 1;
setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
Every serious server framework exposes this (reuse_address, reuseaddr, Go's default net
listener behavior effectively handles it). SO_REUSEADDR lets you bind while old connections
sit in TIME_WAIT - it does not let two live processes listen on the same port. That's
SO_REUSEPORT, a different option, used deliberately for load-balancing accept queues
across workers - and occasionally the surprise explanation for "two copies of the service are
running and both bound successfully."
What not to do: net.ipv4.tcp_tw_reuse tweaks affect outbound connections, not your
listener, and disabling TIME_WAIT semantics globally to fix one daemon is trading a local
annoyance for protocol correctness. If the app is yours, set the socket option; if it isn't,
wait the 60 seconds or file the bug.
The wildcard-vs-specific trap
One more shape of this error: binding 0.0.0.0:8080 fails because something holds
127.0.0.1:8080 (or vice versa). A wildcard bind conflicts with any specific bind on the same
port. ss -ltnp | grep 8080 without the sport filter shows every variant. Same story with
IPv6: some stacks bind :: with v6only=0 and cover IPv4 too - ss -ltnp shows *:8080 vs
[::]:8080, and the "conflict" is one dual-stack socket you didn't recognize.
Verify the fix
ss -ltnp 'sport = :8080' # exactly one listener, the one you expect
curl -s localhost:8080/health # and it answers
Then make the recurrence boring: deploys that stop-before-start (or do proper socket handoff),
SO_REUSEADDR in the server config, and one authoritative map of who owns which port. Port
conflicts are a favorite drill for a reason - resisting the urge to kill -9 the first PID you
see, and identifying the owner first, is precisely the judgment that
deliberate troubleshooting practice builds.
FAQ
How do I find which process is using a port?
ss -ltnp 'sport = :PORT' as root, or lsof -iTCP:PORT -sTCP:LISTEN. Both print the PID and
process name; fuser PORT/tcp prints just the PID.
Why do I get address already in use when nothing is listening?
Old connections in TIME_WAIT still reserve the address tuple for 60 seconds after the previous
process exited. Set SO_REUSEADDR on the listening socket (a framework option in most
servers), or wait out the timer. No process needs killing - there is no process.
What's the difference between SO_REUSEADDR and SO_REUSEPORT?
SO_REUSEADDR allows binding despite lingering TIME_WAIT state from a dead process.
SO_REUSEPORT allows multiple live sockets to share one port for load balancing - every
listener must set it, and it can mask duplicate-instance bugs.
Can two processes ever listen on the same port?
Yes: with SO_REUSEPORT on both, or when one binds a specific address and another a
different specific address on the same port (127.0.0.1 vs a NIC address). A wildcard bind
conflicts with everything on that port.