TExcelTAD
Home › Linux Server Administration › Listening Sockets: ss & ip

Listening Sockets: ss & ip

"The site is down" and "I can't connect to the database" start with the same two questions on the server: is the service listening, and on which address? ss (socket statistics, from iproute2) answers them; it replaced netstat, which is still found on older systems (netstat -tlnp ≈ ss -tlnp). ip shows the addresses and routes the sockets use. Ports & Sockets explained the theory; this is the server-side view.

Who is listening?

The flags to memorise: -t TCP, -u UDP, -l listening, -n numeric (don't resolve names), -p show the process (as root, for every process; as a normal user only for your own). In this session three small servers start on loopback:

bash · try it yourself
python3 -m http.server 8000 --bind 127.0.0.1 > /dev/null 2>&1 &
python3 -m http.server 9000 --bind ::1 > /dev/null 2>&1 &
python3 -c 'import socket, time; s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM); s.bind(("127.0.0.1", 5353)); time.sleep(30)' &
for i in $(seq 50); do curl -s localhost:8000/ > /dev/null && break; sleep 0.1; done
sleep 0.5
echo "--- TCP listeners"
ss -tln
echo "--- UDP sockets"
ss -uln
echo "--- with processes"
ss -tlnp | sed -E 's/pid=[0-9]+/pid=<PID>/; s/fd=[0-9]+/fd=N/'
kill %1 %2 %3
--- TCP listeners
State  Recv-Q Send-Q Local Address:Port Peer Address:PortProcess
LISTEN 0      5          127.0.0.1:8000      0.0.0.0:*
LISTEN 0      5              [::1]:9000         [::]:*
--- UDP sockets
State  Recv-Q Send-Q Local Address:Port Peer Address:PortProcess
UNCONN 0      0          127.0.0.1:5353      0.0.0.0:*
--- with processes
State  Recv-Q Send-Q Local Address:Port Peer Address:PortProcess
LISTEN 0      5          127.0.0.1:8000      0.0.0.0:*    users:(("python3",pid=<PID>,fd=N))
LISTEN 0      5              [::1]:9000         [::]:*    users:(("python3",pid=<PID>,fd=N))

How to read a listener line:

Local Address:Port Meaning Reachable from
127.0.0.1:8000 IPv4 loopback only this machine only
[::1]:9000 IPv6 loopback only this machine only (and not via 127.0.0.1!)
0.0.0.0:80 every IPv4 address every network the host is on (the firewall decides the rest)
[::]:80 or *:80 every IPv6 address (often IPv4 too, as a dual-stack socket) everywhere
10.0.0.21:5432 one specific interface address that network

The column Send-Q on a listening socket is the backlog limit (here 5, Python's default); Recv-Q is how many connections are waiting to be accepted. A Recv-Q that reaches the backlog means the application isn't accepting connections fast enough, and new clients see timeouts.

Two of the most common outage causes are visible right here: the service listens on 127.0.0.1 (the default for many databases and dev servers) while clients connect from another host, or it listens only on IPv6 ::1 while the client tries 127.0.0.1. curl localhost:9000 would work (curl tries ::1 too), curl 127.0.0.1:9000 would not.

Filtering connections

ss has its own filter language: states (state established, state time-wait) and expressions on ports and addresses:

bash · try it yourself
python3 -m http.server 8000 --bind 127.0.0.1 > /dev/null 2>&1 &
for i in $(seq 50); do curl -s localhost:8000/ > /dev/null && break; sleep 0.1; done
python3 -c 'import socket, time; c = socket.create_connection(("127.0.0.1", 8000)); time.sleep(5)' &
sleep 0.5
echo "--- established connections to or from port 8000"
ss -Htn state established '( dport = :8000 or sport = :8000 )' | sed -E 's/:[0-9]{5}( |$)/:<EPHEMERAL>\1/' | sort
echo "--- count of established connections to :8000"
ss -Htn state established '( sport = :8000 )' | wc -l
kill %1 %2
--- established connections to or from port 8000
0      0      127.0.0.1:8000  127.0.0.1:<EPHEMERAL>
0      0      127.0.0.1:<EPHEMERAL> 127.0.0.1:8000
--- count of established connections to :8000
1

Each TCP connection appears twice on a single host: once from the client side (an ephemeral port to :8000), once from the server side. On a real server you only see the server side, and the useful one-liners are:

Question Command
How many clients are connected to Nginx? ss -Htn state established '( sport = :443 )' | wc -l
Which hosts connect most? ss -Htn state established '( sport = :443 )' | awk '{print $4}' | cut -d: -f1 | sort | uniq -c | sort -rn | head
Too many TIME-WAIT sockets (short connections without keep-alive)? ss -Htn state time-wait | wc -l
Connections stuck opening (firewall or dead upstream)? ss -tn state syn-sent
Summary counters ss -s

ss -tni adds TCP internals per connection (RTT, congestion window, retransmissions): the first stop when a single client complains about slowness.

Addresses and routes with ip

bash · try it yourself
ip -br addr
ip -br link
echo "routes: $(ip route | wc -l)"
lo               UNKNOWN        127.0.0.1/8 ::1/128
lo               UNKNOWN        00:00:00:00:00:00 <LOOPBACK,UP,LOWER_UP>
routes: 0

The sandbox only has loopback, so here is the same on a typical cloud VM (sample session):

bash
$ ip -br addr
lo               UNKNOWN        127.0.0.1/8 ::1/128
ens5             UP             10.0.1.21/24 metric 100 fe80::8ff:fe3a:1c22/64
docker0          DOWN           172.17.0.1/16
$ ip route
default via 10.0.1.1 dev ens5 proto dhcp src 10.0.1.21 metric 100
10.0.1.0/24 dev ens5 proto kernel scope link src 10.0.1.21 metric 100
172.17.0.0/16 dev docker0 proto kernel scope link src 172.17.0.1 linkdown
$ ip route get 10.20.5.9
10.20.5.9 via 10.0.1.1 dev ens5 src 10.0.1.21 uid 1000
    cache
$ ip -s link show ens5 | sed -n '3,6p'
    RX:  bytes packets errors dropped  missed   mcast
    9128840212 6411893      0     412       0       0
    TX:  bytes packets errors dropped carrier collsns
    2711502344 3020117      0       0       0       0

ip route get <address> answers "which interface and source address would the kernel use to reach this host?", which settles most "why does traffic go out the wrong way" questions. Growing errors or dropped counters on an interface point at the network or at a host that can't keep up. Note the cloud VM's private address: the public IP of a cloud instance usually isn't on any interface at all; it is NAT'd by the provider.

Test yourself

ss -tln shows 127.0.0.1:5432. Can another server connect to this PostgreSQL?

  1. Yes, if the firewall allows it
  2. No, it listens on loopback only; change listen_addresses
  3. Only over IPv6
  4. Only with SSL

What does Recv-Q mean on a listening socket?

  1. Bytes received
  2. Connections waiting to be accepted by the application; if it reaches the backlog (Send-Q), new connections stall
  3. Packets dropped
  4. The number of threads

Which command shows which interface and source address the kernel uses to reach 10.20.5.9?

  1. ip addr
  2. ip route get 10.20.5.9
  3. ss -tn
  4. ping -r 10.20.5.9