Sooner or later every developer and home-lab owner sees it: bind: address already in use, EADDRINUSE, or Docker refusing to start a container because "port is already allocated". Something is already listening on the port you want. The fix takes a minute once you know which command to run on each operating system, and that's what this guide collects in one place.
Examples use port 8080. Replace it with your port. Commands are for current releases: Ubuntu and Debian, macOS 14 and 15, and Windows 11 with PowerShell.
The two questions to answer
- Which process owns the port (its name and process ID, the PID)?
- What started it: a service, a container, an app at login or something you forgot running in another terminal?
Killing a PID makes the error go away until the next reboot. Finding what started it fixes it for good.
Linux
ss (the modern default)
ss replaced netstat years ago and is installed everywhere:
sudo ss -ltnp 'sport = :8080'
-lshows listening sockets only,-tTCP,-nnumeric ports,-pthe owning process.sudomatters: without it,sscan't show processes owned by other users, and the process column is just empty.
Output looks like this:
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))
For UDP (DNS on 53, WireGuard on 51820), swap -t for -u:
sudo ss -lunp 'sport = :53'
To list everything listening, which is a useful security check in itself:
sudo ss -ltnup
Pay attention to the address. 127.0.0.1:8080 is only reachable from the machine itself, while 0.0.0.0:8080 or [::]:8080 is reachable from the network.
lsof and fuser
lsof gives a friendlier view and is handy because the same command works on macOS:
sudo lsof -nP -iTCP:8080 -sTCP:LISTEN
fuser is the quickest one-liner and can also kill the owner (use with care):
sudo fuser -v 8080/tcp
Find what started it
With the PID, find out who's responsible:
ps -o pid,user,cmd -p 1432 systemctl status 1432
systemctl status <PID> shows the systemd unit the process belongs to, if any. Then stop and disable it properly:
sudo systemctl disable --now nginx
Two Linux special cases trip people up:
- Socket activation. Some services listen through a
.socketunit, so systemd itself holds the port and starts the service on the first connection.systemctl list-socketsshows them. Stop the socket unit, not just the service. - Docker. A published container port shows up as
docker-proxy(or not at all when Docker uses iptables directly). Checkdocker ps --format '{{.Names}} {{.Ports}}'to see which container has it.
macOS
lsof
macOS doesn't have ss, and its netstat doesn't show process IDs in the usual form, so lsof is the tool:
sudo lsof -nP -iTCP:8080 -sTCP:LISTEN
The COMMAND and PID columns tell you what it is. For a full list of listening ports:
sudo lsof -nP -iTCP -sTCP:LISTEN
For UDP:
sudo lsof -nP -iUDP:5353
Then look the process up:
ps -p 1432 -o pid,user,command
The AirPlay surprise on ports 5000 and 7000
If port 5000 or 7000 is taken on a Mac and lsof shows ControlCenter, that's the AirPlay Receiver built into macOS since Monterey. It's a common clash with Flask, the Docker registry and other dev tools that default to port 5000. Either run your app on a different port, or switch the receiver off in System Settings > General > AirDrop & Handoff > AirPlay Receiver.
Find what started it
Background services on macOS are started by launchd. To see whether a launch agent or daemon is behind the process:
launchctl list | grep -i <name> ls ~/Library/LaunchAgents /Library/LaunchAgents /Library/LaunchDaemons
Apps that start at login are listed in System Settings > General > Login Items & Extensions. Homebrew services (postgresql, redis, nginx) are managed with brew services list and stopped with brew services stop <name>.
Windows
netstat and tasklist (works everywhere)
In Command Prompt or PowerShell:
netstat -ano | findstr :8080
The last column is the PID. Look it up:
tasklist /FI "PID eq 1432"
PowerShell (clearer output)
PowerShell can show the process name in one go:
Get-NetTCPConnection -LocalPort 8080 -State Listen |
Select-Object LocalAddress, LocalPort, OwningProcess,
@{ n = 'Process'; e = { (Get-Process -Id $_.OwningProcess).ProcessName } }
For UDP:
Get-NetUDPEndpoint -LocalPort 53 | Select-Object LocalAddress, LocalPort, OwningProcess
To see whether the process is a Windows service:
Get-CimInstance Win32_Service | Where-Object ProcessId -eq 1432 | Select-Object Name, DisplayName, StartMode
Then stop it with Stop-Service <Name>, and set it to manual with Set-Service <Name> -StartupType Manual if you don't want it at startup.
PID 4 means "System"
If the owner is PID 4 (System), the port is held by the Windows kernel's HTTP driver, http.sys, on behalf of a service such as IIS, Windows Remote Management or a print or media-sharing service. Find out which one with (in an elevated prompt):
netsh http show servicestate
The output lists registered URLs and the process ID of the service behind each one.
"Port in use" when nothing is listening
On machines with Hyper-V, WSL2 or Docker Desktop, Windows reserves blocks of ports for its virtual networking. A port inside a reserved range can't be used even though no process owns it, and you get errors like "An attempt was made to access a socket in a way forbidden by its access permissions". Check the reserved ranges:
netsh interface ipv4 show excludedportrange protocol=tcp
If your port is inside one, pick a port outside the ranges. Restarting the Windows NAT service (net stop winnat then net start winnat in an elevated prompt) often changes the ranges. WSL2 users should also know that ports opened inside WSL show up on Windows as wslrelay.exe. My WSL2 web development guide covers how that forwarding works.
Free the port safely
Once you know what it is:
- It's yours and forgotten (an old
npm run devorphp artisan serve): stop it with Ctrl+C in its terminal, or end the process. - It's a service you don't need: stop and disable the service, as shown above, rather than killing the process.
- It's a service you do need: move your app to another port instead.
Ending a process directly, as a last resort:
# Linux and macOS: ask nicely first, force only if needed kill 1432 kill -9 1432 # Windows (PowerShell, then the classic equivalent) Stop-Process -Id 1432 taskkill /PID 1432 /F
Don't kill processes you don't recognise without looking them up first. On Windows in particular, PID 4 and other system processes can't be ended safely.
Check from another machine
Sometimes the port is free locally but still unreachable, or open when it shouldn't be. Test from a second computer:
# Linux and macOS nc -zv 192.168.1.20 8080 # Windows PowerShell Test-NetConnection 192.168.1.20 -Port 8080
If it's listening locally but these fail, the firewall is the next place to look. For Linux servers, my SSH hardening guide includes firewall rules. An unexpected open port, on the other hand, is worth taking seriously. Listing listening ports now and then with ss -ltnup, lsof -iTCP -sTCP:LISTEN or Get-NetTCPConnection -State Listen is one of the simplest ways to spot something running that shouldn't be.
Quick reference
- Linux:
sudo ss -ltnp 'sport = :PORT', thensystemctl status PID - macOS:
sudo lsof -nP -iTCP:PORT -sTCP:LISTEN, and check AirPlay for 5000 and 7000 - Windows:
Get-NetTCPConnection -LocalPort PORT -State Listen,netsh http show servicestatefor PID 4, andnetsh interface ipv4 show excludedportrange protocol=tcpwhen nothing is listening
Comments (0)
No comments yet. Be the first to leave one!
Leave a Comment