Find number of active connections in Linux using netstat


Lets start with the basics. The “netstat” command is quite useful for checking connections to your machine. If we wanted to see ALL of the connections (which i really recommend you don’t do unless you’re trying to debug something and then you should probably pipe it to a file) we could use the “netstat -a” command.

Using “netstat -a” will give you something sort of like this (this is a segment of my server):

Quote

tcp 0 0 app.mydomain.com:http 41.190.3.161:16494 SYN_RECV
tcp 0 0 app.mydomain.com:http 41.190.3.11:18733 SYN_RECV
tcp 0 0 app.mydomain.com:http 41-135-22-100.dsl.mwe:64775 SYN_RECV
tcp 0 0 app.mydomain.com:http 92.41.182.2.threembb.:16490 SYN_RECV
tcp 0 0 app.mydomain.com:http 41.63.193.1:video-activmail SYN_RECV
tcp 0 0 app.mydomain.com:http 69.171.229.246:45025 SYN_RECV
tcp 0 0 app.mydomain.com:http 41.63.193.11:dvl-activemail SYN_RECV
tcp 0 0 app.mydomain.com:http 41-135-22-100.dsl.mwe:64774 SYN_RECV

As you can see it does name resolving for us and all that good stuff. Sometimes very hand but that’s not what this is about. We want to get some solid numbers so we can take a broader perspective. To do this we can use the following command:

netstat -an | wc -l

This will show us a count of all connections that we presently have to our machine. But we can take this one step further even. Lets say you only wanted to see traffic comming across port 80 (standard http). We can grep our netstat then count it like so:

netstat -an | grep :80 | wc -l

Finally, lets take a look at the big picture in a category form. It is often extremely useful to see what those connections are doing, especially when you think you might just have tons of open connections that are idle and are trying to tweak your settings.

netstat -ant | awk '{print $6}' | sort | uniq -c | sort -n

So there you have it. A quick way to return counts on your connections in your linux environment. (Note the netstat command is standard on most operating systems, including windows, but you may need to use some other way to count your results)

One another popular needs is the number of open connections per ip.Terminal – Number of open connections per ip.

netstat -ntu | awk '{print $5}' | cut -d: -f1 | sort | uniq -c | sort -n

About NhocConan

A super lazy guy who tries to write tech blog entries in English.He is lazy, so he can only write when he is in a good mood or when he is tired of coding.

Leave a comment

Your email address will not be published. Required fields are marked *