92

Some broadband providers impose a monthly download limit, charging extra if you go over. It is also quite easy to exceed some of the lower limits just by installing/updating packages and 'normal' browsing (which to me includes streaming TV programs and movies).

This means that you need to limit the amount you use the internet, yet it is hard to know when.

The System Monitor helps a bit with this by giving a total received/total sent in the networking section of the Resources tab. However, this is reset every reboot. It would be good if there was a way to have a monthly total received so you can know how close you are to exceeding your limit and maybe even be given warnings if it looks like you are going to exceed the limits.

Does anyone know of a way to achieve this?

1

6 Answers 6

75

vnStat - Light Weight Console-based Network Monitor

vnStat is a console-based network traffic monitor for Linux and BSD that keeps a log of network traffic for the selected interface(s). It uses the network interface statistics provided by the kernel as information source. This means that vnStat won't actually be sniffing any traffic and also ensures light use of system resources.

In this tutorial we'll review:

  • Features
  • Installation
  • Configuration
  • Start Systemd Service
  • Usage (from command line)
  • Conky example

Features

  • quick and simple to install and get running
  • gathered statistics persists through system reboots
  • can monitor multiple interfaces at the same time
  • several output options
  • summary, hourly, daily, monthly, weekly, top 10 days
  • optional png image output (using libgd)
  • months can be configured to follow billing period
  • light, minimal resource usage
  • same low cpu usage regardless of traffic
  • can be used without root permissions
  • online color configuration editor

Installation

nvStat is in the official repositories so no need to link to a new ppa. To install create a Terminal instance using Ctrl+Alt+T and type at the prompt:

sudo apt-get install vnstat

After installation, keep your Terminal open for the following sections. There is no need to reboot.

Configuration

Pick a preferred network interface and edit the Interface variable in the /etc/vnstat.conf accordingly. To the list all interfaces available to vnstat, use:

vnstat --iflist

To start monitoring a particular interface you must initialize a database first. Each interface needs its own database. The command to initialize one for the eth0 interface is:

sudo vnstat -u -i eth0 

Start Systemd Service

After introducing the interface(s) and checking the config file. You can start the monitoring process via systemd:

sudo systemctl start vnstat.service

To make this service permanent use:

sudo systemctl enable vnstat.service

