Find what is using a port on Linux
To find what is using a port, list listening sockets with ss or lsof and read the process name and ID next to the port.
- Updated Nov 27, 2026
- Markdown
Find which process holds a port and stop it safely to fix address already in use.
- A Linux server with sudo access
- A port number that is already taken
To find what is using a port, run sudo ss -tlnp and read the process name next to the port. You start an app and it exits with address already in use, or Docker reports bind: address already in use. Only one program can listen on a given address and port at a time, and something else already has it. It is often an old copy of the same app, a web server you forgot about, or a container you started last week.
How it works#
A listening port is a socket the kernel has handed to exactly one process (or a group of processes that share it on purpose). When a second program asks for the same address and port, the kernel refuses, and the program prints the error.
The address matters as much as the port number. A listener on 0.0.0.0:8080 takes that port on every IPv4 address and blocks a second listener on any of them. A listener on 127.0.0.1:8080 takes only the loopback address. The IPv6 forms are [::]:8080 for all addresses and [::1]:8080 for loopback.
Two tools read the kernel’s table of sockets:
| Tool | Shows | Notes |
|---|---|---|
ss | Sockets and, with -p, their processes | Installed on Ubuntu by default |
lsof -i | Open network files per process | May need sudo apt install lsof |
Both need sudo to show processes owned by other users. Without it the port appears but the process column is empty.
Step 1: List all listening ports#
sudo ss -tlnpFlags: -t TCP, -l listening, -n numbers instead of names, -p show the process. What you should see: a table with Local Address:Port and, at the end of each row, text like users:(("nginx",pid=812,fd=6)). Add -u too if you are hunting a UDP port.
Step 2: Look at one port#
Replace 8080 with your port:
sudo ss -tlnp | grep ':8080'
sudo lsof -i :8080What you should see: for ss, the matching row with the process name and PID. For lsof, a table with COMMAND, PID and USER columns. If both print nothing, nothing is listening on that port.
Step 3: Identify the process#
ps -p 812 -o pid,user,cmdUse the PID from the previous output. What you should see: one line with the PID, the owner and the full command that started it. If it is a service, check systemctl status nginx. If it is a Docker container, run docker ps and look for a published port in the PORTS column.
Step 4: Free the port#
Prefer stopping a service properly (see Run a service with systemd):
sudo systemctl stop nginxIf the service should stay stopped, also run sudo systemctl disable nginx, otherwise it comes back at the next boot.
Warning:
killends a process immediately and may lose unsaved data. Make sure you know what the PID is first.
sudo kill 812kill sends a polite stop request (SIGTERM) that lets the program clean up. Only use kill -9 if a normal kill does not work, because it gives no chance to save anything. Alternatively, change your own app to a different port.
Check it worked#
Run sudo ss -tlnp | grep ':8080' again. It should print nothing. Then start your app and run the same command: you should see your app’s name on the port. To test from outside the process table, curl -I http://127.0.0.1:8080 should reach your app, not an error page from an old service.
Edge cases and gotchas#
- Loopback and all-interfaces listeners can still conflict. A program on
0.0.0.0:8080blocks one on127.0.0.1:8080in most setups. Read the address column, not only the port. - Listening on
[::]can cover IPv4 too. A dual-stack listener on[::]:80often accepts IPv4 clients as well, sossshows only an IPv6-looking line. Check both IPv4 and IPv6 rows before deciding the port is free. - A Docker-published port shows
docker-proxy, or nothing. Many hosts show adocker-proxyprocess for each published port. If the userland proxy is turned off, the port can be taken by firewall rules with no process inss. Usedocker psto find the container. - Ports inside a container are separate.
sson the host does not list sockets inside a container’s own network namespace. Rundocker exec <container> ss -tlnpif the tool exists in the image. - systemd can hold the port for a service. With socket activation, PID 1 (
systemd) owns the listening socket until the service starts. Checksystemctl list-socketsbefore blaming the app. - Low ports need privileges. Ports below 1024 normally require root or a special capability. Run your app on a high port and put a reverse proxy in front.
- A second copy of your own app is the usual culprit. If a process manager restarts the app in a loop, one instance may still be shutting down. Look for duplicate PIDs.
Troubleshooting#
| Symptom | Likely cause | Fix |
|---|---|---|
bind: address already in use | Another process listens on that address and port | Find it with ss, then stop it or pick another port |
Error starting userland proxy: listen tcp4 0.0.0.0:80: bind: address already in use | The host already uses port 80 (often nginx or Apache) | Stop that service or publish the container on another host port |
ss shows the port but no process name | Not run with sudo, or the owner is another user | Run sudo ss -tlnp |
lsof: command not found | lsof not installed | sudo apt install lsof, or use ss |
Port in ss but curl fails | The service listens on 127.0.0.1 only | Connect from the server, or bind to the right address |
| Nothing listens, error persists | A quick restart left a socket in TIME_WAIT, or two starts race | Wait a few seconds and start once |
permission denied binding port 80 or 443 | Port below 1024 without privileges | Use a high port plus a reverse proxy |
| Port is free on the host but the container will not publish it | A published port is already mapped by another container | Run docker ps and look at the PORTS column |
Real-world scenario#
You start a new app with node server.js and it exits with Error: listen EADDRINUSE: address already in use :::3000. You run sudo ss -tlnp | grep ':3000' and see a node process with a PID.
ps -p shows it was started two days ago by the same user: an earlier test you forgot. You run kill with that PID (a plain SIGTERM), wait a second, check ss again (empty), and start the app. It listens.
The next week the same error appears after you set up nginx. This time ss shows nginx on port 80, so you stop the container that was meant to use that port, or publish it on 8080.
Quick reference#
| Task | Command |
|---|---|
| All TCP listeners | sudo ss -tlnp |
| One port | sudo ss -tlnp | grep ':8080' |
| Same, with lsof | sudo lsof -i :8080 |
| Process details | ps -p PID -o pid,user,cmd |
| Stop a service | sudo systemctl stop NAME |
| Stop a process | sudo kill PID |
Security and safety notes#
- Do not
killa process you cannot name. It may be your database or SSH. - Never stop
sshdon a remote server without a second way in.
Related lessons#
- Run a service with systemd: stop or disable what holds the port
- Essential Linux commands for servers: ps, grep and friends
- Docker networks explained: why container ports differ from host ports
- Publish container ports safely: avoid conflicts and accidental exposure
Next#
Stop typing long connection details with SSH config file shortcuts.
Check what you learned
Frequently asked questions
Why do I need sudo?
Without sudo you only see your own processes. With sudo you see the process name and ID for every listener, including services run by root.
What does 0.0.0.0 versus 127.0.0.1 mean in the output?
0.0.0.0 means the port is open on all network interfaces. 127.0.0.1 means it is reachable only from the server itself.
What do [::] and * mean in the Local Address column?
[::] is the IPv6 equivalent of 0.0.0.0, meaning all IPv6 addresses. On many systems a listener on [::] also accepts IPv4 connections, so one line can cover both.
The port looks free but my app still says address already in use. Why?
A socket in TIME_WAIT or a second copy of your app starting at the same moment can hold it briefly. Wait a few seconds and retry, and check that your own process manager is not starting two copies.
Keep going: guides and tutorials
- Install a Self-Hosted PaaS on Your Own Server in Five Minutes One installer download, one setup token, one sample app. The full Levelrail install on a fresh Linux server, with the checks and the fixes.
- Manage Secrets Self-Hosted: Env Vars and Master Key Rotation Set env vars and encrypted secrets on a self-hosted server, apply changes safely, and rotate the master key with no downtime without locking yourself out.