Skip to main content

Web servers and networking · Beginner · 14 min

Find what is listening on a port with ss

By Padding · Updated 18 Nov 2026
Ubuntu 26.04.1 LTS · iproute2 6.19

You will use ss to list every listening socket on an Ubuntu 26.04 server, read which addresses each one accepts, find the process behind an "address already in use" error, filter and count live connections, and confirm a port is reachable from another machine with nc.

On this page

Most network problems on a server start with one question: is anything actually listening on that port, and on which address? A site that loads on the server but not from your laptop, a deploy that fails with "address already in use", a port you did not know was open: ss answers all three in one line.

ss ("socket statistics") is part of iproute2, which every Ubuntu 26.04 server has installed. It replaces the older netstat, reads socket data straight from the kernel, and stays fast on busy machines with thousands of connections.

In this tutorial you will run three listeners on purpose (Nginx on port 80, and two small Python web servers bound to different addresses), then use ss to tell them apart, cause and fix a port clash, look at UDP and connection states, count connections per client, compare ss with lsof and fuser, and finally test the ports from another computer with nc.

Step 1 · Install the tools and start some listeners

Make sure the tools are present. ss comes with iproute2, fuser with psmisc, and nc with netcat-openbsd. Most of them are already on a stock server, so this command usually installs little or nothing:

Bash
sudo apt update
sudo apt install -y iproute2 lsof psmisc netcat-openbsd python3 curl

Make sure Nginx is running. If it already is, this does nothing:

Bash
sudo systemctl start nginx

Now start two Python web servers in a throwaway folder. The first is bound to 127.0.0.1, the loopback address, so only programs on the server itself can reach it. The second has no --bind, so Python binds it to every address the server has.

Bash
mkdir -p ~/ss-demo
cd ~/ss-demo
nohup python3 -m http.server 8000 --bind 127.0.0.1 > loopback.log 2>&1 &
nohup python3 -m http.server 8080 > all.log 2>&1 &
sleep 1

Check it worked

Ask each listener for a page from the server itself:

Bash
for port in 80 8000 8080; do
  curl -s -o /dev/null -w "port $port: %{http_code}\n" "http://127.0.0.1:$port/"
done

All three lines end in 200. If one prints 000, that server is not running; check ~/ss-demo/loopback.log or ~/ss-demo/all.log for the error.

Step 2 · List the listening TCP ports

This is the command you will type most often:

Bash
sudo ss -tlnp

The letters mean:

  • -t: TCP sockets only.
  • -l: listening sockets only, the ones waiting for new connections.
  • -n: show numbers (80, 127.0.0.1) instead of looking up names (http, localhost). It is faster and avoids guessing what a name maps to.
  • -p: show the process that owns each socket. It needs sudo to see processes that belong to other users, such as Nginx, which runs as root.

The output is a table. Your process IDs will differ, and your server shows more lines, for example sshd on port 22 and systemd-resolved on 127.0.0.53%lo:53:

Text
State  Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0      511          0.0.0.0:80        0.0.0.0:*     users:(("nginx",pid=812,fd=6),("nginx",pid=811,fd=6))
LISTEN 0      5          127.0.0.1:8000      0.0.0.0:*     users:(("python3",pid=1203,fd=3))
LISTEN 0      5                  *:8080            *:*     users:(("python3",pid=1204,fd=3))

Read the columns from left to right:

  • State is LISTEN for every row, because -l asked only for listeners.
  • Recv-Q on a listening socket is the number of connections that have arrived but that the program has not picked up yet. It should sit at or near 0. A number that stays high means the program is too slow to accept new connections.
  • Send-Q on a listening socket is the backlog limit: how many of those waiting connections the kernel will queue before it starts refusing more. Nginx asks for 511, Python's server for 5.
  • Local Address:Port is what this tutorial is about. The next step reads it closely.
  • Peer Address:Port is * for a listener, because no one is connected to it yet.
  • Process names the program and its PID. Nginx has a master process and one or more workers, and all of them share the socket, so it shows several entries.

Check it worked

You can see three rows with your listeners in them: port 80 owned by nginx, and ports 8000 and 8080 owned by python3. Run the command again without sudo (ss -tlnp). The rows are still there, but the Process column is empty for Nginx, because your user cannot see root's processes. That is why the habit is sudo ss -tlnp.

Step 3 · Read the local address

The address in front of the port decides who can connect. This is the column that explains "it works on the server but not from outside".

  • 0.0.0.0:80 means every IPv4 address on the server. Anyone who can reach any of the server's IPv4 addresses can connect, unless a firewall blocks them.
  • 127.0.0.1:8000 means loopback only. Programs on the same server can connect; nothing on the network can, whatever the firewall says. Databases and app servers behind a reverse proxy are often bound here on purpose.
  • [::]:80 means every IPv6 address, and only IPv6. Ubuntu's own Nginx package listens on both 0.0.0.0:80 and [::]:80, so it shows two rows for the same port. With the nginx.org package's default site you may see only the 0.0.0.0:80 row; adding listen [::]:80; to a server block adds the IPv6 one.
  • *:8080 means every address, IPv4 and IPv6 together, on one socket. Python's server does this when you give it no --bind. On some servers it shows [::]:8080 or 0.0.0.0:8080 instead, depending on how IPv6 is set up.
  • A specific address such as 10.0.0.5:5432 means the program accepts connections only on that one interface, for example a private network.

