How to limit network bandwidth in Linux
If you often run multiple network applications on the Linux desktop or share bandwidth with multiple computers at home, you may want to better control the bandwidth usage. Otherwise, when you download a large file using the download tool, the interactive SSH session may become slow or unavailable; or when you use Dropbox to move a large folder in the same step, your roommate may complain that video streams become intermittent on her computer.
In this tutorial, I will describe two different methods to limit the network traffic rate in Linux.
Limit the speed of an application in Linux
One way to limit the network traffic rate is through a command line tool named trickle. The trickle command allows you to change the traffic of any specific program by loading a method that limits the socket library speed in advance when the program is running. A good feature of the trickle command is that it only runs in the user space, which means that you do not need the root permission to limit the bandwidth usage of a program. To use the trickle program to control the program's bandwidth, this program must use the socket interface of the non-static Link Library. Trickle can help you limit the speed of a program without built-in bandwidth control.
Install trickle in Ubuntu, Debian and its derivative releases:
$ sudo apt-get install trickle
In Fdora or CentOS/RHEL (with EPEL software repository ):
$ sudo yum install trickle
The basic usage of trickle is as follows. You only need to simply put the trickle command (and the speed parameter) before the command you want to run.
$ trickle -d <download-rate>-u <upload-rate><command>
You can set<command>
The download and upload speed is limited to a specific value (unit: 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 to create a custom starter, you can use the following command to set the maximum download rate (for example, 300 KB/s) for your Firefox browser ).
trickle -d 300 firefox %u
Finally, trickle can run in daemon mode. In this mode, it limits the total bandwidth of all programs started and running through trickle. Start trickle as a daemon (for example, trickled ):
$ sudo trickled -d 1000
Once the trickled daemon runs in the background, you can use the trickle command to start other programs. If you start a program through trickle, the maximum download rate of the program will be 1000 KB/s. If you start another program through trickle) the speed limit is limited to 500 KB/s.
Limit the speed of a network interface in Linux
Another way to control your bandwidth resources is to limit the bandwidth on each interface. This is especially useful when you share the uplink bandwidth of your network connection with others. Like others, Linux has a tool to do this for you. This is what wondershaper does.
Wondershaper is actually a shell script that uses tc to define traffic adjustment commands and QoS to process specific network interfaces. Outbound traffic is placed in queues of different priorities to limit the outgoing traffic rate. inbound traffic is limited by packet loss.
In fact, the established goal of wondershaper is not only to increase the bandwidth limit for an interface; while batch download or upload is in progress, wondershaper also tries to maintain a low latency for interactive sessions such as SSH. Similarly, it also controls batch uploads (for example, Dropbox synchronization) and does not "suffocate" downloads, and vice versa.
Install wondershaper in Ubuntu Debian and its derivative releases:
$ sudo apt-get install wondershaper
Install wondershaper in Fdora or CentOS/RHEL (with EPEL software repository:
$ sudo yum install wondershaper
The basic usage of wondershaper is as follows:
$ sudo wondershaper <interface><download-rate><upload-rate>
For exampleeth0
The maximum download/upload bandwidth is set to 1000 Kbit/s and 500 Kbit/s respectively:
$ sudo wondershaper eth0 1000500
You can also run the following command to remove the speed limit:
$ sudo wondershaper clear eth0
If you are interested in the running principle of wondershaper, you can read its shell script source file (/sbin/wondershaper ).
Summary
In this tutorial, I introduced two different methods to control the bandwidth usage of each application or each interface in a Linux desktop environment. These tools are easy to use and provide users with a fast and easy way to adjust or limit traffic. For those who want to learn more about Speed Control in Linux, refer to the Linux bible.
This article permanently updates the link address: