12 ss Command Examples to Monitor Network Connections

[‘

n

ss command is a tool that is used for displaying network socket related information on a Linux system. The tool displays more detailed information that the netstat command which is used for displaying active socket connections.

n

In this guide, we delve in and see how the ss command can be used to display varied socket connection information in Linux.

n

1. Listing all Connections

n

The basic ss command without any options simply lists all the connections regardless of the state they are in.

n

$ ssrn

n

List All Connections in Linux
List All Connections in Linux

n

2. Listing Listening and Non-listening Ports

n

You can retrieve a list of both listening and non-listening ports using the -a option as shown below.

n

$ ss -arn

n

List All Ports in Linux
List All Ports in Linux

n

3. Listing Listening Sockets

n

To display listening sockets only, use the -l flag as shown.

n

$ ss -lrn

n

List Listening Sockets in Linux
List Listening Sockets in Linux

n

4. List all TCP Connections

n

To display all TCP connection, use the -t option as shown.

n

$ ss -trn

n

List TCP Connections in Linux
List TCP Connections in Linux

n

5. List all Listening TCP Connections

n

To have a view of all the listening TCP socket connection use the -lt combination as shown.

n

$ ss -ltrn

n

List Listening TCP Connections in Linux
List Listening TCP Connections in Linux

n

6. List all UDP Connections

n

To view all the UDP socket connections use the -ua option as shown.

n

$ ss -uarn

n

List UDP Socket Connections in Linux
List UDP Socket Connections in Linux

n

7. List all Listening UDP Connections

n

To list listening UDP connections use the -lu option.

n

$ ss -lurn

n

List Listening UDP Connections in Linux
List Listening UDP Connections in Linux

n

8. Display PID (Process IDs) of Sockets

n

To display the Process IDs related to socket connections, use the -p flag as shown.

n

$ ss -prn

n

Find Process ID of Sockets in Linux
Find Process ID of Sockets in Linux

n

9. Display Summary Statistics

n

To list the summary statistics, use the -s option.

n

$ ss -srn

n

Find Summary Statistics
Find Summary Statistics

n

10. Display IPv4 and IPv6 Socket Connections

n

If you are curious about the IPv4 socket connections use the -4 option.

n

$ ss -4rn

n

Find IPv4 Socket Connections in Linux
Find IPv4 Socket Connections in Linux

n

To display IPv6 connections, use the -6 option.

n

$ ss -6rn

n

Find IPv6 Socket Connections in Linux
Find IPv6 Socket Connections in Linux

n

11. Filter Connections by Port Number

n

ss command also lets you filter socket port number or address number. For example, to display all socket connections with a destination or source port of ssh run the command.

n

$ ss -at '( dport = :22 or sport = :22 )'rn

n

Filter Connections by Port Number
Filter Connections by Port Number

n

Alternatively, you can run the command.

n

$ ss -at '( dport = :ssh or sport = :ssh )'rn

n

Filter Connections by Service
Filter Connections by Service

n

12. Check Man Pages for ss Command

n

To get more insights into the ss command usage, check the man pages using the command.

n

$ man ssrn

n

Find ss Command Usage and Options
Find ss Command Usage and Options

n

Those are some of the commonly used options that are used with ss command. The command is considered more superior to netstat command and provide detailed information about network connections.

n

‘]