Chapter 1 · Lesson 18 of 19
Server basics

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.

By the end you will

Find which process holds a port and stop it safely to fix address already in use.

Time
12 min
Level
Beginner
Check
4 questions
You need
  • 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:

ToolShowsNotes
ssSockets and, with -p, their processesInstalled on Ubuntu by default
lsof -iOpen network files per processMay 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 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):

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#

SymptomLikely causeFix
bind: address already in useAnother process listens on that address and portFind 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 useThe 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 nameNot run with sudo, or the owner is another userRun sudo ss -tlnp
lsof: command not foundlsof not installedsudo apt install lsof, or use ss
Port in ss but curl failsThe service listens on 127.0.0.1 onlyConnect from the server, or bind to the right address
Nothing listens, error persistsA quick restart left a socket in TIME_WAIT, or two starts raceWait a few seconds and start once
permission denied binding port 80 or 443Port below 1024 without privilegesUse a high port plus a reverse proxy
Port is free on the host but the container will not publish itA published port is already mapped by another containerRun 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#

TaskCommand
All TCP listenerssudo ss -tlnp
One portsudo ss -tlnp | grep ':8080'
Same, with lsofsudo lsof -i :8080
Process detailsps -p PID -o pid,user,cmd
Stop a servicesudo systemctl stop NAME
Stop a processsudo 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.

Next#

Stop typing long connection details with SSH config file shortcuts.

Check what you learned4 questions
1What does sudo ss -tlnp list?
2A port shows 127.0.0.1:5432. Who can reach it?
3What should you try first to free a port used by nginx?
4ss shows the port held by docker-proxy. What is the right fix?

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.

View MD Edit
Share
Helpful?
Progress is saved in this browser only.

Keep going: guides and tutorials

View MD Edit
Share
Helpful?
Run this on your own server

Levelrail deploys apps from git with health-checked releases and one-click rollback.

docker pull ghcr.io/glincker/levelrail:latest Install Levelrail From GLINR Studios