From now on, vnstat will be gathering network usage in the background using such a small percentage of CPU it doesn't show up on conky's (system monitor's) top 9 list of processes (on my machine).

Usage (from Command Line)

Query the network traffic:

vnstat -q

Viewing live network traffic usage:

vnstat -l

To find more options, use:

vnstat --help

Monthly Totals

To see monthly totals, use:

rick@dell:~$ vnstat -m

 eth0  /  monthly

       month        rx      |     tx      |    total    |   avg. rate
    ------------------------+-------------+-------------+---------------
      Nov '16     76.31 MiB |    2.03 MiB |   78.35 MiB |   10.45 kbit/s
    ------------------------+-------------+-------------+---------------
    estimated      3.13 GiB |      84 MiB |    3.21 GiB |

Conky example

Conky is a popular light-weight System Monitor used across many Linux distributions. You can vnStat bandwidth totals to your conky display like this:

enter image description here

Note when picture was taken Yesterday was Sunday which explains why the Weekly total is less.

The conky code to achieve this is:

${color orange}${voffset 2}${hr 1}
${color1}Network using vnStat "-i", "-w" and "-m"
${color}${goto 5}Today ${goto 100}Yesterday ${goto 225}Week ${goto 325}Month ${color green}
${execi 300 vnstat -i eth0 | grep "today" | awk '{print $8" "substr ($9, 1, 1)}'} ${goto 110}${execi 300 vnstat -i eth0 | grep "yesterday" | awk '{print $8" "substr ($9, 1, 1)}'} ${goto 220}${execi 300 vnstat -i eth0 -w | grep "current week" | awk '{print $9" "substr ($10, 1, 1)}'} ${goto 315}${execi 300 vnstat -i eth0 -m | grep "`date +"%b '%y"`" | awk '{print $9" "substr ($10, 1, 1)}'}
${color orange}${voffset 2}${hr 1}

To save space on my narrow window, I used "G" instead of "GiB", "M" instead of "MiB", etc. If you have more screen real estate change substr ($10, 1, 1) to $10 and the same for $9.

You may have to change eth0 to wlan0 or eth1, etc. depending on your network name reported by ifconfig.

14
  • 1
    Is it possible to "filter" local network traffic? I have a lot of local traffic and don't want to count it for my internet usage.
    – JPelletier
    Apr 8, 2019 at 17:10
  • 1
    @JPelletier The vnStat author is quoted saying it's not possible. He's quoted on this thread where about 20 people were raising $280 to pay someone to do exactly what you are asking for: forum.netgate.com/topic/29677/… Doug Smythies is an expert at monitoring traffic per IP address and I'll ask him to look at your comment. Apr 8, 2019 at 23:13
  • 1
    Thanks for the reply! Currently I'm using iftop to diagnose live internet usage but having the historical information would be nice
    – JPelletier
    Apr 9, 2019 at 15:45
  • 5
    vnstat 2.6 found in Ubuntu 20.04 does not have the u option anymore. So people using the newer version can skip the configuration part, and go directly to the systemd service section..
    – cnxsoft
    Dec 5, 2020 at 2:35
  • 2
    @KasunSiyambalapitiya No because vnstat details are gathered in real time while network cards are operating. Your ISP (Internet Service Provider) could tell you the month to date bandwidth though. Jan 10, 2022 at 11:19
48

You can check out vnstat. It is command-line based and is available in the repository.

You can start it with sudo vnstat -u -i [interface]

To see the stats sudo vnstat -i [interface]

3
  • 1
    Do I need to start it everytime I boot my computer?
    – sgrmshrsm7
    Jun 16, 2020 at 16:45
  • 5
    Error: The "-u" parameter is not supported in this version. ??!
    – Michael
    Mar 5, 2023 at 23:17
  • @Michael guess that switch was dropped. You can always get the latest available options from vnstat --help Jan 4 at 14:42
18

Just to expand rickys anwer:

With ifconfig | cut -c 1-8 | sort | uniq -u you can list the interfaces:

enp0s31f
lo      
wlp3s0

For one interface, you can then visualize the traffic like this:

vnstati -vs -i wlp3s0 -o ~/summary.png

gives a nice summary:

enter image description here

  • rx is the received traffic
  • tx is the transferred traffic
1
  • 3
    Just make sure you have vnstati installed in addition to vnstat. sudo apt install vnstat vnstati Mar 14, 2019 at 14:36
7

Though not a "ubuntu" answer, I use the Tomato firmware on my WRT54G router for this. It gives me monthly up/down usage for the past couple of years, and the nice thing (in the context of your question) is that it is for the whole network, not just the one system it's running on (though this point is moot if the system in question is your router or directly connected).

2
4

Apart from any software solution I would suggest looking at your provider. Many of them have monitoring tools which send you a warning when you reach a certain limit or block your access temporarily. This has the advantage that you get some "official" number.

1
  • A few years late to the party, what you fail to recognise is the ISP is not cvoncerned with if you go over, in fact thats how they make their money, so their tools will not likely take in to account many factors like. My ISP give 40GB of allowance for a £20 fee but charges £20 per Gigabyte you go over which I have been over my allowance once before and I remember it ruined christmas but made the ISP happy in charging me a huge chunk of cash. I said my internet was off, they calimed it was on. Later I found someone linking directly in to the MUX off random accounts, it explained a lot Sep 3, 2018 at 19:43
3

I recommend the ntop utility which is available from the repositories, it runs as a service ands keeps traffic usage records. The reports are available from am internal http server (port 3000). You can easily check them using a browser, http://localhost:300/

Check what you can get from ntop at the ntop website .

5
  • ntop doesn't keep track of usage between reboots, does it?
    – Erigami
    Aug 6, 2010 at 20:54
  • According to the documentation it can using the rrd plugin, I didn't test myself. Aug 6, 2010 at 21:23
  • How do You use rrd plugin then?
    – Anwar
    Mar 17, 2015 at 8:00
  • An internal HTTP server, it means listening a port every time, it means start at boot, it means more startup time or more resource usage. Jun 16, 2020 at 11:32
  • It possibly keeps track of usage between reboots - Record and Visualize hosts’ historical application protocols usage (timeseries)
    – George
    Oct 23, 2023 at 12:14

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .