SkillPilotAI

ss

ss (socket statistics) dumps every kind of Linux socket: TCP, UDP, Unix, raw, packet, netlink and MPTCP. It is part of iproute2, installed by default on every distribution, and is the modern, faster replacement for netstat. Windows and macOS have no ss; use netstat there.

70.1 How it works: ss vs netstat

  • ss sends one request over netlink sock_diag; the filter travels to the kernel in the same request.
  • The kernel replies with only the matching sockets, in binary: fast even with many connections.
  • netstat makes the kernel write every socket into text files like /proc/net/tcp, then parses them line by line.
  • That is why netstat gets slow with thousands of connections.
Where the data comes from
  1. 1.ss: request + filter over netlink sock_diag

  2. 2.Kernel filters its socket tables itself

  3. 3.Only matching sockets return to ss in binary

  4. 4.netstat: kernel writes all sockets to /proc/net/tcp as text

  5. 5.netstat parses every line in userspace (slow at scale)

Lab: one TCP connection from cli to srv
  1. 1.cli 192.168.90.20 sends SYN through the n070-lan switch

  2. 2.SYN reaches srv 192.168.90.10 port 443

  3. 3.SYN-ACK from srv comes back to cli; cli sends ACK

  4. 4.ss -tn on cli shows one row: ESTAB 192.168.90.20:port -> 192.168.90.10:443

70.2 General and output format

  • No options: non-listening sockets of all types (Unix sockets make it long).
  • -t TCP, -l listening only, -n numeric, -a all (adds LISTEN and TIME-WAIT), -H no header.
  • -r resolves IPs to hostnames; without -n, port 7000 shows as bbs: always use -n in scripts.
  • -V shows the iproute2 version.
Terminal
ssss -tln               # srvss -tlnH              # srvss -tass -trss -tl                # srvss -tnss -V
Output
$ ssNetid State      Recv-Q Send-Q Local Address:Port     Peer Address:Portu_str ESTAB      0      0      /tmp/app.sock 1519383             * 1518222u_dgr ESTAB      0      0                  * 1519369             * 1519368tcp   CLOSE-WAIT 1      0      192.168.90.20:45262   192.168.90.10:bbstcp   ESTAB      0      0      192.168.90.20:36998   192.168.90.10:httpstcp   ESTAB      0      0      192.168.90.20:57226   192.168.90.10:httpsrv$ ss -tlnState  Recv-Q Send-Q Local Address:Port  Peer Address:PortLISTEN 0      5            0.0.0.0:443        0.0.0.0:*LISTEN 0      5            0.0.0.0:80         0.0.0.0:*LISTEN 0      5            0.0.0.0:7000       0.0.0.0:*LISTEN 0      4096               *:5201             *:*srv$ ss -tlnHLISTEN 0      5         0.0.0.0:443   0.0.0.0:*$ ss -taState      Recv-Q Send-Q Local Address:Port   Peer Address:PortCLOSE-WAIT 1      0      192.168.90.20:45262 192.168.90.10:bbsTIME-WAIT  0      0      192.168.90.20:41066 192.168.90.10:httpESTAB      0      0      192.168.90.20:36998 192.168.90.10:https$ ss -trESTAB      0      0                cli:36998 n070-srv.n070-lan:httpssrv$ ss -tlLISTEN 0      5            0.0.0.0:https      0.0.0.0:*LISTEN 0      5            0.0.0.0:bbs        0.0.0.0:*$ ss -tnCLOSE-WAIT 1      0      192.168.90.20:45262 192.168.90.10:7000$ ss -Vss utility, iproute2-6.9.0

70.3 Extra socket information

  • -p: process name, pid and fd (python3 owns port 80 on srv).
  • -e: uid, inode, cookie; -m: socket memory (skmem); -i: TCP internals (rtt, cwnd, mss, rto).
  • -O prints everything on one line; --tos, --cgroup, --inet-sockopt add more detail.
  • -Z needs SELinux (error here); -T threads; -b attached BPF filters.
Terminal
ss -tlnp              # srvss -tnpss -tne dst :443ss -tnm dst :443ss -tni dst :443ss -tiO dst :443ss -tn --tos dst :443ss -tn --cgroup dst :443ss -tnZ
Output
srv$ ss -tlnpState  Recv-Q Send-Q Local Address:Port  Peer Address:PortProcessLISTEN 0      5            0.0.0.0:443        0.0.0.0:*    users:(("socat",pid=14,fd=5))LISTEN 0      5            0.0.0.0:80         0.0.0.0:*    users:(("python3",pid=7,fd=3))LISTEN 0      4096               *:5201             *:*    users:(("iperf3",pid=26,fd=3))$ ss -tnpESTAB      0      0      192.168.90.20:36998 192.168.90.10:443  users:(("socat",pid=33,fd=5))$ ss -tne dst :443ESTAB 0      0      192.168.90.20:43512 192.168.90.10:443  ino:1520906 sk:6040 cgroup:/ <->$ ss -tnm dst :443ESTAB 0      0      192.168.90.20:43512 192.168.90.10:443	 skmem:(r0,rb131072,t0,tb87040,f0,w0,o0,bl0,d0)$ ss -tni dst :443ESTAB 0      0      192.168.90.20:43512 192.168.90.10:443	 cubic wscale:10,10 rto:204 rtt:0.128/0.064 mss:1448 pmtu:1500 rcvmss:536 advmss:1448 cwnd:10 ...$ ss -tiO dst :443ESTAB 0      0      192.168.90.20:43512 192.168.90.10:https cubic wscale:10,10 rto:204 rtt:0.128/0.064 ...$ ss -tn --tos dst :443ESTAB 0      0      192.168.90.20:43512 192.168.90.10:443  tos:0 class_id:0$ ss -tn --cgroup dst :443ESTAB 0      0      192.168.90.20:43512 192.168.90.10:443  cgroup:/$ ss -tnZss: SELinux is not enabled.

70.4 Actions, summary, namespaces and files

  • -s: socket counts without parsing the full list (fast on big servers).
  • -N blue: look inside a network namespace (the 8080 server is not visible without it).
  • -D dumps raw TCP info to a file; -F reads the filter from a file.
  • -K force-closes matching sockets, but needs CONFIG_INET_DIAG_DESTROY (off in this WSL kernel, so the port 80 connection stayed ESTAB).
  • -E prints sockets live as they are destroyed.
Terminal
ss -sip netns add blueip netns exec blue python3 -m http.server 8080 --bind 127.0.0.1 &ss -N blue -tlnss -D /tmp/tcp.dump state establishedprintf 'dport = :443\n' > /tmp/filter.txtss -tn -F /tmp/filter.txtss -K -t dst 192.168.90.10 dport = :80ss -tnzcat /proc/config.gz | grep -E 'INET_DIAG(_DESTROY)?[ =]'
Output
$ ss -sTotal: 22TCP:   55 (estab 4, closed 50, orphaned 0, timewait 2) Transport Total     IP        IPv6RAW	  0         0         0UDP	  1         1         0TCP	  5         5         0INET	  6         6         0$ ss -N blue -tlnState  Recv-Q Send-Q Local Address:Port Peer Address:PortLISTEN 0      5          127.0.0.1:8080      0.0.0.0:*$ ss -D /tmp/tcp.dump state established; ls -l /tmp/tcp.dump-rw-r--r--    1 root     root           412 Nov  8 21:16 /tmp/tcp.dump$ cat /tmp/filter.txt; ss -tn -F /tmp/filter.txtdport = :443State Recv-Q Send-Q Local Address:Port   Peer Address:PortESTAB 0      0      192.168.90.20:36998 192.168.90.10:443ESTAB 0      0      192.168.90.20:43512 192.168.90.10:443$ ss -K -t dst 192.168.90.10 dport = :80State Recv-Q Send-Q Local Address:Port Peer Address:Port$ ss -tnESTAB      0      0      192.168.90.20:57226 192.168.90.10:80$ zcat /proc/config.gz | grep -E 'INET_DIAG(_DESTROY)?[ =]'CONFIG_INET_DIAG=y# CONFIG_INET_DIAG_DESTROY is not set

