Skip to content

/cheatsheets/ss/ss-cheat-sheet

ss cheat sheet: sockets, ports and connections on Linux

The commands on this page have not been executed by our verification runner yet.updated
tool
ss / netstat
versions
iproute2 5.x / 6.x
os
linux

ss lists sockets on Linux. The combination used most is ss -tulpn (listening TCP and UDP ports with their processes); add sudo to see processes of other users. Everything below works with iproute2 5.x and 6.x.

Select sockets

CommandShows
ssConnected (non-listening) sockets of all kinds
ss -t / -u / -x / -wTCP / UDP / Unix / raw sockets
ss -lListening sockets only
ss -aListening and connected
ss -4 / -6IPv4 / IPv6 only
ss -tulpnListening TCP+UDP, numeric, with process
ss -tanAll TCP sockets, numeric

Output options

OptionAdds or changes
-nNumeric ports and addresses (no service or host names)
-rResolve host names
-pProcess name, PID and file descriptor
-eExtended info: uid, inode, cookie
-oTimers (keepalive, on, retrans)
-iTCP internals: rtt, cwnd, retransmissions, bytes
-mSocket memory usage
-HNo header line (for scripts)
-sSummary totals
-KKill matching sockets (needs root and kernel support)

Filters

bash
ss -tn state established
ss -tn state time-wait
ss -tn state established '( dport = :443 or sport = :443 )'
ss -tn dst 203.0.113.10
ss -tn src 192.0.2.0/24
ss -ltn 'sport = :8080'
ss -tn 'dport >= :1024'
ss -xp | grep docker.sock

State names: established, syn-sent, syn-recv, fin-wait-1, fin-wait-2, time-wait, closed, close-wait, last-ack, listening, closing, plus the groups all, connected, synchronized, bucket, big. Expressions use sport, dport, src, dst with =, !=, <, >, <=, >= and combine with and, or, not and parentheses; quote them so the shell does not interpret the parentheses.

Recipes

bash
sudo ss -ltnp 'sport = :80'                       # who listens on port 80
sudo ss -tnp state established '( dport = :5432 )'  # which processes talk to PostgreSQL
ss -Htan | awk '{print $1}' | sort | uniq -c      # count by state
ss -tni dst 203.0.113.10                          # rtt and retransmits to one host
ss -tn state close-wait                           # sockets the application forgot to close
sudo ss -K dst 203.0.113.10 dport = :443          # drop connections to one peer (careful)

Sources