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?
12 Answers
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.
- 331
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,908
- 4
- 40
- 48
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.
- 2,081
- 31
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.
- 356
- 3
- 13
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.
- 1,029
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).
- 518
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
Try
sudo vnstatd -D -n
then restart vnstat service
As stated here
Update:
So i managed to trace the problem, you either change ownership of vnstat files into vnstat user like what's stated in here, this can cause lots of problems OR you edit /usr/lib/systemd/system/vnstat.service file user under [Service] from vnstat to root, User=root... Source
then reload systemd:
sudo systemctl daemon-reload
Restart vnstat:
sudo systemctl restart vnstat
To start daemon in background:
sudo vnstatd -d
Finally check vnstat in processes or system monitor.
- 409
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.
- 101
- 1
- 6
It can be the "Hardening" options on systemd's unit file: /lib/systemd/system/vnstat.service
I was moved default's /var/lib/vnstat folder to another location, even if it is world rw on new location, the options of "Hardening" will block writes there. So you need to comment out some of these options:
# Hardening
#ProtectSystem=strict
#StateDirectory=vnstat
#ProtectHome=yes
I also commented out #User=vnstat , because only then it could write to some directory inside root folder (it showed permission denied, even if its world rw).
Doing systemctl daemon-reload , restarting the service and you are done.
- 149
vnstat --version
vnStat 2.6 by Teemu Toivola <tst at iki dot fi>
sudo vnstat --add -i usb0
Adding interface "usb0" for monitoring to database...
Restart the vnStat daemon if it is currently running in order to start monitoring "usb0".
sudo systemctl restart vnstat
systemctl status vnstat
● vnstat.service - vnStat network traffic monitor
Loaded: loaded (/lib/systemd/system/vnstat.service; enabled; vendor preset: enabled)
Active: active (running) since Wed 2024-08-28 23:51:15; 3s ago
Docs: man:vnstatd(8)
man:vnstat(1)
man:vnstat.conf(5)
Main PID: 253536 (vnstatd)
Tasks: 1 (limit: 8971)
Memory: 736.0K
CGroup: /system.slice/vnstat.service
└─253536 /usr/sbin/vnstatd -n
timestamp comp systemd[1]: Started vnStat network traffic monitor.
timestamp comp vnstatd[253536]: Info: vnStat daemon 2.6 started. (pid:253536 uid:131 gid:139 64-bit)
timestamp comp vnstatd[253536]: Info: Monitoring (1): usb0 (1000 Mbit)
Tested on Ubuntu 20.04.
- 1,145
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".
- 3,291