To look at a single port, add a filter. The quotes keep the shell from treating the spaces and colon specially:

Bash
sudo ss -tlnp 'sport = :8000'

sport is the source port, which for a listener is its own port. Only the 127.0.0.1:8000 row is printed.

Check it worked

Find your server's own non-loopback address (ip -br addr lists it; the ip tutorial explains the output), then try both Python servers on that address from the server:

Bash
SERVER_IP=$(ip -4 -o addr show scope global | awk '{print $4}' | cut -d/ -f1 | head -n 1)
echo "Server address: $SERVER_IP"
curl -s -o /dev/null -w "8080: %{http_code}\n" "http://$SERVER_IP:8080/"
curl -s -o /dev/null -w "8000: %{http_code}\n" "http://$SERVER_IP:8000/" || true

Port 8080 returns 200. Port 8000 returns 000, because the connection is refused: it listens on 127.0.0.1 only, and $SERVER_IP is a different address. The ss output told you this before you tried.

Step 4 · Find the process behind "address already in use"

A program that tries to listen on a port another program already holds fails at once. Start a second server on port 8000 to see the error:

Bash
python3 -m http.server 8000 --bind 127.0.0.1

Python prints a traceback that ends with OSError: [Errno 98] Address already in use. Nginx says bind() to 0.0.0.0:80 failed (98: Address already in use), Node.js says EADDRINUSE; the cause is the same.

Ask ss who holds the port. -H drops the header line, which is handy when you only want the answer:

Bash
sudo ss -tlnpH 'sport = :8000'

The Process column gives the owner, something like users:(("python3",pid=1203,fd=3)). Now decide what to do with it:

  • If it is a service you know, such as a second copy of your app or an old Apache install holding port 80, stop it the proper way: sudo systemctl stop <service>, and sudo systemctl disable <service> if it should not come back after a reboot.
  • If it is a stray process you started yourself, as here, stop it by its PID.

Before you run kill, read the PID from your own output and check the name next to it. Stopping the wrong PID can take down something else on the server. Replace <PID> with the number you saw:

Bash
kill <PID>

Check it worked

Bash
ss -tlnH 'sport = :8000' | wc -l

It prints 0: nothing listens on port 8000 any more. Start the loopback server again, because the last step uses it:

Bash
cd ~/ss-demo
nohup python3 -m http.server 8000 --bind 127.0.0.1 > loopback.log 2>&1 &
sleep 1
ss -tlnH 'sport = :8000'

The 127.0.0.1:8000 row is back.

Step 5 · Check UDP ports

DNS, NTP, WireGuard and many game and voice services use UDP. Swap -t for -u to see UDP sockets. Start a small UDP listener first; -d tells nc not to read from your keyboard, so it can run in the background:

Bash
nc -d -u -l 127.0.0.1 9999 > ~/ss-demo/udp.log &
UDP_PID=$!
sleep 1
sudo ss -ulnp

UDP has no connections to set up, so the state column reads UNCONN instead of LISTEN. Look for 127.0.0.1:9999 owned by nc. On a stock server you also see something like 127.0.0.53%lo:53 for systemd-resolved, the local DNS cache, and 127.0.0.1:323 or [::1]:323 for chrony's command port.

To see TCP and UDP listeners together, combine the letters: sudo ss -tulnp.

Check it worked

Send the listener a message and read what it received:

Bash
echo hello | nc -u -w 1 127.0.0.1 9999
sleep 1
cat ~/ss-demo/udp.log
kill "$UDP_PID"

hello is printed. A UDP port that shows in ss but never receives anything from outside usually means a firewall or a cloud security group in front of the server drops it.

Step 6 · Look at connections and their states

Without -l, ss shows sockets that are in use. Without -n it would try to resolve every address, so keep it. Open three idle connections to Nginx so there is something to see, and send twenty quick requests:

Bash
NC_PIDS=""
for i in 1 2 3; do
  nc -d 127.0.0.1 80 &
  NC_PIDS="$NC_PIDS $!"
done
for i in $(seq 1 20); do curl -s -o /dev/null http://127.0.0.1/; done
sleep 1

Every TCP connection moves through named states. The ones you meet most:

  • ESTAB (established): a live connection.
  • TIME-WAIT: the connection is closed, and the side that closed first keeps a record of it for about a minute on Linux, so late packets from the old connection are not mixed into a new one. Lots of these after a burst of short requests is normal.
  • CLOSE-WAIT: the other side has closed, but the program on this server has not closed its end yet. A count that keeps rising points to a bug in that program.
  • SYN-SENT and SYN-RECV: a connection still being set up. Many SYN-SENT sockets mean this server is trying to reach something that does not answer.

Ask for one state with state. -a includes every socket:

Bash
ss -tan state time-wait

You see roughly twenty rows from the curl requests, each with 127.0.0.1:80 on one side. When you filter on a single state, ss leaves out the State column, because every row would say the same thing.

Filters combine with and, or and brackets. These show the established connections to your web server, then everything to or from port 80:

Bash
ss -tn state established '( sport = :80 )'
ss -tan '( sport = :80 or dport = :80 )'

For HTTPS, use sport = :443. To see connections this server makes to a database, filter on the destination instead: dport = :5432.

Check it worked

Count established connections to port 80 per client address. With the State column gone, the peer address is the fourth column; awk strips the port from it:

Bash
ss -tnH state established '( sport = :80 )' \
  | awk '{ sub(/:[0-9]+$/, "", $4); print $4 }' \
  | sort | uniq -c | sort -rn

It prints 3 127.0.0.1: your three idle nc connections. On a public server this list shows which addresses hold the most connections, which is the first thing to check when the server slows down under load. Many connections from one address are not always an attack. Mobile networks in many African countries, and office networks everywhere, put hundreds of users behind one shared address (carrier-grade NAT), so treat a high count as a lead, not proof.

For totals, ss -s gives a summary by protocol and state:

Bash
ss -s

The TCP: line includes something like estab 6 (the three connections counted from both ends) and a timewait figure. Close the idle connections when you are done:

Bash
kill $NC_PIDS

Step 7 · Cross-check with lsof and fuser

Two older tools answer the same question from the other direction: they start from processes and open files rather than from the kernel's socket tables. They are useful when you already know the port and want only the owner, and their output is familiar from many other guides.

lsof lists open files, and on Linux a socket is a file. -i selects network files, -sTCP:LISTEN keeps only listeners, and -n -P turns off name lookups as -n does for ss:

Bash
sudo lsof -nP -iTCP:8080 -sTCP:LISTEN

The row starts with python3 and its PID, and ends with something like TCP *:8080 (LISTEN). Drop the port (sudo lsof -nP -iTCP -sTCP:LISTEN) to list all TCP listeners.

fuser prints the processes using a port. -v adds the user and command name:

Bash
sudo fuser -v 80/tcp

You see the Nginx master and worker processes, owned by root and nginx.

fuser also has -k, which kills every process using the port. Do not use it on a port held by a service: it stops the processes without telling systemd, which may restart them at once, and it gives no chance to check what you are about to kill. Prefer systemctl stop or kill with a PID you have read.

Check it worked

All three tools agree: ss, lsof and fuser name python3 for port 8080 and nginx for port 80. If they ever disagree, trust ss for which sockets exist and on which address, because it reads the kernel's socket tables directly. All three tools find the owning process by scanning /proc, so run the command again if a process has just started or stopped.

Step 8 · Check a port from another machine

ss shows what the server is willing to accept. Whether a connection actually arrives also depends on every firewall in between: UFW on the server, a cloud provider's security group, and sometimes your own network. Test from outside with nc. -z connects and hangs up without sending data, -v prints the result, and -w 3 gives up after three seconds.

On your laptop, replace 203.0.113.10 with your server's public address:

Bash
# On your laptop
nc -vz -w 3 203.0.113.10 80
nc -vz -w 3 203.0.113.10 8080
nc -vz -w 3 203.0.113.10 8000

macOS includes a compatible nc. On Windows, PowerShell's Test-NetConnection 203.0.113.10 -Port 80 does the same job.

Read the result together with ss:

  • succeeded (or open): the port is listening and nothing in between blocks it.
  • Connection refused, answered at once: the packets reached the server, and nothing listens on that address and port. Port 8000 gives this, because it is bound to 127.0.0.1. Check the Local Address column in ss.
  • timed out after three seconds: something dropped the packets on the way. ss shows a listener, so look at the firewall. If UFW is on, port 8080 times out until you allow it, as described in the UFW tutorial; then check the security group in your cloud console.

Each check sends only a handful of packets, so it costs almost nothing even on prepaid mobile data.

Check it worked

Port 80 succeeds from your laptop, port 8000 is refused or times out, and you can say why for each from the ss -tlnp output you read earlier. If 80 does not succeed, open 80 in UFW and your cloud firewall and try again.

When you have finished, stop the Python servers and remove the demo folder. This deletes ~/ss-demo and everything in it, so check you have not saved anything else there. Nginx keeps running.

Bash
pkill -f 'python3 -m http.server 8000'
pkill -f 'python3 -m http.server 8080'
rm -rf ~/ss-demo

Next steps

  • See the addresses, routes and neighbours that sit beneath these ports in /tutorials/see-addresses-and-routes-with-the-ip-command.
  • Decide which of your listening ports the internet may reach with /tutorials/set-up-ufw-firewall-ubuntu-26-04.
  • Bind an app to 127.0.0.1 and publish it only through Nginx with /tutorials/nginx-reverse-proxy-for-a-node-app.
  • Follow a request from the browser to your listening socket in /tutorials/how-a-request-reaches-your-server.

Need this done for you?

Padding does this work for growing companies, from the first diagnosis to a system that keeps working.

Platform Engineering

Get new tutorials

New tutorials, about twice a month.