112

We are hosting an application on remote server. We need to test it with a limited network bandwidth (for users with bad Internet access).

Can I limit my internet bandwidth? For instance: 128 KB per second.

This question focuses on system-wide or container-wide solutions on Linux. See Limiting a specific shell's internet bandwidth usage for process- or session-specific solutions.

6 Answers6

99

You can throttle the network bandwidth on the interface using the command called tc Man page available at http://man7.org/linux/man-pages/man8/tc.8.html

For a simple script, try wondershaper.

An example from using tc: tc qdisc add dev eth0 root tbf rate 1024kbit latency 50ms burst 1540

silvio
  • 187
  • 6
    Link to atmail.com prompt for username/password authorisation for me... :( – Ash Jun 15 '14 at 01:25
  • 1
    You can get to the atmail content here: http://web.archive.org/web/20130719031908/http://atmail.com/kb/2009/throttling-bandwidth – Tom Saleeba May 07 '15 at 06:55
  • better late than never if your gonna use a busybox router a better method is the tc command, you can check out mark's post here http://mark.koli.ch/slowdown-throttle-bandwidth-linux-network-interface

    the openvz wiki has more examples https://openvz.org/Traffic_shaping_with_tc

    – nwgat Jul 31 '15 at 04:08
  • looks like it is also possible to limit bandwidth with iptables (you can edit the answer if needed): http://serverfault.com/questions/52027/how-can-i-limit-per-user-bandwidth – baptx Apr 27 '16 at 21:29
  • Use the same command with "del" instead of "add" to revert changes. – Grumoll Oct 04 '23 at 10:15
94

As noted in previous answer, wondershaper does the job easily. I include the information from above link by Jwalanta Shrestha

apt-get install wondershaper

wondershaper - An easy tool to limit bandwidth of a particular interface.

$ sudo wondershaper {interface} {down} {up}

the {down} and {up} are bandwidth in kpbs

So for example if you want to limit the bandwidth of interface eth1 to 256kbps downlink and 128kbps uplink,

$ sudo wondershaper eth1 256 128

To clear the limit,

$ sudo wondershaper clear eth1 
Eden
  • 1,181
  • 2
    Ideally, you should have just edited the original answer instead of adding a new one. This is the standard policy. Anyways, +1 for your answer, for it does add information to the answers. – shivams Jun 11 '15 at 16:22
  • 3
    This is very useful if you are tethering mobile data and have a fair-use limit (and you have to buy new bundles after, the case for Lycamobile). On video streaming websites like YouTube, the whole video is directly loaded if you have enough bandwidth. If you limit the bandwidth, the video will automatically switch to a low resolution and download what you are watching only, which saves data if you don't want to watch everything. – baptx Apr 27 '16 at 21:10
  • 2
    Usefull also for uploading big files, as it not only limit bandwidth, but also prioritize traffic. So that when uploading 10gb file you still can use SSH. – PeterM Mar 09 '17 at 14:19
  • 9
  • 3
    If someone thinks Wondershaper must die, can they post an answer using OpenWrt and then link to it in the comments here? This answer was the easiest to understand implement, it doesn't matter much to me that it's an old tool. I took a quick look at OpenWrt (suggested by the article, "Wondershaper Must Die," and I figured it'd be at least a half hour of reading documentation and mucking around to figure out how to replicate this simple answer. – David Parks May 07 '21 at 19:49
  • 1
    Wondershaper help says its arguments must be up first, then down, i.e. Usage: /usr/sbin/wondershaper [device] clean|[upload speed in Kb/s] [download speed in Kb/s] Also it's clean not clear. This is at least on Fedora, who knows, may be you had a different version of wondershaper. – d.k Oct 19 '22 at 01:41
  • Works very poorly for me. I have VM which is running by VirtualBox. When I tried to limit VM's network by command wondershaper vboxnet0 100000 100000 on host, it limited download to ~110 mbit/s and upload to 7 mbit/sec (??how??). Also, when I tried to install it on VM and to run wondershaper eth0 100000 100000, download became 30 mbit/s and upload 200-250 mbit/s – Semyon Bayandin Aug 21 '23 at 22:16
11

Limiting network resources based on some criterias is the subject of QoS. There are several different ways to control user traffic on Linux systems.

There is a good How-to about advanced routing techniques and traffic control on Linux by Bert Hubert.

  • 2
    Were you referring to a particular section in it? – rogerdpack Apr 23 '15 at 16:21
  • 1
    If you don't wanna change system settings, just install squid3 proxy then set your "limited" aplication through proxy. For cli app's you can set torsocks or proxychains, and for heavy GUI app's you can also install Fiddler mono, so yes... there a are some ways to control everything :) – m3nda Dec 22 '15 at 16:27
  • 1
    (this is a link only answer) – Konrad Gajewski Oct 01 '19 at 19:08
8

You can also use iptables hashlimit module. Here is a simple example:

iptables -A FORWARD -m hashlimit --hashlimit-above 512kb/sec --hashlimit-burst 1mb --hashlimit-mode srcip,dstip --hashlimit-name bwlimit -j DROP

That rule limits traffic that pass through FORWARD chain as 512kb/sec with 1mb burst for each source and destination pair.

Check hashlimit section of iptables manual for more information.

http://ipset.netfilter.org/iptables-extensions.man.html

ibrahim
  • 1,077
  • 8
  • 17
7

If you already have an Apache setup somewhere, you can use mod_bw, which also works on proxied connections (i.e. Apache just forwards everything to the proper server, but slows the responses down.)

6

Dummynet does what you want and more, you can even control the latency, random packet loss and lots more.