How to Control Bandwidth in Linux?

February 27, 2015 / FAQs

Trickle and WonderShaper:

When we start working on a lot of machines specifically Linux servers. It is interesting to know how we can control or limit the bandwidth. This can be useful in different contexts, for instance, as part of a dedicated server or shared server infrastructure. Where we can control a download or app taking all traffic over other network users. It could very well put QoS or other technologies. But the tools that we are going to see in this post allow putting a limitation on the bandwidth which is normally used pretty quickly and easily in Linux.

Limiting the bandwidth per interface via WonderShaper:

WonderShaper is a small, lightweight, and very simple tool with only one function to limit the bandwidth that used by a given network interface of a Linux machine. For example, Incoming bandwidth – we can indeed limit “download” and outgoing bandwidth “upload” with different values and have different settings per interface. The installation part is also easy:

For Debian and Ubuntu machines run the following command:

apt-get install wondershaper

For CentOS / RHEL machines:

yum install wondershaper

The use of this command is the most basic, the following is the general syntax:

wondershaper

Note: here the limits included in kilobits per second and not in kilobytes as it is more common to do so.

For example, to limit the bandwidth to 1 MB/s in download and 200 KB/s upload on “eth0”

wondershaper eth0 8192 1600

Note: 1 megabyte equals to 8192 kilobits and 200 KB/s to 1600 kilobits. There are very efficient converters available on the Internet; here we used “Bit Calculator” for our conversion.

For instance, a test may then be run by downloading an archive from the Internet to see that the bandwidth is limited. Also note that if you have multiple interfaces then let other interfaces work without limitation or on the contrary. More or less restrictively by using the same syntax. It just suffices for that to change the interface of the name in the command used.

To blow any restriction on a given interface, you can simply use the following command line. For example, the “eth0” interface:

wondershaper clear eth0

The designated interface will no longer have any restrictions.

Limiting the bandwidth by using Trickle:

Although it also acts on the limitation of bandwidth for upload and download. Trickle corresponds to other contexts since applies using only when using the command line i.e. ad hoc basis. If you want to download an archive on the Internet and also want to limit the bandwidth used by the download, you can use this tool to limit the bandwidth.

Here we start by installing it with the following command lines for Debian and Ubuntu:

apt-get install trickle

For CentOS / RHEL machines:

yum install trickle

Once installed you can use it; you just have to precede your limitation order if desired. For example, if you want to download the archive of the latest WordPress by limiting the bandwidth used in download to 10 kilobytes per second:

trickle -d 10 wget https://wordpress.org/wordpress-4.1.zip

You will then see the download proceed normally, but the bandwidth will be limited to 10 KB/s. You can also perform a limit on the upload independently with the “-u” instead of “d” or the two at the same time such as the following:

trickle -d 10 -u 20 scp archive.zip paul@server-34:/home/paul

Here we are sending an archive via scp to a remote server and upload bandwidth may not exceed 20 MB/s and download bandwidth cannot exceed 10 MB/s.

This is a limitation that ends once the limitation order completed.

If you do not wish to enter our limitation whenever we entered a command. You can use trickle in daemon mode. We will start with the beginning of the daemon indicating our bandwidth limit, for example, 50 MB/s in download and no limit upload:

trickled -d 20

Then we’ll just grab “trickle” before each command on which we want to apply this restriction:

trickle wget https://wordpress.org/...

If the same command issue without the “trickle” in front. No restrictions will apply.

These two commands are very easy to use and can be quite useful. When bandwidth is low and multiple users share!