Monitoring network usage on Ubuntu

 

If you want to see how much traffic is passing through your network port there’s a handy tool called vnstat which will tally the amount of data passing through. You can install it with:

 

sudo apt-get install vnstat

 

It will usually add the databases and network ports automatically like so:

 

vnstat-0

 

If it doesn’t and gives you an error you can create the database(s) with:

 

sudo vnstat -u -i eth0

 

If you have multiple network cards/ports you can add those in, too:

 

vnstat -u -i eth1

vnstat -u -i eth2

…etc

 

If it couldn’t create the databases you can start it with:

 

sudo /etc/init.d/vnstat start

 

If you need to change the maximum bandwidth from 100Mb you can edit the file:

 

/etc/vnstat.conf

 

Scroll down until you see the following:

 

# maximum bandwidth (Mbit) for all interfaces, 0 = disable feature
# (unless interface specific limit is given)
MaxBandwidth 100

 

and make MaxBandwidth the figure you require (e.g. 1000). If you make a change restart vnstat with:

 

/etc/init.d/vnstat restart

 

You can now see how much traffic has come through the NIC since vnstat started recording – at first it probably won’t be much (if any), but as it adds up you can check it with:

 

vnstat

 

The output should look like:

 

vnstat-01

 

You can watch how much traffic is flowing through in real-time by running:

 

vnstat -i eth0 -l

 

This will give you a screen showing you the current traffic:

 

vnstat-02

 

You can end this with CTRL+C, which shows you a summary screen:

 

vnstat-03

 

You can get an hourly summary with:

 

vnstat -i eth0 -h

 

vnstat-04 vnstat-05

 

Daily summary with:

 

vnstat -i eth0 -d

 

vnstat-04

Monthly summary with:

 

vnstat -i eth0 -m

 

vnstat-06

 

This is a really handy way of keeping track of your network traffic – whether it’s out of curiosity, wanting to know how much stress your network is under or looking for a bottleneck this can be quite a valuable tool.