01-22-2019, 10:55 PM
An Introduction to the ss Command
<div style="margin: 5px 5% 10px 5%;"><img src="http://www.sickgaming.net/blog/wp-content/uploads/2019/01/an-introduction-to-the-ss-command.jpg" width="737" height="360" title="" alt="" /></div><div><div><img src="http://www.sickgaming.net/blog/wp-content/uploads/2019/01/an-introduction-to-the-ss-command.jpg" class="ff-og-image-inserted" /></div>
<p><strong>Learn how to get network information using the ss command in this tutorial from the archives.</strong></p>
<p>Linux includes a fairly massive array of tools available to meet almost every need. From development to security to productivity to administration…if you have to get it done, Linux is there to serve. One of the many tools that admins frequently turned to was netstat. However, the netstat command has been deprecated in favor of the faster, more human-readable ss command. </p>
<p>The<em> </em><em>ss </em>command is a tool used to dump socket statistics and displays information in similar fashion (although simpler and faster) to netstat. The ss command can also display even more TCP and state information than most other tools. Because ss is the new netstat, we’re going to take a look at how to make use of this tool so that you can more easily gain information about your Linux machine and what’s going on with network connections.</p>
<p>The ss command-line utility can display stats for the likes of PACKET, TCP, UDP, DCCP, RAW, and Unix domain sockets. The replacement for netstat is easier to use (compare the man pages to get an immediate idea of how much easier ss is). With ss, you get very detailed information about how your Linux machine is communicating with other machines, networks, and services; details about network connections, networking protocol statistics, and Linux socket connections. With this information in hand, you can much more easily troubleshoot various networking issues.</p>
<p>Let’s get up to speed with ss, so you can consider it a new tool in your administrator kit.</p>
<h3>Basic usage</h3>
<p>The ss command works like any command on the Linux platform: Issue the command executable and follow it with any combination of the available options. If you glance at the ss man page (issue the command man ss), you will notice there aren’t nearly the options found for the netstat command; however, that doesn’t equate to a lack of functionality. In fact, ss is quite powerful.</p>
<p>If you issue the ss command without any arguments or options, it will return a complete list of TCP sockets with established connections (Figure 1).</p>
<p>Because the ss command (without options) will display a significant amount of information (all tcp, udp, and unix socket connection details), you could also send that command output to a file for later viewing like so:</p>
<pre>
ss > ss_output
</pre>
<p>Of course, a very basic command isn’t all that useful for every situation. What if we only want to view current listening sockets? Simple, tack on the -l option like so:</p>
<pre>
ss -l</pre>
<p>The above command will only output a list of current listening sockets.</p>
<p>To make it a bit more specific, think of it this way: ss can be used to view TCP connections by using the <em>-t </em>option, UDP connections by using the <em>-u </em>option, or UNIX connections by using the <em>-x</em> option; so <em>ss -t, </em><em>ss -u,</em> or <em>ss -x</em>. Running any of those commands will list out plenty of information for you to comb through (Figure 2).</p>
<p>By default, using either the <em>-t</em>, the <em>-u</em>, or the<em> -x </em>options alone will only list out those connections that are established (or connected). If we want to pick up connections that are listening, we have to add the <em>-a </em>option like:</p>
<pre>
ss -t -a </pre>
<p>The output of the above command will include all TCP sockets (Figure 3).</p>
<p>In the above example, you can see that UDP connections (in varying states) are being made from the IP address of my machine, from various ports, to various IP addresses, through various ports. Unlike the netstat version of this command, ss doesn’t display PID and command name responsible for these connections. Even so, you still have plenty of information to begin troubleshooting. Should any of those ports or URLs be suspect, you now know what IP address/Port is making the connection. With this, you now have the information that can help you in the early stages of troubleshooting an issue. </p>
<h3>Filtering ss with TCP States</h3>
<p>One very handy option available to the ss command is the ability to filter using TCP states (the the “life stages” of a connection). With states, you can more easily filter your ss command results. The ss tool can be used in conjunction with all standard TCP states:</p>
<ul>
<li>
<p>established</p>
</li>
<li>
<p>syn-sent</p>
</li>
<li>
<p>syn-recv</p>
</li>
<li>
<p>fin-wait-1</p>
</li>
<li>
<p>fin-wait-2</p>
</li>
<li>
<p>time-wait</p>
</li>
<li>
<p>closed</p>
</li>
<li>
<p>close-wait</p>
</li>
<li>
<p>last-ack</p>
</li>
<li>
<p>listening</p>
</li>
<li>
<p>closing</p>
</li>
</ul>
<p>Other available state identifiers ss recognizes are:</p>
<ul>
<li>
<p>all (all of the above states)</p>
</li>
<li>
<p>connected (all the states with the exception of listen and closed)</p>
</li>
<li>
<p>synchronized (all of the connected states with the exception of syn-sent)</p>
</li>
<li>
<p>bucket (states which are maintained as minisockets, for example time-wait and</p>
</li>
<li>
<p>syn-recv)</p>
</li>
<li>
<p>big (Opposite to bucket state)</p>
</li>
</ul>
<p>The syntax for working with states is simple.</p>
<pre>
For tcp ipv4:
ss -4 state FILTER
For tcp ipv6: ss -6 state FILTER</pre>
<p>Where FILTER is the name of the state you want to use.</p>
<p>Say you want to view all listening IPv4 sockets on your machine. For this, the command would be:</p>
<pre>
ss -4 state listening</pre>
<p>The results of that command would look similar to Figure 4.</p>
<h3>Show connected sockets from specific address</h3>
<p>One handy task you can assign to ss is to have it report connections made by another IP address. Say you want to find out if/how a machine at IP address 192.168.1.139 has connected to your server. For this, you could issue the command:</p>
<pre>
ss dst 192.168.1.139</pre>
<p>The resulting information (Figure 5) will inform you the Netid, the state, the local IP:port, and the remote IP:port of the socket.</p>
<h3>Make it work for you</h3>
<p>The ss command can do quite a bit to help you troubleshoot issues with your Linux server or your network. It would behoove you to take the time to read through the ss man page (issue the command <em>man ss</em>). But, at this point, you should at least have a fundamental understanding of how to make use of this must-know command.</p>
<p><em>Learn more about Linux through the free <a href="https://training.linuxfoundation.org/linux-courses/system-administration-training/introduction-to-linux">“Introduction to Linux” </a>course from The Linux Foundation and edX.</em></p>
</div>
<div style="margin: 5px 5% 10px 5%;"><img src="http://www.sickgaming.net/blog/wp-content/uploads/2019/01/an-introduction-to-the-ss-command.jpg" width="737" height="360" title="" alt="" /></div><div><div><img src="http://www.sickgaming.net/blog/wp-content/uploads/2019/01/an-introduction-to-the-ss-command.jpg" class="ff-og-image-inserted" /></div>
<p><strong>Learn how to get network information using the ss command in this tutorial from the archives.</strong></p>
<p>Linux includes a fairly massive array of tools available to meet almost every need. From development to security to productivity to administration…if you have to get it done, Linux is there to serve. One of the many tools that admins frequently turned to was netstat. However, the netstat command has been deprecated in favor of the faster, more human-readable ss command. </p>
<p>The<em> </em><em>ss </em>command is a tool used to dump socket statistics and displays information in similar fashion (although simpler and faster) to netstat. The ss command can also display even more TCP and state information than most other tools. Because ss is the new netstat, we’re going to take a look at how to make use of this tool so that you can more easily gain information about your Linux machine and what’s going on with network connections.</p>
<p>The ss command-line utility can display stats for the likes of PACKET, TCP, UDP, DCCP, RAW, and Unix domain sockets. The replacement for netstat is easier to use (compare the man pages to get an immediate idea of how much easier ss is). With ss, you get very detailed information about how your Linux machine is communicating with other machines, networks, and services; details about network connections, networking protocol statistics, and Linux socket connections. With this information in hand, you can much more easily troubleshoot various networking issues.</p>
<p>Let’s get up to speed with ss, so you can consider it a new tool in your administrator kit.</p>
<h3>Basic usage</h3>
<p>The ss command works like any command on the Linux platform: Issue the command executable and follow it with any combination of the available options. If you glance at the ss man page (issue the command man ss), you will notice there aren’t nearly the options found for the netstat command; however, that doesn’t equate to a lack of functionality. In fact, ss is quite powerful.</p>
<p>If you issue the ss command without any arguments or options, it will return a complete list of TCP sockets with established connections (Figure 1).</p>
<p>Because the ss command (without options) will display a significant amount of information (all tcp, udp, and unix socket connection details), you could also send that command output to a file for later viewing like so:</p>
<pre>
ss > ss_output
</pre>
<p>Of course, a very basic command isn’t all that useful for every situation. What if we only want to view current listening sockets? Simple, tack on the -l option like so:</p>
<pre>
ss -l</pre>
<p>The above command will only output a list of current listening sockets.</p>
<p>To make it a bit more specific, think of it this way: ss can be used to view TCP connections by using the <em>-t </em>option, UDP connections by using the <em>-u </em>option, or UNIX connections by using the <em>-x</em> option; so <em>ss -t, </em><em>ss -u,</em> or <em>ss -x</em>. Running any of those commands will list out plenty of information for you to comb through (Figure 2).</p>
<p>By default, using either the <em>-t</em>, the <em>-u</em>, or the<em> -x </em>options alone will only list out those connections that are established (or connected). If we want to pick up connections that are listening, we have to add the <em>-a </em>option like:</p>
<pre>
ss -t -a </pre>
<p>The output of the above command will include all TCP sockets (Figure 3).</p>
<p>In the above example, you can see that UDP connections (in varying states) are being made from the IP address of my machine, from various ports, to various IP addresses, through various ports. Unlike the netstat version of this command, ss doesn’t display PID and command name responsible for these connections. Even so, you still have plenty of information to begin troubleshooting. Should any of those ports or URLs be suspect, you now know what IP address/Port is making the connection. With this, you now have the information that can help you in the early stages of troubleshooting an issue. </p>
<h3>Filtering ss with TCP States</h3>
<p>One very handy option available to the ss command is the ability to filter using TCP states (the the “life stages” of a connection). With states, you can more easily filter your ss command results. The ss tool can be used in conjunction with all standard TCP states:</p>
<ul>
<li>
<p>established</p>
</li>
<li>
<p>syn-sent</p>
</li>
<li>
<p>syn-recv</p>
</li>
<li>
<p>fin-wait-1</p>
</li>
<li>
<p>fin-wait-2</p>
</li>
<li>
<p>time-wait</p>
</li>
<li>
<p>closed</p>
</li>
<li>
<p>close-wait</p>
</li>
<li>
<p>last-ack</p>
</li>
<li>
<p>listening</p>
</li>
<li>
<p>closing</p>
</li>
</ul>
<p>Other available state identifiers ss recognizes are:</p>
<ul>
<li>
<p>all (all of the above states)</p>
</li>
<li>
<p>connected (all the states with the exception of listen and closed)</p>
</li>
<li>
<p>synchronized (all of the connected states with the exception of syn-sent)</p>
</li>
<li>
<p>bucket (states which are maintained as minisockets, for example time-wait and</p>
</li>
<li>
<p>syn-recv)</p>
</li>
<li>
<p>big (Opposite to bucket state)</p>
</li>
</ul>
<p>The syntax for working with states is simple.</p>
<pre>
For tcp ipv4:
ss -4 state FILTER
For tcp ipv6: ss -6 state FILTER</pre>
<p>Where FILTER is the name of the state you want to use.</p>
<p>Say you want to view all listening IPv4 sockets on your machine. For this, the command would be:</p>
<pre>
ss -4 state listening</pre>
<p>The results of that command would look similar to Figure 4.</p>
<h3>Show connected sockets from specific address</h3>
<p>One handy task you can assign to ss is to have it report connections made by another IP address. Say you want to find out if/how a machine at IP address 192.168.1.139 has connected to your server. For this, you could issue the command:</p>
<pre>
ss dst 192.168.1.139</pre>
<p>The resulting information (Figure 5) will inform you the Netid, the state, the local IP:port, and the remote IP:port of the socket.</p>
<h3>Make it work for you</h3>
<p>The ss command can do quite a bit to help you troubleshoot issues with your Linux server or your network. It would behoove you to take the time to read through the ss man page (issue the command <em>man ss</em>). But, at this point, you should at least have a fundamental understanding of how to make use of this must-know command.</p>
<p><em>Learn more about Linux through the free <a href="https://training.linuxfoundation.org/linux-courses/system-administration-training/introduction-to-linux">“Introduction to Linux” </a>course from The Linux Foundation and edX.</em></p>
</div>