70.5 Socket types and -A tables

  • -4 / -6 IPv4 / IPv6; -t, -u, -w raw, -x Unix, -0 packet, -M MPTCP, -S SCTP.
  • UDP sockets show UNCONN (no connection state).
  • -0 -p shows the tcpdump packet socket; -0 -b prints its BPF filter.
  • -f netlink: the tcpdiag:ss socket belongs to ss itself.
  • -A picks tables by name: all, inet, unix_stream, packet_raw...; ! excludes ('all,!tcp').
Terminal
ss -4 -tnss -u -a              # srvss -x -atcpdump -i eth0 -n port 443 -w /dev/null &ss -0 -pss -0 -bss -f netlinkss -a -A 'all,!tcp'ss -a -A unix_stream
Output
srv$ ss -u -aState  Recv-Q Send-Q Local Address:Port   Peer Address:PortUNCONN 0      0            0.0.0.0:domain      0.0.0.0:*$ ss -x -aNetid State  Recv-Q Send-Q Local Address:Port    Peer Address:Portu_str LISTEN 0      5      /tmp/app.sock 1519370            * 0u_str ESTAB  0      0      /tmp/app.sock 1519383            * 1518222u_dgr ESTAB  0      0                  * 1519369            * 1519368$ ss -0 -pNetid Recv-Q Send-Q Local Address:Port Peer Address:PortProcessp_raw 0      0                  *:eth0             *     users:(("tcpdump",pid=257,fd=4))$ ss -0 -bp_raw 0      0                  *:eth0             *	bpf filter (24):  0x28 0 0 12, 0x15 0 8 34525, 0x30 0 0 20, ...$ ss -f netlinkRecv-Q Send-Q Local Address:Port        Peer Address:Port0      0               rtnl:kernel                  *4352   0            tcpdiag:ss/372                  *960    0            tcpdiag:kernel                  *$ ss -a -A 'all,!tcp'Netid State  Recv-Q Send-Q Local Address:Port        Peer Address:Portnl    UNCONN 0      0               rtnl:kernel                  *nl    UNCONN 4352   0            tcpdiag:ss/390                  *p_raw UNCONN 0      0                  *:eth0                    *u_str LISTEN 0      5      /tmp/app.sock 1519370                * 0udp   UNCONN 0      0         127.0.0.11:39395            0.0.0.0:*$ ss -a -A unix_streamLISTEN 0      5      /tmp/app.sock 1519370            * 0

70.6 State filters

  • state / exclude filter by TCP state inside the kernel.
  • SYN-SENT: the SYN to 8080 is dropped by iptables, so the socket waits for a reply.
  • CLOSE-WAIT on cli, FIN-WAIT-2 on srv: port 7000 closes right after accept and cli's app never closes.
  • Groups: connected (not listening/closed), synchronized, bucket (syn-recv + time-wait), big, all.
  • state connected exclude established leaves only the problem states.
CLOSE-WAIT: the app never closed its socket
  1. 1.srv port 7000 accepts the connection and closes it at once

  2. 2.srv sends FIN through the switch to cli

  3. 3.cli kernel replies ACK; srv waits in FIN-WAIT-2

  4. 4.cli application never calls close()

  5. 5.cli socket stays in CLOSE-WAIT (Recv-Q 1)

Many CLOSE-WAIT = application bug

