This tutorial comes to show you how to use the netstat command with the Linux operating system.
To list out all the connections TCP, UDP & Unix on a system, we can use ‘a’ option with netstat command:
1 | $ netstat -a |
To list only the TCP connections our system, use ‘t’ options with netstat:
1 | $ netstat -at |
To list out only the UDP connections on our system, we can use ‘u’ option with netstat:
1 | $ netstat -au |
To only list out Unix socket connections, we can use ‘x’ options:
1 | $ netstat -ax |
To get a list of all connections along with PID or process name, we can use ‘p’ option & it can be used in combination with any other netstat option:
1 | $ netstat -ap |
To speed up our output, we can use ‘n’ option as it will perform any reverse lookup & produce output with only numbers (since no lookup is performed, our output will much faster):
1 | $ netstat -an |
To print only the listening ports, we will use ‘l’ option with netstat (it will not be used with ‘a’ as it prints all ports):
1 | $ netstat -l |
To print network statistics of each protocol like packet received or transmitted, we can use ‘s’ options with netstat:
1 | $ netstat -s |
To display only the statistics on network interfaces, use ‘i’ option:
1 | $ netstat -i |
To display multicast group information, we can use option ‘g’ ( that print the multicast group information for IPV4 & IPV6):
1 | $ netstat -g |
To print the network routing information, use ‘r’ option:
1 | $ netstat -r |
To get the continuous output of netstat, use ‘c’ option:
1 | $ netstat -c |
To filter a single port connection, we can combine ‘grep’ command with netstat:
1 | $ netstat -anp | grep 3306 |
To use multiple commands with netstat.
Example: count the number of connections with CONNECTED value, we can further add ‘wc’ command with netstat and grep command:
1 | $ netstat -anp | grep 'CONNECTED'| wc -l |