How to limit network bandwidth in Linux

Source: Internet
Author: User

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:

  1. $ sudo apt-get install trickle

In Fdora or CentOS/RHEL (with EPEL software repository ):

  1. $ 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.

  1. $ 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:

  1. $ 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 ).

  1. 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 ):

  1. $ 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:

  1. $ sudo apt-get install wondershaper

Install wondershaper in Fdora or CentOS/RHEL (with EPEL software repository:

  1. $ sudo yum install wondershaper

The basic usage of wondershaper is as follows:

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

For exampleeth0The maximum download/upload bandwidth is set to 1000 Kbit/s and 500 Kbit/s respectively:

  1. $ sudo wondershaper eth0 1000500

You can also run the following command to remove the speed limit:

  1. $ 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:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.