23

I installed vnstat on my Ubuntu 14.04 server to track my internet usage (I have a limited monthly transfer). The database never updates, however. I've tried uninstalling/reinstalling, but that doesn't work. What do I need to do to get vnstat to update and be accurate?

10 Answers 10

20

Run

sudo chown -R vnstat:vnstat /var/lib/vnstat

This fixed my problem on Ubuntu 14.04.

Make sure you have following files in that folder and have proper ownership.

boby@fwhlin:/var/lib/vnstat$ ls -la
total 16
drwxr-xrwx  2 vnstat vnstat 4096 May 16 01:50 .
drwxr-xr-x 78 root   root   4096 Jul 22 15:14 ..
-rw-r--rwx  1 vnstat vnstat 2792 Jul 26 00:26 eth0
-rw-rw-r--  1 vnstat vnstat 2792 Jul 26 00:26 .eth0
boby@fwhlin:/var/lib/vnstat$ 

.eth0 is temp file, that may disappear sometimes.

1
  • I have the same issue, but on Manjaro Archlinux. sudo chown -R vnstat:vnstat /var/lib/vnstat outputs chown: invalid user: ‘vnstat:vnstat’. I have only vnstat.db` in /var/lib/vnstat: -rw-r--r-- 1 root root 68K Oct 23 13:07 vnstat.db. I suppose I need to add wlp2s0b1 this folder, although I don't know what should be in the file. I have filed a separate issue for this at github.com/vergoh/vnstat/issues/143. The above instructions may no longer be applicable, as this was answered 5 years ago.
    – James Ray
    Oct 23, 2019 at 2:12
11

After installing vnstat the database need to be created (one time). To create the database you can use the update switch, which create new database in the base of no database. Create DB: sudo vnstat -i eth0 -u where eth0 is the name of the network interface. If you are unsure update for all interfaces by sudo vnstat -u. After sometime (1 minute) you can view the details vnstat -i eth0 or simply vnstat .

Update

It seems vnstat not running ! . Start the daemon by running sudo /etc/init.d/vnstat start and check for the result after 5 minutes.

Example

sudo vnstat -u -i eth0

Error: Unable to read database “/var/lib/vnstat/eth0. Info: -> A new database has been created.

sudo /etc/init.d/vnstat start
  • Starting vnStat daemon vnstatd [ OK ]

Once database have been initialiase, its ready to monitor bandwidth usage. You can view the report by invoke command vnstat.

6
  • @Vaindil, did you tried this?
    – totti
    Jul 27, 2014 at 12:04
  • 1
    Yes, I did do this after I installed vnstat. Doing vnstat does show the output, but it shows that the database hasn't been updated in weeks.
    – vaindil
    Jul 28, 2014 at 15:42
  • Does vnstat running ? What's the output of ps aux | grep vnstat
    – totti
    Jul 28, 2014 at 15:56
  • steven 6475 0.0 0.1 11744 916 pts/0 S+ 13:35 0:00 grep --color=auto vnstat (vnstat shows up red in the terminal, if it matters or helps)
    – vaindil
    Jul 30, 2014 at 17:36
  • When attempting to run sudo /etc/init.d/vnstat start, I get the following: Starting vnStat daemon: vnstatd. / pidfile: No such file or directory and then it sits as if it's doing something, but it sits indefinitely.
    – vaindil
    Jul 31, 2014 at 18:14
3

Same issues here.

After checking the details it turned out that the databases for the different devices (eth0, ppp0, wlan0, etc.) were created in /var/lib/vnstat but these files were owned by the USER. After sudo chown -R vnstat:vnstat /var/lib/vnstat it seems to be ok. I also had to check the default interface in /etc/vnstat.conf - it was the wrong one for me, so I just changed it.

1
  • I forgat to add that I let run "vnstat -u -i wlan0" (and same for the other devices) as ROOT. This could also cause issues with permission. And one more thing: You have to check whether you are using systemd, init or upstart and start the daemon according to this.
    – bradley
    Jan 19, 2016 at 14:36
2

Are you sure that vnstatd is running, i.e. the daemon (background process) that does the data collection? If not, check with

ps aux | grep vnstatd

You should see it there.

For me, after upgrading to 14.04 vnstatd kept crashing on start-up.

My solution was to remove the folder it uses to store its data (/var/lib/vnstat) and create a new empty directory instead. You may need to run vnstatd manually once to create the database after that:

vnstatd -n -s

Apart from that, I also needed to apply

sudo chown -R vnstat:vnstat /var/lib/vnstat

as suggested by HostOnNet.

2

Sorry I had to make this its own answer instead of editing Boby's since my edit was rejected :|

By default, when you install vnstat, it auto starts a vnstatd daemon, which collects metrics every 30 seconds and "updates them" (for all local devices) every 5 minutes. So in essence, after installing vnstat packet, you should start to see metrics 5 minutes later.

Appears to sometimes be a problem with the install package however. It sometimes seems to create /var/lib/vnstat directory in such a way that it isn't writable by user vnstat so vnstat program only works when run as root (the daemon runs as user vnstat).

You can check if this is your problem by running this:

ll /var/lib | grep vnstat
drwxr-xr-x 2 root        root        4096 Oct 25  2014 vnstat

If it is owned by root (as it is in this example), then that may be your problem. You can also look for the cause in your /var/log/syslog file, it'll probably have lines like this:

Jul 27 22:06:19 xxx vnstatd[13276]: Error: Unable to open database "/var/lib/vnstat/eth0" for writing: Permission denied

Run

 sudo chown -R vnstat:vnstat /var/lib/vnstat

This fixed my problem on Ubuntu 15.10.

Make sure you have following files in that folder and have proper ownership, should look like this when you're done:

/var/lib/vnstat$ ls -la
total 16
drwxr-xrwx  2 vnstat vnstat 4096 May 16 01:50 .
drwxr-xr-x 78 root   root   4096 Jul 22 15:14 ..
-rw-r--rwx  1 vnstat vnstat 2792 Jul 26 00:26 eth0
-rw-rw-r--  1 vnstat vnstat 2792 Jul 26 00:26 .eth0

You may need to restart your vnstat daemon via sudo /etc/init.d/vnstat restart if it died from its initial failed startup attempt. You should start getting data for all devices in 5 minutes after the fix.

0

Try to output some from this help:

$ vnstat --help
 vnStat 1.11 by Teemu Toivola 

         -q,  --query          query database
         -h,  --hours          show hours
         -d,  --days           show days
         -m,  --months         show months
         -w,  --weeks          show weeks
         -t,  --top10          show top10
         -s,  --short          use short output
         -u,  --update         update database
         -i,  --iface          select interface (default: eth0)
         -?,  --help           short help
         -v,  --version        show version
         -tr, --traffic        calculate traffic
         -ru, --rateunit       swap configured rate unit
         -l,  --live           show transfer rate in real time

See also "--longhelp" for complete options list and "man vnstat".

READ MORE

0

Old thread, but still useful. All answers are there, just needed to be put in the right order, which worked for me:

1. Check your network interface

After vnstat installation, run the following command:

ifconfig

This will list all available interfaces. This example will use 'eth0' as the interface in question. In your case, use whatever value you have.

2. Update vnstat configuration with your network interface

Update '/etc/vnstat.conf' with the value of your network interface. By default, it uses 'eth0'. Change to whatever value you have in #1.

Interface "eth0"

3. Create vnstat DB for your network interface

Run the following command:

sudo vnstat -i eth0 -u
Error: Unable to read database "/var/lib/vnstat/eth0": No such file or directory
Info: -> A new database has been created.

This will create a required DB for your network interface. Use the interface value that you have in #1.

4. Fix vnstat network interface DB permissions

Running command in #3, will create a required DB with 'root' permissions that we need to fix first by running the following command:

sudo chown -R vnstat:vnstat /var/lib/vnstat

5. Start vnstat

Run the following commands to start vnstat and verify:

sudo systemctl start vnstat
sudo systemctl status vnstat -l
vnstat
1
  • How to use two interface, eth1 and eth2
    – Anas
    Oct 30, 2023 at 17:57
0

Try

sudo vnstatd -D -n

then restart vnstat service

As stated here

0

For vnstat version 2.9

you need to add the interface you want to monitor, eg:

sudo vnstat --add enp10s0f0
# Adding interface "enp10s0f0" to database for monitoring.
# vnStat daemon will automatically start monitoring "enp10s0f0" within 5 minutes if the daemon process is currently running.

most answers here already outdated (no longer relevant to later version of vnstat).

0

Use sudo vnstat --add -i <your_network_interface>. This works for me on Ubuntu 22.04. For the list of detectable interfaces, use vnstat --iflist or even ifconfig should work. If you don't find your network interface in this list, you need to find the way to get it show up here first.

You must log in to answer this question.

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