How to limit network bandwidth in Linux

Estimated reading: 6 minutes 389 views

There may be cases when you need to limit network bandwidth in Linux. Learn how to limit the network bandwidth in Linux globally and application-specifically with two simple programs: wondershaper and trickle.

1. How To Limit the bandwidth per network interface with Wondershaper

Wondershaper is a simple command-line utility that can limit the network bandwidth in Linux. It can be configured to limit download and upload speed for each network interface of your Linux machine

Step 1: Update your system

Always update your system repositories before installing

# apt update

Step 2: Install Wondershaper

To install Wondershaper, search for it in your Linux package manager. It is an open-source application that should be available in all Linux distros. This is the installation I recommend. In Ubuntu and other Debian-based distros, you can run this command:

# apt install wondershaper

If you want the latest version of Wondershaper, you can install it from GitHub:

# apt install git
# git clone https://github.com/magnific0/wondershaper.git
# cd wondershaper
# make install

And, run the following command to start wondershaper service automatically on every reboot.

# systemctl enable wondershaper.service
# systemctl start wondershaper.service

Hopefully, this installation goes without errors for you. It is a less reliable way to install Wondershaper than using the version provided with your distro. I have tested this installation on Ubuntu 22, it works fine. But if you have problems with the Github version, remove it by running this command from the wondershaper git-cloned directory:

# make uninstall

If you decide to keep the GitHub version of Wondershaper, keep in mind that unlike the version included with the distro, it will not update itself when the new version is released. You need to update it manually. You can do it by entering the wondershaper folder, pulling the updates from GitHub, and re-installing:

# cd wondershaper
# git pull
# make install

Step 3: Learn the syntax of Wondershaper

Depending on the version of wondershaper, it may have a different syntax for configuration. You can check it with the man command:

# man wondershaper

You need to install the man package first if it’s not already available

# apt install man

It will give you an output similar to this:

It is just a sequence of the interface, download and upload speed limit in my case:

wondershaper [ interface ] [ downlink ] [ uplink ]

More recent versions require specifying the option names:

wondershaper -a [interface] -d [downlink] -u [uplink]

Step 4: Find out the network interface name

First, find the name of your network interface. Here are some common ways to find the details of a network card.

# ip addr show

or

# ifconfig

In my system, I only have the Ethernet interface eth0 :

Step 5: Limit Network Bandwidth

Knowing the network interface name, we can limit it with wondershaper. For example, set the bandwidth limit to 6 MB/s for download and 3 MB/s for upload on the Ethernet interface eth0:

# wondershaper eth0 6144 3072

or

# wondershaper -a eth0 -d 6144 -u 3072
  • -a : network card name
  • -d : download rate
  • -u : upload rate

Now, you can check if these settings have been applied by testing your internet speedtest .This is the results I get with these settings:

 

If I clear all the limits:

# wondershaper clear eth0

And test my internet speed again, I get these values:

Step 6: Run Wondershaper persistently

The settings we tested above work only until you reboot your Linux system. To apply the limits persistently, you need to create a systemd configuration and service files for Wondershaper.

Open the configuration file:

# nano /etc/systemd/wondershaper.conf

And paste the following content with your interface and limit settings:

[wondershaper]

# Adapter
IFACE="enth0"

# Download rate in Kbps
DSPEED="6144"

# Upload rate in Kbps
USPEED="3072"

Then create a service file:

# nano /etc/systemd/system/wondershaper.service

With the following content:

[Unit]
Description=Bandwidth shaper/Network rate limiter
After=network-online.target
Wants=network.target

[Service]
Type=oneshot
RemainAfterExit=yes
EnvironmentFile=/etc/systemd/wondershaper.conf
ExecStart=/usr/sbin/wondershaper $IFACE $DSPEED $USPEED
ExecStop=/usr/sbin/wondershaper clear $IFACE

[Install]
WantedBy=multi-user.target

Note, if your Wondershaper uses option names, you need to edit the ExecStart and ExecStop lines to:

ExecStart=/usr/sbin/wondershaper -a $IFACE -d $DSPEED -u $USPEED
ExecStop=/usr/sbin/wondershaper -c -a $IFACE

To save the changes press Ctrl+O and to exit press Ctrl+X.

Now, activate Wondershaper to run persistently:

# systemctl enable --now wondershaper.service

The Wondershaper will now limit network bandwidth in your Linux even after you reboot your system.

If you want to disable the persistent run of Wondershaper, execute:

# systemctl disable --now wondershaper.service

 2. Limiting the bandwidth per application with trickle

Trickle is a lightweight bandwidth shaper that can be used to set the data limits per application. By using the Unix loader preloading, it adds a new version of the data transferring functionality to an application through sockets and then limits traffic by delaying the sending and receiving of data over a socket

Step 1: Install trickle

Trickle is available in most Linux distributions. In Ubuntu, you install it with this command:

# apt install trickle

Step 2: how to use trickle

You can see all available options and how to use trickle with its help message:

# trickle -h

In most cases, you only need to set the download/upload limits and the application name:

# trickle -d [ download speed ] -u [ upload speed ] [ application/command ]

Here is an example of 6 MB/s limit for download, and 1 MB/s to upload in Firefox:

# trickle -d 6144 -u 1024 firefox

You can also set download and upload rates separately if you want to limit only one of them:

#trickle -d 6144 firefox

You can also limit network bandwidth for Linux commands. For example, you may want to limit the wget download speed:

#trickle -d 1024 wget https://releases.ubuntu.com/20.04.3/ubuntu-20.04.3-desktop-amd64.iso

You can also launch bash shell with bandwidth limits for all commands:

# trickle -d 6144 -u 1024 bash

To reset the bandwidth limits, simply close the bash with the exit command or Ctrl+Q.

You can also add some advanced settings to your trickle command. You can read the detailed description of each option with the man command:

# man trickle

Step 3: trickle with rsync

There is a small difference in using trickle with rsync to copy files over ssh. If you simply put trickle in front of rsync, it will not work because rsync forks the ssh process. Thus, ssh will run without trickle limits. To call rsync with trickle limits, run it like this:

# rsync --rsh="trickle -d 6144 -u 1024 ssh" SORCE DESTINATION

Step 4: Test trickle

Let’s set the download limits to Firefox and add a small smoothing value as it is an interactive application:

# trickle -d 6144 -u 1024 -t 0.1 firefox

You can also test trickle with wget:

# trickle -d 1024 wget https://releases.ubuntu.com/20.04.3/ubuntu-20.04.3-desktop-amd64.iso

You will see that the speed is limited to 1024 KB/s:

Conclusion

I tried to provide simple and clear instructions on how to limit network bandwidth in Linux with Wondershaper and trickle without technical details. I hope these instructions helped you to configure your Linux bandwidth, and it saved you a couple of back

Good Luck!