A loopback lab for reading the socket, identifying its owner, and proving you fixed the right thing.
You hit Run. Your dev server prints Address already in use and dies before it can disappoint you in any more interesting ways.
A common fix is to copy a command that takes a port number and kills whatever comes out the other end. It might free the port. So might unplugging the machine. Neither tells you why your app couldn't start, and neither helps much when the thing you killed comes back with a new PID.
A port number is not an identity. Start with the address and protocol. Find the process that actually holds the socket. Figure out who started it. Then decide whether you even need to stop it.
Here's the whole sequence on a machine you control. No production services, no network scan, no sudo for the lab.
Make a conflict you can afford to lose
You need Linux, Python 3, ss from iproute2, and curl. Pick a high port that isn't already listening. We'll use 18765:
ss -H -ltn 'sport = :18765'
If it prints a listener, choose another port and use it throughout. Don't clear an existing listener just to do a tutorial.
In terminal A:
lab=$(mktemp -d)
printf '%s\n' 'loopback port lab' > "$lab/index.html"
python3 -m http.server 18765 --bind 127.0.0.1 --directory "$lab"
The scratch directory makes sure Python isn't serving your home folder. The explicit bind keeps this demo on IPv4 loopback, not on the network. Leave terminal A open.
In terminal B:
curl --noproxy '*' -fsS --max-time 2 http://127.0.0.1:18765/
python3 -c "import socket; s=socket.socket(); s.bind(('127.0.0.1', 18765))"
The first command should print loopback port lab. The second should fail with OSError: [Errno 98] Address already in use. If the first command fails, read terminal A first; you need a working listener for the demonstration. If the second command exits without an error, it didn’t hit a conflict. Recheck the first listener before going on.
Read the socket before the process
In terminal B:
ss -ltnp 'sport = :18765'
The flags ask for listening sockets, TCP, numeric addresses and ports, and process information. The filter matches the local port exactly. This is better than searching the output for 18765, which can catch the wrong field or another number that happens to contain the same digits. See the ss manual.
Illustrative output, shortened; your PID and file descriptor may differ:
LISTEN 0 5 127.0.0.1:18765 0.0.0.0:* users:(("python3",pid=4242,fd=3))
Read it from the middle out. 127.0.0.1:18765 is the local endpoint. It's the address Python actually bound. 0.0.0.0:* is in the peer column; it doesn't mean this particular server is exposed on every interface. If you see 0.0.0.0:18765 in the local column, that's a wildcard IPv4 bind. IPv6 has its own loopback, ::1, and its own wildcard, ::. Depending on how a program configures its sockets, a wildcard IPv6 listener can also account for IPv4 traffic; inspect both before deciding you have a ghost.
If users:(...) is missing, the socket can still be real. You might not have permission to see the owning process. If you administer the host and are allowed to inspect it, rerun the inspection, not a kill command, with sudo ss -ltnp 'sport = :18765'.
One more easy miss: if the app's error concerns UDP, change the protocol you inspect. ss -uanp 'sport = :18765' lists matching UDP sockets, including unconnected sockets that don't show a TCP-style LISTEN state. TCP and UDP with the same number aren't automatically fighting over the same socket.
A PID tells you who. It doesn't tell you who is in charge.
Use the PID from your own result, not the sample:
pid=4242 # replace with the PID you actually saw
ps -p "$pid" -o pid,ppid,user,etime,args
cat "/proc/$pid/cgroup"
Look at the command arguments and parent PID. python3 could be a spare dev server, the working app you're trying to preserve, or a worker somebody started under a supervisor. The command line in this lab names the scratch directory and loopback address. That's how you know you're looking at your demo and not just at another interpreter.
On a systemd host, systemctl status "$pid" can help associate a live PID with a system unit or scope. A cgroup path can also hint that it's under a per-user unit, in which case inspect that named unit with systemctl --user status UNIT.service as the owning user. A scope is not itself evidence that you should stop everything in it. Read the unit description and check what else depends on it. If the listener came from a container or a dev runner, use that manager's status and stop controls instead of killing an arbitrary child PID.
If the port was selected dynamically, write down the exact address the application reports as well as where you looked for it. A port printed inside a development container need not be the port another terminal can reach on the host. Check from the same network context as the failing application; then use the container manager’s published-port view if you need to understand the host side.
If it belongs to something useful, leave it alone. Change the new app's port. A conflict isn't proof that the older listener is wrong.
Ask the right thing to stop
For this lab, terminal A started the server in the foreground. Return there and press Ctrl+C. There's no reason to send a signal from another shell when you can stop the demo from the terminal where you can see it.
If it's an unmanaged process you started and you can't get back to its terminal, first recheck that the current PID from ss still belongs to the same process. Then kill -TERM "$pid" asks that one process to terminate. TERM gives programs that handle it an opportunity to clean up; it doesn't guarantee they do. Don't copy the example PID. Don't escalate just because the number didn't disappear immediately.
For an intentionally managed service you are authorized to stop, use the manager: sudo systemctl stop UNIT.service for a system service or systemctl --user stop UNIT.service for your own user service. Substitute the actual unit name and avoid stopping anything production-facing on the strength of a tutorial. If the PID changes but the port is still occupied, that's a clue to check the supervisor, not an invitation to chase PIDs.
There's a particular systemd trick worth knowing: a .socket unit can hold the port and start a service when traffic arrives. Stopping only the service might leave the listener intact. Check systemctl list-sockets --all or systemctl --user list-sockets --all. If you're intentionally taking a confirmed socket-activated service offline, stop its socket and service through systemd; don't disable them permanently as a side effect of diagnosing a bind. systemctl reference · socket activation reference
Verify twice, once while it's gone and once when it's back
After stopping the demo in terminal A, check in terminal B:
ss -H -ltnp 'sport = :18765'
curl --noproxy '*' -fsS --max-time 2 http://127.0.0.1:18765/
For the lab you want no listener row and a failed curl. Those are separate checks: you don't see a listener, and the client can't hit the old endpoint. Now rerun the first Python command in terminal A. Run the same two checks again from B. This time you want the expected listener on 127.0.0.1 and the expected loopback port lab body. A green status flag is nice. The actual response you asked the server to serve is better.
One caveat for the real incident: if ss shows no listener and the app still can't bind, record the app's exact bind address and error before guessing again. Check whether it requests another address, a different protocol, or a different network namespace (common with containers). A socket also doesn't have to be a TCP listener to show up in binding trouble; ss -tan 'sport = :18765' can distinguish visible TCP states. A TIME-WAIT row isn't a running server or a PID to kill. Context matters.
To clean up the demo, stop the server with Ctrl+C in terminal A, then run rm -- "$lab/index.html" and rmdir -- "$lab" in that shell.
Disclosure: AI assisted in drafting and editing this article.
Top comments (0)