Linux administrators are always on the lookout for ways to optimize their systems and save bandwidth. In this article, we will show you how to monitor monthly bandwidth usage in Linux. To begin, open a terminal window and type the following command: sudo iptables -L -n -v This will display all of the current rules that are being applied to your system’s network traffic. The next step is to identify which applications are using the most bandwidth. To do this, you can use the following command: sudo iptables -A INPUT -p tcp –dport 22 -j ACCEPT This rule will allow traffic from port 22 (SSH) into your system. Next, you need to identify which applications are using the most bandwidth by looking at the packets that are being sent and received. To do this, you can use the following command: sudo iptables -A OUTPUT -p tcp –dport 22 -j ACCEPT This rule will allow traffic from port 22 out of your system. Finally, you can use the following command to save all of your current rules: ..
As a server owner, it’s important to keep track of your network usage over time. Many hosting providers will charge for bandwidth and transmitted data, so you’ll want to keep tabs on your month-to-month usage.
Install vnstat
There are plenty of bandwidth monitoring tools out there—most real-time monitoring tools like htop and glances will show Rx (received) and Tx (transmitted) out. However, it’s much more useful to look at daily and monthly averages, and to do that, you’ll need a tool that can keep logs over time.
Of course, if you’re hosting your servers on a big cloud provider like AWS or GCP, they will probably have built-in log collection tools like AWS CloudWatch and GCP Cloud Monitoring. For a generic Linux solution though, you’ll want to install vnstat.
vnstat monitors all network interfaces, and keeps logs on how much traffic your servers are handling, which can be used to present monthly, daily, and hourly averages of traffic. It also has the option of outputting to a PNG for a better looking graph.
vnstat is available from most main package managers. For Debian-based systems like Ubuntu, that would be:
You’ll also want to install vnstati for image output:
If it isn’t available on your package manager, you can download it from source, and use make to build it for your system.
vnstat will immediately start collecting data, but it will take a while for enough data to be collected to actually present anything. Come back to it in a few hours once it’s collected some data, and run vnstat to view its output:
This shows received GiB (RX), and transmitted GiB (TX), as well as a total and an estimate based on prior usage if the logs are incomplete. Keep in mind that this is in Gibibytes, not Gigabytes, though the difference isn’t as much as the much smaller Gigabits.
If you want more detailed output, you can output hourly:
To output an image summary, you can use the following command (-s for summary), replacing eth0 with whatever network device you want to view:
You can also view hourly output in the same fashion.
If you’d like to do more in-depth analysis, or send these logs off somewhere else, you can output all of vnstat‘s logs with the –json flag.