How to limit network bandwidth on Linux

If you often run multiple networking applications on your Linux desktop, or share bandwidth among multiple computers at home, you will want to have a better control over bandwidth usage. Otherwise, when you are downloading a big file with a downloader, your interactive SSH session may become sluggish to the point where it's unusable. Or when you sync a big folder over Dropbox, your roommate may complain that video streaming at her computer gets choppy.

In this tutorial, I am going to describe two different ways to rate limit network traffic on Linux.

Rate Limit an Application on Linux

One way to rate limit network traffic is via a command-line tool called trickle. The trickle command allows you to shape the traffic of any particular program by "pre-loading" a rate-limited socket library at run-time. A nice thing about trickle is that it runs purely in user-space, meaning you don't need root privilege to restrict the bandwidth usage of a program. To be compatible with trickle, the program must use socket interface with no statically linked library. trickle can be handy when you want to rate limit a program which does not have a built-in bandwidth control functionality.

To install trickle on Ubuntu, Debian and their derivatives:

$ sudo apt-get install trickle

To install trickle on Fedora or CentOS/RHEL (with EPEL repository):

$ sudo yum install trickle

Basic usage of trickle is as follows. Simply put, you prepend trickle (with rate) in front of the command you are trying to run.

$ trickle -d <download-rate> -u <upload-rate> <command>

This will limit the download and upload rate of <command> to specified values (in KBytes/s).

For example, set the maximum upload bandwidth of your scp session to 100 KB/s:

$ trickle -u 100 scp backup.tgz alice@remote_host.com:

If you want, you can set the maximum download speed (e.g., 300 KB/s) of your Firefox browser by creating a custom launcher with the following command.

trickle -d 300 firefox %u

Finally, trickle can run in a daemon mode, where it can restrict the "aggregate" bandwidth usage of all running programs launched via trickle. To launch trickle as a daemon (i.e., trickled):

$ sudo trickled -d 1000

Once the trickled daemon is running in the background, you can launch other programs via trickle. If you launch one program with trickle, its maximum download rate is 1000 KB/s. If you launch another program with trickle, each of them will be rate limited to 500 KB/s, etc.

Rate Limit a Network Interface on Linux

Another way to control your bandwidth resource is to enforce bandwidth limit on a per-interface basis. This is useful when you are sharing your upstream Internet connection with someone else. Like anything else, Linux has a tool for you. wondershaper exactly does that: rate-limit a network interface.

wondershaper is in fact a shell script which uses tc to define traffic shaping and QoS for a specific network interface. Outgoing traffic is shaped by being placed in queues with different priorities, while incoming traffic is rate-limited by packet dropping.

In fact, the stated goal of wondershaper is much more than just adding bandwidth cap to an interface. wondershaper tries to maintain low latency for interactive sessions such as SSH while bulk download or upload is going on. Also, it makes sure that bulk upload (e.g., Dropbox sync) does not suffocate download, and vice versa.

To install wondershaper on Ubuntu, Debian and their derivatives:

$ sudo apt-get install wondershaper

To install wondershaper on Fedora or CentOS/RHEL (with EPEL repository):

$ sudo yum install wondershaper

Basic usage of wondershaper is as follows.

$ sudo wondershaper <interface> <download-rate> <upload-rate>

For example, to set the maximum download/upload bandwidth for eth0 to 1000Kbit/s and 500Kbit/s, respectively:

$ sudo wondershaper eth0 1000 500

You can remove the rate limit by running:

$ sudo wondershaper clear eth0

If you are interested in how wondershaper works, you can read its shell script (/sbin/wondershaper).

Conclusion

In this tutorial, I introduced two different ways to control your bandwidth usages on Linux desktop, on per-application or per-interface basis. Both tools are extremely user-friendly, offering you a quick and easy way to shape otherwise unconstrained traffic. For those of you who want to know more about rate control on Linux, refer to the Linux bible.

Subscribe to Xmodulo

Do you want to receive Linux FAQs, detailed tutorials and tips published at Xmodulo? Enter your email address below, and we will deliver our Linux posts straight to your email box, for free. Delivery powered by Google Feedburner.


Support Xmodulo

Did you find this tutorial helpful? Then please be generous and support Xmodulo!

The following two tabs change content below.
Dan Nanni is the founder and also a regular contributor of Xmodulo.com. He is a Linux/FOSS enthusiast who loves to get his hands dirty with his Linux box. He likes to procrastinate when he is supposed to be busy and productive. When he is otherwise free, he likes to watch movies and shop for the coolest gadgets.

8 thoughts on “How to limit network bandwidth on Linux

  1. I am a registered RSS reader, and I don't miss your great articles.
    At this, and only this specific case, I just wonder what is the meaning to promote programs abandoned a decade ago - 2003 & 2002 correspondingly.

    • Thanks for your feedback. Well, at least these programs get the job done, and more importantly it's quick and easy. If someone still finds them useful, why not share them? Of course if there are better alternatives, I'll be happy to cover them. Feel free to chime in.

    • Was going to mention the exact same thing. I have managed and tested bandwidth tools since 2005 and trickle still does a not so bad job (although lacking a lot) but come on... Wondershaper? That's been dead long ago. The author's answer to your comment does not answer your question nor provide an updated answer in the post. Please check the tools you want to mention in a post before putting them on your site or you will mislead a lot of people.

      • Thanks for your input. I was actually trying to engage with OP to find out any better alternatives.
        Same with you, can you please elaborate what the problem is with wondershaper, and suggest other options? Are you complaining about tc or the wondershaper script itself? I'm just trying to keep the discussion more useful to readers. Simply saying that it's dead long ago isn't very helpful. :-(

  2. I agree with Dan Nanni. If a tool gets the job done, I do not care whether it is actively maintained or not.

    However, it is a different matter if there are security/critical bugs that have not been fixed - which I do not think is the case here.
    It is open source software and the source code is always available if you want to hack around.

    So unless you guys have a better solution/tools for the above task, please shut up?

  3. Just because wondershaper hasn't been updated in a long time, that doesn't mean it's obsolete. I've been using scripts based on LARTC for years, mine look much like wondershaper. If I'm not mistaken, the technology that wondershaper uses (tc) is still current and has not been deprecated. You can sure make a fancier interface if you want, but you're still gonna be calling tc. Wondershaper works great called from rc.local or interface up scripts with no fuss.

Leave a comment

Your email address will not be published. Required fields are marked *