June 28

Linux: netstat monitoring

Netstat is an extremely useful Linux command line tool that allows you to check which service is connecting to a port on your machine. It is very useful to analyze what’s going on on your machine when you are facing or trying to to prevent an attack on it. You can find information such as how many connection are being made on a port, which IP addresses these connections originate from, and much more. Netstat ships with most distributions of Linux so it should already be installed on yours.

Launch a shell terminal on your machine and run the following command:

# netstat -ant
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 ::ffff:192.168.1.19:80 ::ffff:192.168.1.240:63049 TIME_WAIT
tcp 0 0 ::ffff:192.168.0.19:80 ::ffff:192.168.1.240:62793 TIME_WAIT
tcp 0 0 ::ffff:192.168.1.19:80 ::ffff:192.168.1.240:62795 TIME_WAIT

The output will most likely be very long. I’ve just given a snapshot of the output above. As you can see in the data above there is a connection made from 192.168.1.240 to my server’s port 80 using the TCP protocol and the connection in the a state of TIME_WAIT. The out put that you get for your server will have information about all the ports on your machine, not just port 80.

The first thing you realize is that an output this big is not of too much use. So let’s organize it a bit. Here are a few tricks I use to analyze the nature of the connections being made to of from my server. the first thing I do is figure out which services I want to analyze. Usually it is port 80 as that’s the default port for the web server to run on, and port 3306 which is the default port for MySQL to run on. So i use the following query to see what’s going on on port 80:

# netstat -ant | grep 80

Again I get big list of connections, smaller than the first, but still too big to grasp. So I use the “wc -l” command to count the number of lines in the output to see approximately how many connections I have on my port 80:

# netstat -ant | grep 80 | wc -l
625

And then I do the same for MySQL:

# netstat -ant | grep 3306 | wc -l
61

Now, if I want to get a complete picture of what’s going on on my server in terms of the nature of connections here’s what I do:

# netstat -ant | awk ‘{print $6}’ | sort | uniq -c | sort -n

1 established)
1 Foreign
4 FIN_WAIT2
8 LISTEN
16 CLOSE_WAIT
134 ESTABLISHED
409 TIME_WAIT

This tells me how many connections of different types of state I have on my machine. I can run a similar command to see a complete picture of the state of all the connections made to my web server:

netstat -ant | grep 80 | awk ‘{print $6}’ | sort | uniq -c | sort -n

1 FIN_WAIT1
4 LISTEN
6 FIN_WAIT2
17 CLOSE_WAIT
94 ESTABLISHED
534 TIME_WAIT

By: S Dhandhania


Copyright 2021. All rights reserved.

Posted June 28, 2012 by Timothy Conrad in category "Linux

About the Author

If I were to describe myself with one word it would be, creative. I am interested in almost everything which keeps me rather busy. Here you will find some of my technical musings. Securely email me using - PGP: 4CB8 91EB 0C0A A530 3BE9 6D76 B076 96F1 6135 0A1B