A tutorial on restricting network bandwidth usage in Linux systems

Source: Internet
Author: User
Tags socket ssh centos

If you often run multiple network applications on a Linux desktop or have multiple computers sharing bandwidth at home, you might want to better control the use of bandwidth. Otherwise, when you download a large file using the downloader, the interactive SSH session can become slow and unusable, or when you sync a large folder through Dropbox, your roommate may complain that the video stream is choppy on her computer.

In this tutorial, I'll describe two different ways to limit the rate of network traffic in Linux.

Limit the rate of an application in Linux

One way to limit the rate of network traffic is through a command-line tool called Trickle. The trickle command allows you to change the flow of any particular program by preloading a method of a rate-limiting socket library while the program is running. A good feature of the trickle command is that it runs only in user space, which means that you can limit the bandwidth usage of a program without requiring root permissions. To be able to use the Trickle program's bandwidth, this program must use a non-static link library socket interface. Trickle can help when you want to rate limit a program that does not have built-in bandwidth control capabilities.

Install trickle in Ubuntu,debian and its derived distributions:

The code is as follows:

$ sudo apt-get install trickle

In Fdora or Centos/rhel (with Epel software warehouse):

The code is as follows:

$ sudo yum install trickle

The basic use method of trickle is as follows. Simply place the trickle command (and the rate parameter) before the command you want to run.

The code is as follows:

$ trickle-d - u

This limits the download and upload rates to specific values (unit kbytes/s).

For example, set the maximum upload bandwidth for your SCP session to KB/S:

The code is as follows:

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

If you want, you can use the following command to set the maximum download rate for your Firefox browser (for example, kb/s) by creating a custom launcher.

The code is as follows:

Trickle-d Firefox%u

Finally, trickle can also run in daemon mode, where it will limit the sum of the total bandwidth of all programs that are started by trickle and are running. Start the trickle as a daemon (for example, trickled):

The code is as follows:

$ sudo trickled-d 1000

Once the trickled daemon is running in the background, you can start other programs by using the Trickle command. If you start a program through trickle, then the maximum download rate for this program will be 1000 kb/s, and if you start another program via trickle, the rate limit for each program will be limited to kb/s, and so on.

Limit the rate of one 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 any other, Linux has a tool to do this for you. That's what wondershaper do.

Wondershaper is actually a shell script that uses TC to define the flow adjustment commands and uses QoS to handle specific network interfaces. Outbound traffic is achieved by placing a queue at different priority levels to limit the outgoing traffic rate, while incoming traffic achieves the goal of rate limiting by dropping packets.

In fact, Wondershaper's stated goal is not only to increase the bandwidth limit on an interface, but also to maintain a low latency of interactive sessions such as SSH when bulk downloads or uploads are in progress. Wondershaper Similarly, it controls bulk uploads (for example, Dropbox Sync) and does not cause downloads to "choke" or vice versa.

Install Wondershaper in Ubuntu Debian and its derivative distributions:

The code is as follows:

$ sudo apt-get install Wondershaper

Install Wondershaper in Fdora or Centos/rhel (with Epel software warehouse):

Copy Code

The code is as follows:

$ sudo yum install Wondershaper

The basic use of Wondershaper is as follows:

The code is as follows:

$ sudo wondershaper

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

The code is as follows:

$ sudo wondershaper eth0 1000 500

You can also eliminate the rate limit by running the following command:

The code is as follows:

$ sudo wondershaper clear eth0

If you are interested in the workings of wondershaper, you can read its shell script source file (/sbin/wondershaper).

Summarize

In this tutorial, I introduce two different ways to achieve the purpose of controlling the bandwidth usage of each application or each interface in a Linux desktop environment. The use of these tools is simple and provides a quick and easy way for users to adjust or limit traffic. For those readers who want to learn more about how to rate control in Linux, refer to the Linux Bible.

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.