Terminal
iptables -A OUTPUT -d 192.168.90.10 -p tcp --dport 8080 -j DROPcurl -s --max-time 60 http://192.168.90.10:8080/ &ss -tn state syn-sentss -tn state close-waitss -tn state fin-wait-2     # srvss -tan state time-waitss -tan state connected exclude establishedss -tan state bucket
Output
$ ss -tn state syn-sentRecv-Q Send-Q Local Address:Port   Peer Address:Port0      1      192.168.90.20:48490 192.168.90.10:8080$ ss -tn state close-waitRecv-Q Send-Q Local Address:Port   Peer Address:Port1      0      192.168.90.20:45262 192.168.90.10:70001      0      192.168.90.20:58926 192.168.90.10:7000srv$ ss -tn state fin-wait-20      0      192.168.90.10:7000 192.168.90.20:58926$ ss -tan state time-wait0      0      192.168.90.20:60574 192.168.90.10:80$ ss -tan state connected exclude establishedCLOSE-WAIT 1      0      192.168.90.20:45262 192.168.90.10:7000CLOSE-WAIT 1      0      192.168.90.20:58926 192.168.90.10:7000SYN-SENT   0      1      192.168.90.20:48490 192.168.90.10:8080TIME-WAIT  0      0      192.168.90.20:60574 192.168.90.10:80$ ss -tan state bucketState     Recv-Q Send-Q Local Address:Port   Peer Address:PortTIME-WAIT 0      0      192.168.90.20:60574 192.168.90.10:80
KeywordMeaning
established / syn-sent / syn-recvReal TCP states
fin-wait-1/2, time-wait, close-wait, last-ack, closingClosing states
connectedAll except listening and closed
synchronizedconnected minus syn-sent
bucketsyn-recv + time-wait (minisockets)
bigEverything except bucket
exclude XRemove a state from the set

70.7 Expressions and operators

  • dst = remote address, src = local address; CIDR like 192.168.90.0/24 works.
  • dport / sport take a number or service name (:http = 80).
  • dev eth0 matches only sockets bound to that device (empty until socat used so-bindtodevice).
  • autobound: sockets whose source port the kernel picked.
  • Operators: = != > < >= <= (or eq ne gt lt ge le), not / and / or; precedence or < and < not, so use ( ) in quotes.
  • No operator between predicates means and.
Terminal
ss -tn dst 192.168.90.10ss -tn dst 192.168.90.0/24ss -tn 'dport = :443'ss -tln 'sport = :http'     # srvss -tn dev eth0sleep 800 | socat - TCP4:192.168.90.10:80,so-bindtodevice=eth0 &ss -tn dev eth0ss -tan autoboundss -tn 'dport != :443'ss -tln 'sport > :1024'     # srvss -tn '( dport = :80 or dport = :443 )'ss -tn 'dst 192.168.90.10 and dport = :443'ss -tln 'not sport = :443'  # srv
Output
$ ss -tn dst 192.168.90.10CLOSE-WAIT 1      0      192.168.90.20:45262 192.168.90.10:7000ESTAB      0      0      192.168.90.20:36998 192.168.90.10:443SYN-SENT   0      1      192.168.90.20:48490 192.168.90.10:8080srv$ ss -tln 'sport = :http'LISTEN 0      5            0.0.0.0:80        0.0.0.0:*$ ss -tn dev eth0State Recv-Q Send-Q Local Address:Port Peer Address:Port$ ss -tn dev eth0      (after socat so-bindtodevice=eth0)State Recv-Q Send-Q      Local Address:Port   Peer Address:PortESTAB 0      0      192.168.90.20%eth0:48998 192.168.90.10:80$ ss -tn 'dport != :443'CLOSE-WAIT 1      0           192.168.90.20:45262 192.168.90.10:7000ESTAB      0      0           192.168.90.20:57226 192.168.90.10:80SYN-SENT   0      1           192.168.90.20:48490 192.168.90.10:8080srv$ ss -tln 'sport > :1024'LISTEN 0      5            0.0.0.0:7000       0.0.0.0:*LISTEN 0      4096               *:5201             *:*$ ss -tn '( dport = :80 or dport = :443 )'ESTAB 0      0      192.168.90.20%eth0:48998 192.168.90.10:80ESTAB 0      0           192.168.90.20:36998 192.168.90.10:443ESTAB 0      0           192.168.90.20:57226 192.168.90.10:80$ ss -tn 'dst 192.168.90.10 and dport = :443'ESTAB 0      0      192.168.90.20:36998 192.168.90.10:443ESTAB 0      0      192.168.90.20:43512 192.168.90.10:443srv$ ss -tln 'not sport = :443'LISTEN 0      5            0.0.0.0:80         0.0.0.0:*LISTEN 0      5            0.0.0.0:7000       0.0.0.0:*LISTEN 0      4096               *:5201             *:*

