The Command Most People Need
sudo ss -tulpn
- -t: TCP sockets.
- -u: UDP sockets.
- -l: listening sockets only.
- -p: the process that owns each socket. Without root, ss shows only your own user's processes, so run it with sudo.
- -n: numbers instead of names,
:443rather than:https.
Example output, shortened:
Netid State Local Address:Port Peer Address:Port Process
udp UNCONN 127.0.0.53%lo:53 0.0.0.0:* users:(("systemd-resolve",pid=601,fd=13))
tcp LISTEN 0.0.0.0:22 0.0.0.0:* users:(("sshd",pid=812,fd=3))
tcp LISTEN 127.0.0.1:3306 0.0.0.0:* users:(("mariadbd",pid=990,fd=20))
tcp LISTEN *:80 *:* users:(("apache2",pid=1022,fd=4))
The real output also has Recv-Q and Send-Q columns. For a listening TCP socket, Send-Q shows the maximum length of its accept queue (the backlog) and Recv-Q how many connections wait in it.
Reading the Local Address
| Shown | Listens on |
|---|---|
0.0.0.0:22 | Every IPv4 address |
[::]:22 | Every IPv6 address only |
*:80 | IPv4 and IPv6 (dual stack) |
127.0.0.1:3306 | The server itself only |
203.0.113.10:443 | One address only |
A service that shows only 127.0.0.1 or [::1] cannot be reached from outside, whatever the firewall allows. A suffix such as %lo names the interface a socket is bound to. Loopback is explained in what is localhost.
Listening vs Connected Sockets
ss -tln # listening TCP sockets
ss -tn # open TCP connections
ss -tan # both
Without -l or -a, ss shows connections and leaves listening sockets out. For a connection, Local is your server's side and Peer the other end. Recv-Q and Send-Q show bytes waiting to be read by the program or to be acknowledged by the peer; numbers that stay high point at a slow program or a slow network.
Filter by State
ss -tn state established
ss -tn state time-wait
ss -tn state syn-recv
When you filter by one state, the State column is left out of the output. Many sockets in time-wait are normal on a busy web server. A large, growing number in syn-recv means many connections are started and never completed.
Filter by Port and Address
ss -tn 'sport = :443'
ss -tn 'dport = :3306'
ss -tn 'dst 203.0.113.50'
ss -tn state established '( sport = :80 or sport = :443 )'
sudo ss -tlnp 'sport = :22'
sport is the local port, dport the remote port, and dst the remote address. Quote filters so the shell leaves the parentheses alone. The last line shows which program listens on port 22; lsof answers the same question from another angle.
Count Connections
ss -s
prints totals per protocol and state. To count established connections, then list remote addresses by number of connections:
ss -Htn state established | wc -l
ss -Htn state established | awk '{print $4}' | sed 's/:[^:]*$//' | sort | uniq -c | sort -rn | head
-H leaves out the header line. With only TCP and one state selected, ss also drops the Netid and State columns, so the fourth field is the peer address. The second command helps when one client opens far more connections than the rest. The text tools in it are covered in awk and sort.
Other Useful Options
| Option | Shows |
|---|---|
-4, -6 | IPv4 or IPv6 only |
-x | Unix domain sockets |
-o | Timers, such as keepalive |
-i | TCP details: RTT, window |
-e | Owner user and inode |
ss, netstat, and nmap
ss shows what the server listens on; it cannot tell whether a firewall lets anyone reach it. Check that from outside with nmap. The older tool with nearly the same letters is the netstat command, and a wider overview is in how to check open ports in Linux.