- 10 examples of Linux ss command to monitor network connections
- ss — socket statistics
- 1. List all connections
- 2. Filter out tcp,udp or unix connections
- 3. Do not resolve hostname
- 4. Show only listening sockets
- 5. Print process name and pid
- 6. Print summary statistics
- 7. Display timer information
- 8. Display only IPv4 or IPv6 socket connections
- 9. Filtering connections by tcp state
- 10. Filter connections by address and port number
- Summary
- 12 thoughts on “ 10 examples of Linux ss command to monitor network connections ”
- 6 commands to check and list active SSH connections in Linux
- Check active SSH connections
- 1. Using ss command
- 2. Using last command
- 3. Using who command
- 4. Using w command
- 5. Using netstat command
- 12 ss Command Examples to Monitor Network Connections
- 1. Listing all Connections
- 2. Listing Listening and Non-listening Ports
- 3. Listing Listening Sockets
- 4. List all TCP Connections
- 5. List all Listening TCP Connections
- 6. List all UDP Connections
- 7. List all Listening UDP Connections
- 8. Display PID (Process IDs) of Sockets
- 9. Display Summary Statistics
- 10. Display IPv4 and IPv6 Socket Connections
- 11. Filter Connections by Port Number
- 12. Check Man Pages for ss Command
- If You Appreciate What We Do Here On TecMint, You Should Consider:
10 examples of Linux ss command to monitor network connections
ss — socket statistics
In a previous tutorial we saw how to use the netstat command to get details about the network/socket connections. However the netstat command has long been deprecated and replaced by the ss command from the iproute suite of tools.
The ss command is capable of showing details about network connections with more information than the netstat command and it is faster. The netstat command reads various /proc files to gather information. However this approach is slower when there are lots of connections to display.
The ss command gets its information directly from kernel space. The options used with the ss commands are very similar to netstat making it an easy replacement.
So in this tutorial we are going to see few examples of how to use the ss command to check the network connections and socket statistics.
1. List all connections
The simplest command is to list out all connections.
We are piping the output to less so that the output is scrollable. The output will contain all tcp, udp and unix socket connection details.
2. Filter out tcp,udp or unix connections
To view only tcp or udp or unix connections use the t, u or x option.
By default the «t» option alone is going to report only those connections that are «established» or CONNECTED». It does not report the tcp sockets that are «LISTENING». Use the ‘-a’ option together with t, to report them all at once.
List all udp connections
Similarly use the x option to list out all unix socket connections.
3. Do not resolve hostname
To get the output faster, use the «n» option to prevent ss from resolving ip addresses to hostnames. But this will prevent resolution of port numbers as well.
4. Show only listening sockets
This will list out all the listening sockets. For example apache web server opens a socket connection on port 80 to listen for incoming connections.
The above command lists out all «listening» «tcp» connections. The n option disables hostname resolution of the ip addresses giving the output faster.
To list out all listening udp connections replace t by u
5. Print process name and pid
To print out the process name/pid which owns the connection use the p option
In the above output the last column contains the process name and pid. In this example dnsmasq is the process name and 1299 is the pid.
6. Print summary statistics
The s option prints out the statistics.
7. Display timer information
With the ‘-o’ option, the time information of each connection would be displayed. The timer information tells how long with
8. Display only IPv4 or IPv6 socket connections
To display only IPv4 socket connections use the ‘-f inet’ or ‘-4’ option.
To display only IPv6 connections use the ‘-f inet6’ or ‘-6’ option.
9. Filtering connections by tcp state
The ss command supports filters that can be use to display only specific connections. The filter expression should be suffixed after all options. The ss command accepts filter in the following format.
Now here are some examples of how to filter socket connections by socket states.
To display all Ipv4 tcp sockets that are in «connected» state.
Display sockets with state time-wait
The state can be either of the following
Note that many states like syn-sent, syn-recv would not show any sockets most of the time, since sockets remain in such states for a very short time. It would be ideal to use the watch command to detect such socket states in real time.
Here is an example
After running the above command, try opening some website in a browser or download something from some url. Immediately you should see socket connections appearing in the output, but for a very short while.
10. Filter connections by address and port number
Apart from tcp socket states, the ss command also supports filtering based on address and port number of the socket. The following examples demonstrate that.
Display all socket connections with source or destination port of ssh.
Sockets with destination port 443 or 80
The following syntax would also work
Ports can also be filtered with dport/sport options. Port numbers must be prefixed with a «:».
The above is same as > ss -nt dst :80
Some more examples of filtering
The following operators are supported when comparing port numbers
Summary
The above examples cover most of what the ss command supports. For more information check the man pages.
Documentation of the filter syntax can be found in the package iproute2-doc that can be installed on debian/ubuntu systems
The file /usr/share/doc/iproute2-doc/ss.html contains details about the ss command filter syntax.
A Tech Enthusiast, Blogger, Linux Fan and a Software Developer. Writes about Computer hardware, Linux and Open Source software and coding in Python, Php and Javascript. He can be reached at [email protected] .
12 thoughts on “ 10 examples of Linux ss command to monitor network connections ”
I’m tried to find ss.html anywhere in debian packages, but as you can see found nothing.
# apt-file search ss.html | grep -w ‘ss.html’
mednafen: /usr/share/doc/mednafen/ss.html
python-pycryptodome-doc: /usr/share/doc/python-pycryptodome-doc/html/src/protocol/ss.html
Thank you very mush for your article, very useful.
I was looking for an alternative between netstat and ss and I’ve found https://agmen.org/netsstat
Good script btw
Thank you! Very useful indeed.
This is very helpful.
Thanks for the examples. Noticed a few things.
Example 5 seems to have incorrect text. [1] The actual output shows dnsmasq is pid 1347 not 1299. [2]
Example 7 seems to have cutoff text. [3]
Example 9 has text saying the command shows all TCP “connected” states [4] however the command example below the text is only showing connections in the established state [5].
[1]
“In the above output the last column contains the process name and pid. In this example dnsmasq is the process name and 1299 is the pid.”
[2]
LISTEN 0 5 127.0.1.1:domain *:* users:((“dnsmasq”,1347,5))
[3]
“The timer information tells how long with ”
[4]
“To display all Ipv4 tcp sockets that are in “connected” state.”
[5]
$ ss -t4 state established
very useful for getting an overview. Thanks!
My experience is that on a RHEL 6 system, netstat -anp takes 0.161 seconds to run, while ss -np takes 6.7 seconds. If by faster you really mean 416 times slower, then I agree.
Источник
6 commands to check and list active SSH connections in Linux
Table of Contents
How to check active SSH connections in Linux. Show SSH connection history. How to show active ssh sessions in Linux. List all the active SSH connections in Unix. Find out all the currently active ssh connections on any Linux node. Which all tools can be used to list all the active ssh connections in Linux. Show active SSH sessions. Check ssh connection history using log files in Linux.
Some more more articles you may be interested on similar topics:
Check active SSH connections
There are various commands and tools available in Linux which can be used to check active SSH connections or sessions on your Linux node. In this article I will share a list of tools which can be used to get the list of active SSH connections. If you are aware of any more commands to show active ssh sessions then please let me know via comment section.
1. Using ss command
ss is used to dump socket statistics. It allows showing information similar to netstat . It can display more TCP and state information than other tools. We will use grep function to only get the list of active SSH sessions on our local host
From the above example we know that there are three hosts which are currently connected to our node3. We have active SSH connections from 10.0.2.31, 10.0.2.30 and 10.0.2.2
2. Using last command
last searches back through the file /var/log/wtmp (or the file designated by the -f flag) and displays a list of all users logged in (and out) since that file was created. Names of users and tty’s can be given, in which case last will show only those entries matching the arguments.
Using this command you can also get the information about the user using which the SSH connection was created between server and client. So below we know the connection from 10.0.2.31 is done using ‘deepak‘ user, while for other two hosts, ‘root‘ user was used for connecting to node3.
Here I am grepping for a string «still» to get all the patterns with » still logged in «. So now we know we have three active SSH connections from 10.0.2.31, 10.0.2.30 and 10.0.2.2
3. Using who command
who is used to show who is logged on on your Linux host. This tool can also give this information
Using this command we also get similar information as from last command. Now you get the user details used for connecting to node3 from source host, also we have terminal information on which the session is still active.
4. Using w command
w displays information about the users currently on the machine, and their processes. This gives more information than who and last command and also serves our purpose to get the list of active SSH connections. Additionally it also gives us the information of the running process on those sessions.
Using w command you will also get the idle time details, i.e. for how long the session is idle. If the SSH session is idle for long period then it is a security breach and it is recommended that such idle SSH session must be killed, you can configure your Linux host to automatically kill such idle SSH session.
5. Using netstat command
Similar to ss we have netstat command to show active ssh sessions. Actually we can also say that ss is the new version of netstat. Here we can see all the ESTABLISHED SSH sessions from remote hosts to our localhost node3. it is also possible that one or some of these active ssh connections are in hung state so you can configure your host to automatically disconnect or kill these hung or unresponsive ssh sessions in Linux.
Источник
12 ss Command Examples to Monitor Network Connections
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.
In this guide, we delve in and see how the ss command can be used to display varied socket connection information in Linux.
1. Listing all Connections
The basic ss command without any options simply lists all the connections regardless of the state they are in.
List All Connections in Linux
2. Listing Listening and Non-listening Ports
You can retrieve a list of both listening and non-listening ports using the -a option as shown below.
List All Ports in Linux
3. Listing Listening Sockets
To display listening sockets only, use the -l flag as shown.
List Listening Sockets in Linux
4. List all TCP Connections
To display all TCP connection, use the -t option as shown.
List TCP Connections in Linux
5. List all Listening TCP Connections
To have a view of all the listening TCP socket connection use the -lt combination as shown.
List Listening TCP Connections in Linux
6. List all UDP Connections
To view all the UDP socket connections use the -ua option as shown.
List UDP Socket Connections in Linux
7. List all Listening UDP Connections
To list listening UDP connections use the -lu option.
List Listening UDP Connections in Linux
8. Display PID (Process IDs) of Sockets
To display the Process IDs related to socket connections, use the -p flag as shown.
Find Process ID of Sockets in Linux
9. Display Summary Statistics
To list the summary statistics, use the -s option.
Find Summary Statistics
10. Display IPv4 and IPv6 Socket Connections
If you are curious about the IPv4 socket connections use the -4 option.
Find IPv4 Socket Connections in Linux
To display IPv6 connections, use the -6 option.
Find IPv6 Socket Connections in Linux
11. Filter Connections by Port Number
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.
Filter Connections by Port Number
Alternatively, you can run the command.
Filter Connections by Service
12. Check Man Pages for ss Command
To get more insights into the ss command usage, check the man pages using the command.
Find ss Command Usage and Options
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.
If You Appreciate What We Do Here On TecMint, You Should Consider:
TecMint is the fastest growing and most trusted community site for any kind of Linux Articles, Guides and Books on the web. Millions of people visit TecMint! to search or browse the thousands of published articles available FREELY to all.
If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.
We are thankful for your never ending support.
Источник