What it is
ss is a command-line utility used to display information about network sockets, providing a faster and more feature-rich alternative to netstat.
Installation
Linux
ss is typically pre-installed on most modern Linux distributions. If not, it’s usually part of the iproute2 package:
sudo apt update && sudo apt install iproute2 # Debian/Ubuntu
sudo yum install iproute2 # CentOS/RHEL
sudo dnf install iproute2 # Fedora
macOS
ss is not natively available on macOS. You can install it via Homebrew:
brew install iproute2mac
Note: iproute2mac aims to provide ss functionality but might have some differences compared to the Linux version.
Windows
ss is not natively available on Windows. You can use tools like netstat or PowerShell cmdlets (Get-NetTCPConnection) for similar functionality. For a Linux-like experience, consider using the Windows Subsystem for Linux (WSL).
Core Concepts
- Sockets: Endpoints for network communication. Each socket is defined by a protocol (TCP, UDP, etc.) and a combination of local and remote IP addresses and port numbers.
- States: Sockets can be in various states, especially for TCP connections (e.g.,
LISTEN,ESTABLISHED,CLOSE-WAIT).ssdisplays these states. - Filters:
ssuses powerful filtering capabilities to narrow down the displayed socket information based on various criteria like state, port, address, and process.
Commands / Usage
Displaying All Sockets
- Show all TCP sockets:
Displays a list of all TCP sockets.ss -t - Show all UDP sockets:
Displays a list of all UDP sockets.ss -u - Show all raw sockets:
Displays a list of all raw sockets.ss -w - Show all listening sockets:
Displays sockets that are in a listening state.ss -l - Show all sockets (TCP, UDP, raw, UNIX):
Displays all types of sockets.ss -a
Filtering Sockets
- Show TCP sockets in
ESTABLISHEDstate:
Filters TCP sockets to only show those that are actively connected.ss -t state established - Show TCP sockets in
LISTENstate:
Filters TCP sockets to only show those waiting for incoming connections.ss -t state listening - Show UDP sockets:
Displays all UDP sockets.ss -u - Show sockets on a specific port (e.g., port 80):
Shows TCP sockets using local port 80.ss -t sport = :80
Shows TCP sockets using remote port 80.ss -t dport = :80 - Show sockets connected to a specific IP address (e.g., 192.168.1.100):
Shows TCP sockets where the destination IP is 192.168.1.100.ss -t dst 192.168.1.100 - Show sockets related to a process (using PID 1234):
Shows TCP sockets associated with process ID 1234, including process information.ss -tp pid 1234 - Show sockets on a specific network interface (e.g., eth0):
Shows TCP sockets originating from thess -ti 'src eth0'eth0interface.
Displaying More Information
- Show process information for sockets:
Appends the process name and PID to each socket entry.ss -p - Show extended socket information:
Displays more detailed information, such as user and inode.ss -e - Show timer information for sockets:
Shows timer information, like retransmission timeouts.ss -o - Show TCP connection details (RTT, congestion control):
Displays TCP internal information for established connections.ss -i - Show numeric addresses and ports (don’t resolve hostnames/service names):
Faster and avoids DNS lookups.ss -n
Combining Options
- Show all established TCP connections with process info:
Useful for seeing which processes are actively communicating.ss -tp state established - Show all listening TCP sockets on port 22 with numeric output:
Quickly identifies SSH servers.ss -ltn 'sport = :22' - Show all UDP sockets with process info:
Helps identify which processes are using UDP.ss -up
UNIX Domain Sockets
- Show all UNIX domain sockets:
ss -x - Show listening UNIX domain sockets:
ss -lx
Common Patterns
- Find which process is listening on a port (e.g., 8080):
Usesudo ss -ltnp 'sport = :8080'sudoas process information might be restricted. - Check for established connections to a specific external IP:
ss -tn state established dst 1.2.3.4 - List all outgoing connections from your machine:
(This assumes your primary IP is the first one listed byss -tn state established src $(hostname -I | awk '{print $1}')hostname -I) - Find sockets using a lot of memory (requires
-eand potentiallysudo):
Sorts by the size column (usually the 7th) in reverse numeric order to show the largest sockets first.sudo ss -tae | sort -k 7 -n -r | head - Monitor network activity for a specific port (e.g., 443) in real-time:
This command refreshes every second and shows established TCP connections to or from port 443.watch -n 1 "ss -tn state established 'dport = :443' || ss -tn state established 'sport = :443'"
Gotchas
- Permissions: To see process information (
-p) for sockets owned by other users or the system, you often needsudo. - Output Format: The output columns can vary slightly depending on the
ssversion and kernel. The order might not always be consistent. - Filtering Syntax: The filter syntax can be powerful but also complex. Pay attention to spaces, quotes, and the exact keywords (
state,sport,dport,src,dst,pid). iproute2macDifferences: If usingsson macOS viaiproute2mac, be aware that some options or the exact output format might differ from the Linuxss.- Large Output: On busy systems,
ss -acan produce a very large amount of output. Use filters aggressively. ssvsnetstat: Whilessis generally faster, especially on systems with many connections, some users might be more familiar withnetstat’s output format.ssaims to provide similar information but with a different presentation.