# 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.

URL: https://levelrail.com/learn/server-basics/find-what-is-using-a-port/
Updated: 2026-11-27

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](https://levelrail.com/learn/glossary/#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

```bash
sudo ss -tlnp
```

Flags: `-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:

```bash
sudo ss -tlnp | grep ':8080'
sudo lsof -i :8080
```

What 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

```bash
ps -p 812 -o pid,user,cmd
```

Use 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](https://levelrail.com/learn/docker/install-docker-on-ubuntu/)) 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](https://levelrail.com/learn/server-basics/run-a-service-with-systemd/))):

```bash
sudo systemctl stop nginx
```

If the service should stay stopped, also run `sudo systemctl disable nginx`, otherwise it comes back at the next boot.

> Warning: `kill` ends a process immediately and may lose unsaved data. Make sure you know what the PID is first.

```bash
sudo kill 812
```

`kill` 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:8080` blocks one on `127.0.0.1:8080` in most setups. Read the address column, not only the port.
- **Listening on `[::]` can cover IPv4 too.** A dual-stack listener on `[::]:80` often accepts IPv4 clients as well, so `ss` shows 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 a `docker-proxy` process for each published port. If the userland proxy is turned off, the port can be taken by firewall rules with no process in `ss`. Use `docker ps` to find the container.
- **Ports inside a container are separate.** `ss` on the host does not list sockets inside a container's own network namespace. Run `docker exec <container> ss -tlnp` if 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. Check `systemctl list-sockets` before 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 `kill` a process you cannot name. It may be your database or SSH.
- Never stop `sshd` on a remote server without a second way in.

## Related lessons

- [Run a service with systemd](https://levelrail.com/learn/server-basics/run-a-service-with-systemd/)): stop or disable what holds the port
- [Essential Linux commands for servers](https://levelrail.com/learn/server-basics/essential-linux-commands-for-servers/)): ps, grep and friends
- [Docker networks explained](https://levelrail.com/learn/docker/docker-networks-explained/)): why container ports differ from host ports
- [Publish container ports safely](https://levelrail.com/learn/docker/publish-container-ports-safely/)): avoid conflicts and accidental exposure

## Next

Stop typing long connection details with [SSH config file shortcuts](https://levelrail.com/learn/server-basics/ssh-config-file-shortcuts/)).

## 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.