70.8 Host syntax per family

  • inet:ADDR:PORT or ADDR:service; '*:443' = any local address; IPv6 goes in [ ].
  • unix: path glob with no port ('/tmp/*').
  • link:PROTO:DEV for packet sockets (link:ip:eth0 was empty because tcpdump captures all protocols; '*' matched).
  • netlink:rtnl:kernel; vsock:CID:PORT.
Terminal
ss -tn dst inet:192.168.90.10:443ss -tn dst 192.168.90.10:httpsss -tn src '*:443'          # srvss -x src '/tmp/*'ss -0 src link:ip:eth0ss -0 src 'link:*:eth0'ss -f netlink src rtnl:kernel
Output
$ ss -tn dst inet:192.168.90.10:443ESTAB 0      0      192.168.90.20:36998 192.168.90.10:443ESTAB 0      0      192.168.90.20:43512 192.168.90.10:443srv$ ss -tn src '*:443'ESTAB 0      0      192.168.90.10:443  192.168.90.20:36998ESTAB 0      0      192.168.90.10:443  192.168.90.20:43512$ ss -x src '/tmp/*'u_str ESTAB 0      0      /tmp/app.sock 1519383            * 1518222$ ss -0 src link:ip:eth0Netid Recv-Q Send-Q Local Address:Port Peer Address:Port$ ss -0 src 'link:*:eth0'p_raw 0      0                  *:eth0             *$ ss -f netlink src rtnl:kernel0      0               rtnl:kernel             *

70.9 Output fields

  • -o timer:(on,...,7): retransmit timer running, SYN retransmitted 7 times.
  • keepalive timer on the socat connection; timewait,1min after curl.
  • -m skmem: rb = receive buffer, tb = send buffer.
  • -i during iperf3: cwnd grew to 1017, plus bytes_acked and retrans counters.
  • Slow connection: check rtt, cwnd, retrans first; full buffer: check skmem and Recv-Q.
Terminal
ss -tno state syn-sentss -tno dst :443curl -s -o /dev/null http://192.168.90.10/ss -tno state time-waitiperf3 -c 192.168.90.10 -t 6 &ss -tni dst :5201
Output
$ ss -tno state syn-sent0      1      192.168.90.20:48490 192.168.90.10:8080 timer:(on,5.792ms,7)$ ss -tno dst :443ESTAB 0      0      192.168.90.20:36998 192.168.90.10:443  timer:(keepalive,25sec,0)$ ss -tno state time-wait0      0      192.168.90.20:59580 192.168.90.10:80   timer:(timewait,1min,0)$ ss -tni dst :5201ESTAB 0      196664 192.168.90.20:50546 192.168.90.10:5201	 cubic wscale:10,10 rto:204 rtt:1.318/1.698 mss:1448 pmtu:1500	 cwnd:1017 ssthresh:807 bytes_sent:2501858741 bytes_retrans:6247824	 bytes_acked:2495610166 ... busy:5552ms unacked:1 retrans:0/4316 ...

70.10 Common mistakes

  • Reading Send-Q / Recv-Q on a LISTEN line as data: Recv-Q = pending connections in the accept queue, Send-Q = backlog limit (socat 5, iperf3 4096).
  • On ESTAB sockets the same columns mean unread / unacknowledged bytes.
  • Forgetting -n and getting service names (bbs, https) in scripts.
  • Expecting ss -K to work everywhere: it needs CONFIG_INET_DIAG_DESTROY.
  • Forgetting quotes around expressions with ( ) or !.

70.11 Interview tips

  • Why ss over netstat? It reads kernel tables via netlink sock_diag with a kernel-side filter (fast); net-tools is deprecated.
  • ss -tlnp = listening TCP ports with their process.
  • ss -i shows rtt, cwnd, retrans; -o timers; -m memory; -K can kill sockets.
  • Thousands of CLOSE-WAIT = application bug, not network.