Inspecting sockets with ss
- Version
- unversioned
- Review
- Draft
Summary#
ss shows information about sockets on a Linux system. Use -ltn to list listening TCP sockets with numeric addresses and ports.
Syntax#
ss -ltn
ss -tulpn
ss -tn state established
ss -H -ltn 'sport = :8080'
-l selects listening sockets, -t TCP, -u UDP, -n numeric output and -p the processes using each socket. -H hides the header.
Example#
This example needs python3 to open a temporary listener on a free loopback port.
(
set -eu
dir=$(mktemp -d)
server=
trap 'if [ -n "$server" ]; then
kill "$server" 2>/dev/null || true
wait "$server" 2>/dev/null || true
fi
rm -rf -- "$dir"' EXIT
python3 -c '
import socket, sys, time
listener = socket.socket()
listener.bind(("127.0.0.1", 0))
listener.listen()
with open(sys.argv[1], "w") as output:
print(listener.getsockname()[1], file=output)
time.sleep(30)
' "$dir/port" &
server=$!
until [ -s "$dir/port" ]; do
kill -0 "$server"
sleep 0.1
done
read -r port < "$dir/port"
ss -H -ltn "sport = :$port" |
awk -v port="$port" '{ sub(":" port "$", ":PORT", $4); print $1, $4 }'
)
Expected output#
The script replaces the randomly assigned port number with PORT.
LISTEN 127.0.0.1:PORT
Common mistakes#
Without -l or -a, ss omits listening sockets, so a running server can seem to be missing. With -p, an unprivileged user may see no process details for sockets that belong to other users; an empty process column does not mean that nothing owns the socket. netstat comes from the older net-tools package and is often not installed; ss -tulpn shows the same kind of list.
Version applicability#
This entry describes ss from iproute2 on Linux. BSD and macOS systems do not normally provide it.
Sources#
- Attribution
- DevLookup contributors
- License
- CC-BY-4.0