{"id":4187,"date":"2021-02-10T06:10:09","date_gmt":"2021-02-10T06:10:09","guid":{"rendered":"https:\/\/linuxways.net\/?p=4187"},"modified":"2021-02-10T06:12:38","modified_gmt":"2021-02-10T06:12:38","slug":"monitor-network-traffic-with-vnstat-on-ubuntu-20-04","status":"publish","type":"post","link":"https:\/\/linuxways.net\/de\/ubuntu\/monitor-network-traffic-with-vnstat-on-ubuntu-20-04\/","title":{"rendered":"Monitor Network Traffic with vnStat on Ubuntu 20.04"},"content":{"rendered":"<p>vnStat is an open-source and lightweight network traffic monitor for Linux operating systems. vnStat quietly monitors the network traffic for specific interfaces and stores the monitoring data in a database. With vnStat, you can view the bandwidth usage per hour, day, month, or for a particular period.<\/p>\n<p>In this guide, we will be looking at how to install vnStat and monitor network traffic on Ubuntu OS.<\/p>\n<p><strong>Note<\/strong>: The procedure described here has been tested on <a href=\"https:\/\/linuxways.net\/de\/category\/ubuntu\/\"><strong>Ubuntu 20.04 LTS<\/strong><\/a>. The commands have been executed on Terminal which you can open through the<strong> Ctrl+Alt+T<\/strong> shortcut.<\/p>\n<h2>Installing vnStat<\/h2>\n<p>VnStat is available in the default Ubuntu repositories but it is not the latest version. To install vnStat latest version, follow the below-mentioned steps:<\/p>\n<h3>Step 1: Update repository index<\/h3>\n<p>First, execute the below command to update the system repository index:<\/p>\n<pre>$ sudo apt update<\/pre>\n<p>Enter the password for sudo.<\/p>\n<h3>Step 2: Install prerequisites<\/h3>\n<p>Next, you will have to install some prerequisites that can build software from source. We will need it for the compilation of vnStat. Execute this command to install the prerequisites:<\/p>\n<pre>$ sudo apt install build-essential gcc make libsqlite3-dev -y<\/pre>\n<h3>Step 3: Download vnStat tar.gz package<\/h3>\n<p>Now from the vnStat official website, download the latest version of vnStat <strong>tar.gz<\/strong> package. Currently, the latest version of vnStat is 2.6, so we can download it as follows:<\/p>\n<pre>$ wget https:\/\/humdi.net\/vnstat\/vnstat-2.6.tar.gz<\/pre>\n<h3>Step 4: Install vnStat<\/h3>\n<p>Once the <strong>tar.gz<\/strong> package is downloaded, we can install it as follows:<\/p>\n<p>First, extract the tar.gz using the below command in Terminal:<\/p>\n<pre>$ tar -xvzf vnstat-2.6.tar.gz<\/pre>\n<p>Then switch to the resulting extracted directory using the below command:<\/p>\n<pre>$ cd vnstat-2.6<\/pre>\n<p>Now configure it using the command below:<\/p>\n<pre>$ .\/configure --prefix=\/usr --sysconfdir=\/etc<\/pre>\n<p>Then, install vnStat as follows:<\/p>\n<pre>$ sudo make<\/pre>\n<pre>$ sudo make install<\/pre>\n<h3>Step 5: Verify Installation<\/h3>\n<p>To verify if vnStat has been successfully installed, execute the below command in Terminal:<\/p>\n<pre>$ vnstat -v<\/pre>\n<p>The following output confirms vnStat version <strong>2.6<\/strong> has been installed.<\/p>\n<h3>Step 6: Enable and start vnStat service<\/h3>\n<p>To enable and start vnStat service, first you will need to copy vnStat service file from the vnStat extracted directory to the <strong>\/etc\/systemd\/system\/<\/strong> using the below command:<\/p>\n<pre>$ sudo cp -v vnstat-2.6\/examples\/systemd\/vnstat.service \/etc\/systemd\/system\/<\/pre>\n<p>Now enable vnStat service using the below command:<\/p>\n<pre>$ sudo systemctl enable vnstat<\/pre>\n<p>Then to start vnStat service, execute the below command<\/p>\n<pre>$ sudo systemctl start vnstat<\/pre>\n<p>To verify if vnStat service is running, execute the below command:<\/p>\n<pre>$ sudo systemctl status vnstat<\/pre>\n<p>The following output confirms vnStat service is running properly without any issues.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"981\" height=\"122\" class=\"wp-image-4188\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/word-image-91.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/word-image-91.png 981w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/word-image-91-300x37.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/word-image-91-768x96.png 768w\" sizes=\"auto, (max-width: 981px) 100vw, 981px\" \/><\/p>\n<h2>Monitor Network Traffic with vnStat<\/h2>\n<p>vnStat supports various options that help you to monitor traffic in different ways. You can view some of the available options by executing the below command in Terminal:<\/p>\n<pre>$ vnstat --help<\/pre>\n<p>Or execute the following command to view the complete list of options:<\/p>\n<pre>$ vnstat --longhelp<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"795\" height=\"506\" class=\"wp-image-4189\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/vnstat-help.png\" alt=\"vnstat help\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/vnstat-help.png 795w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/vnstat-help-300x191.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/vnstat-help-768x489.png 768w\" sizes=\"auto, (max-width: 795px) 100vw, 795px\" \/><\/p>\n<p>You can also visit vnStat <a href=\"https:\/\/linux.die.net\/man\/1\/vnstat\">man<\/a> page for the complete list of options.<\/p>\n<p>Running the vnStat command without any option prints the bandwidth usage statistics for all the available interfaces.<\/p>\n<pre>$ vnstat<\/pre>\n<p>Here is the output of vnStat command on our system which is showing statistics of one interface as this is the only available interface on our system.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"884\" height=\"482\" class=\"wp-image-4190\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/vnstat-command-output.png\" alt=\"vnstat command output\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/vnstat-command-output.png 884w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/vnstat-command-output-300x164.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/vnstat-command-output-768x419.png 768w\" sizes=\"auto, (max-width: 884px) 100vw, 884px\" \/><\/p>\n<p>You can also monitor a specific interface by using the <strong>-i<\/strong> option followed by the interface name.<\/p>\n<pre>$ vnstat -i &lt;interface_name&gt;<\/pre>\n<p>For instance, to monitor an interface <strong>ens33<\/strong>, the command would be:<\/p>\n<pre>$ vnstat -i ens33<\/pre>\n<p>You can also monitor bandwidth usage as per <strong>hourly, daily, monthly, <\/strong>and<strong> yearly<\/strong> usage using the <strong>-h, -d, -m, <\/strong>and<strong> -y <\/strong>options respectively. For example, to find the daily bandwidth usage, the command would be:<\/p>\n<pre>$ vnstat -d<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"882\" height=\"217\" class=\"wp-image-4191\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/vnstat-daily-statistics.png\" alt=\"vnstat daily statistics\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/vnstat-daily-statistics.png 882w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/vnstat-daily-statistics-300x74.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/vnstat-daily-statistics-768x189.png 768w\" sizes=\"auto, (max-width: 882px) 100vw, 882px\" \/><\/p>\n<p>You can also get the bandwidth usage for the top traffic days. For instance, to find the top 10 bandwidth usage days, use <strong>-t<\/strong> option followed by the number of days:<\/p>\n<pre>$ vnstat -t 10<\/pre>\n<p>To monitor bandwidth usage in real-time, use the <strong>-l<\/strong> option as follows:<\/p>\n<pre>$ vnstat -l<\/pre>\n<p>If you want to remove all the statistics for a specific interface from the database and stop monitoring it, use the below command syntax:<\/p>\n<pre>$ sudo vnstat --remove -i &lt;interface_name&gt; --force<\/pre>\n<p>For instance, to remove the interface <strong>ens33<\/strong> from the database and stop monitoring it, the command would be:<\/p>\n<pre>$ sudo vnstat --remove -i ens33 --force<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"680\" height=\"99\" class=\"wp-image-4192\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/word-image-92.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/word-image-92.png 680w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/word-image-92-300x44.png 300w\" sizes=\"auto, (max-width: 680px) 100vw, 680px\" \/><\/p>\n<p>To add this interface again for monitoring, use the below command:<\/p>\n<pre>$ sudo vnstat --add -i ens33<\/pre>\n<p>After adding the interface, restart vnStat service:<\/p>\n<pre>$ sudo systemctl restart vnstat<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"664\" height=\"123\" class=\"wp-image-4193\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/word-image-93.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/word-image-93.png 664w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/word-image-93-300x56.png 300w\" sizes=\"auto, (max-width: 664px) 100vw, 664px\" \/><\/p>\n<p>vnStat is a handy tool to keep an eye on the bandwidth usage on your Linux OS. In this guide, you have learned how to install and use this tool on Ubuntu 20.04 LTS for monitoring the traffic on network interfaces.<\/p>","protected":false},"excerpt":{"rendered":"<p>vnStat is an open-source and lightweight network traffic monitor for Linux operating systems. vnStat quietly monitors the network traffic for specific interfaces and stores the monitoring data in&hellip;<\/p>","protected":false},"author":4,"featured_media":4194,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[100,186],"class_list":["post-4187","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-ubuntu","tag-ubuntu-20-04","tag-vnstat"],"_links":{"self":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts\/4187","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/comments?post=4187"}],"version-history":[{"count":0,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts\/4187\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/media\/4194"}],"wp:attachment":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/media?parent=4187"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/categories?post=4187"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/tags?post=4187"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}