Linux Commands

What is the SS Command in Linux?

The ability to view and understand network socket connections as they happen in your Linux system can be valuable when troubleshooting and getting your system status.

This tutorial will take you on an in-depth look at the ss command-line utility that allows us to view network connections and other detailed information. Using what you’ll learn from this guide, you should understand and use the ss utility for maximum information and productivity.

Let us get started.

What is ss?

In a nutshell, ss is a command-line utility that allows users to dump network socket information. It is similar to the popular netstat tool, but it offers more features and information than netstat.

Ss allows you to view detailed information on how your machine communicates with other resources such as networks, services, and network connections.

Using ss information, you can clearly understand what is happening, when, and how. It can be very handy during the troubleshooting process.

Basic Command Usage

Using the ss command is as easy as typing two letters into a terminal and pressing enter. Although it supports numerous arguments, calling the ss command with no options displays information about all connections, irrespective of their state.

When used with no options, the ss command dumps a lot of information you can reference later. To save the output to a file, you can use the output redirection operator as shown in the command:

sudo ss > output.txt

NOTE: It is good to note that running the ss command with and without sudo privileges might give different outputs—meaning the ss command lists information based on the user context.

Basic ss Command Options

As mentioned, the ss command supports various options that allow you to control the output and the information displayed. You can view the basic options using the command:

ss --help

Several basic options supported by the ss command include:

  1. -V or –version: Allows you to view the installed version of the ss utility.
  2. -H or –no-header: This flag suppresses the header line. The default header line of the ss command contains the following values: Netid, State, Recv-Q, Send-Q, Local Address:Port, and Peer Address:Port. Suppressing the header is useful when you need to process the ss output using other tools.
  3. -t or –tcp: Tells ss command to only display the TCP connections.
  4. -a or –all: Displays both listening and non-listening connections.
  5. -e or –extended: Displays additional information about a network socket.
  6. -u or –udp: Tells ss command to show only the UDP connections.
  7. -s or –summary: Displays a summary of the connection statistics.
  8. -l or –listening: Shows listening sockets that are not included by default.
  9. -p or –process: Shows process using a socket.
  10. -4 or –ipv4: Tells ss command to show only IPv4 connections.
  11. -6 or –ipv6: Shows IPv6 connections only.
  12. -m or –memory: Displays socket memory usage.

Although the above are some of the basic arguments you will use when working with ss, it also supports many additional options. Refer to the manual for more information.

Display Listening and Non-Listening Ports

To show information about both listening and non-listening ports, you can use the -a flag as shown in the command below:

ss -a

You can pipe the output from ss commands for more specific information using tools such as grep.

For example:

ss -a | grep ssh

Show TCP Connections

Using the -t flag with the ss command, you can filter to show only TCP connections as shown in the command below:

ss -t

To get more information, you can specify to show listening TCP connections using the -l and -t flag as shown in the command below:

sudo ss -tl

Show UDP Connections

To display all UDP connections, use the -l flag as shown in the command below:

sudo ss -au

Understanding the Header Line

As you can see from various outputs provided in the commands above, ss shows a lot of information. It includes the header format unless explicitly specified using the -H flag, which removes the header line.

Understanding the header line can be helpful to show what information is in a particular section. It includes the following rows:

State, Recv-Q, Send-Q, Local Address:Port, Peer Address:Port
  1. State: The State row of the header indicates the state of the connection, such as LISTEN, ESTABLISHED, CLOSED, TIME-WAIT, etc. This header row is useful in TCP connections as UDP does not keep track of the state of the packets, making it a stateless protocol.
  2. Recv-Q: This shows the total number of bytes not copied by the program connected to the specific socket.
  3. Send-Q: The count of bytes that is not ACK by the remote host.
  4. Local Address:Port: This shows the local socket and the port number bound to the connection
  5. Peer Address:Port: Remote socket and port number bound for the connection.

Show Processes

To get the process using the specific socket, you can use the -p flag as shown in the command below:

sudo ss - tp

As shown in the output above, you can see the TCP connections of the process using the socket, including its PID.

Filtering by Connection (TCP) State

As you know, TCP supports various states that we will not discuss in this tutorial. However, you can filter the ss output to get only the connections with the supported TCP states.

sudo ss -t state listening

You will notice the output in the image shown above does not include the state in the header because we filtered the output using the specified state. Thus, only the listening connections are displayed.

Conclusion

In this tutorial, we discussed how to use and understand the ss command utility. It is a powerful tool when you need to look beyond the listing processes. To understand how it works, you can learn more from the official manual.

Consider the following resource:

https://en.wikipedia.org/wiki/Iproute2

http://www.policyrouting.org/iproute2.doc.html

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list