The ss Command versus netstat

A replacement for the deprecated netstat command, ss gives you detailed information about how your computer communicates with other computers, networks, and services.

ss displays statistics for Transmission Control Protocol (TCP), User Datagram Protocol (UDP), Unix (interprocess), and raw sockets. Raw sockets operate at the network OSI level, which means TCP and UDP headers have to be handled by the application software, not by the transport layer. Internet Control Message Protocol (ICMP) messages and the ping utility both use raw sockets.

Using ss

You don’t have to install ss, as it’s already part of an up-to-date Linux distribution. Its output, however, can be very long—we’ve had results containing over 630 lines. The results are also very wide.

Because of this, we’ve included text representations of the results we obtained, as they wouldn’t fit in a screenshot. We’ve trimmed them to make them more manageable.

Listing Network Connections

Using ss with no command-line options lists sockets that are non-listening. That is, it lists the sockets that aren’t in the listening state.

To see this, type the following:

The columns are as follows:

Netid: The type of socket. In our example, we have “u_str,” a Unix stream, a “udp,” and “icmp6,” an IP version 6 ICMP socket. You can find more descriptions of Linux socket types in the Linux man pages. State: The state the socket is in. Recv-Q: The number of received packets. Send-Q: The number of sent packets. Local Address:Port: The local address and port (or equivalent values for Unix sockets). Peer Address:Port: The remote address and port (or equivalent values for Unix sockets).

For UDP sockets the “State” column is usually blank. For TCP sockets it can be one of the following:

LISTEN: Server-side only. The socket is waiting for a connection request. SYN-SENT: Client-side only. This socket has made a connection request and is waiting to see if it’s accepted. SYN-RECEIVED: Server-side only. This socket is waiting for a connection acknowledgment after accepting a connection request. ESTABLISHED: Server and clients. A working connection has been established between the server and the client, allowing data to be transferred between the two. FIN-WAIT-1: Server and clients. This socket is awaiting a connection termination request from the remote socket, or an acknowledgment of a connection termination request that was previously sent from this socket. FIN-WAIT-2: Server and clients. This socket is awaiting a connection termination request from the remote socket. CLOSE-WAIT: Server and client. This socket is awaiting a connection termination request from the local user. CLOSING: Server and clients. This socket is awaiting a connection termination request acknowledgment from the remote socket. LAST-ACK: Server and client. This socket is awaiting an acknowledgment of the connection termination request it sent to the remote socket. TIME-WAIT: Server and clients. This socket sent an acknowledgment to the remote socket to let it know it received the remote socket’s termination request. It’s now waiting to make sure that acknowledgment was received. CLOSED: There is no connection, so the socket has been terminated.

Listing Listening Sockets

To see the listening sockets we’ll add the -l (listening) option, like so:

These sockets are all unconnected and listening. The “rtnl” means routing netlink, which is used to transfer information between kernel and userspace processes.

Listing All Sockets

To list all sockets, you can use the -a (all) option:

The output contains all sockets, regardless of state.

Listing TCP Sockets

You can also apply a filter so only matching sockets are displayed. We’ll use the -t (TCP) option, so only TCP sockets will be listed:

ss -a -t

Listing UDP Sockets

The -u (UDP) option performs the same type of filtering action. This time, we’ll see only UDP sockets:

Listing Unix Sockets

To see only Unix sockets, you can include the  -x (Unix) option, as shown below:

Listing Raw Sockets

The filter for raw sockets is the -w (raw) option:

Listing IP Version 4 Sockets

Sockets using the TCP/IP version 4 protocol can be listed using the -4 (IPV4) option:

Listing IP Version 6 Sockets

You can turn on the matching IP version 6 filter with the -6 (IPV6) option, like so:

Listing Sockets By State

You can list sockets by the state in which they’re in with the state option. This works with established, listening, or closed states. We’ll also use the resolve option (-r), which tries to resolve network addresses to names, and ports to protocols.

The following command will look for established TCP connections, and ss will try to resolve the names:

Four connections are listed that are in the established state. The hostname, ubuntu20-04, has been resolved and “ssh” is shown instead of 22 for the SSH connection on the second line.

We can repeat this to look for sockets in the listening state:

Listing Sockets By Protocol

You can list the sockets using a particular protocol with the dport and sport options, which represent the destination and source ports, respectively.

We type the following to list sockets using the HTTPS protocol on an established connection (note the space after the opening parenthesis and before the closing one):

We can use the protocol name or the port usually associated with that protocol. The default port for Secure Shell (SSH) is port 22.

We’ll use the protocol name in one command, and then repeat it using the port number:

As expected, we get the same results.

Listing Connections to a Specific IP Address

With the dst (destination) option, we can list connections to a particular destination IP address.

We type the following:

Identifying Processes

To see which processes are using the sockets, you can use the processes option (-p), as shown below (note you must use sudo):

This shows us that the two established connections on TCP sockets are being used by the SSH daemon and Firefox.

A Worthy Successor

The ss command provides the same information previously supplied by netstat, but in a simpler, more accessible way. You can check out the man page for more options and tips.