Merge pull request #2367 from FSSlc/master

【Translated】 20150127 How to limit network bandwidth on Linux.md
This commit is contained in:
Xingyu.Wang 2015-02-05 20:46:52 +08:00
commit 66f09e1da1
2 changed files with 112 additions and 92 deletions

View File

@ -1,92 +0,0 @@
[Translating] by FSSlc
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][1]. 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][2]):
$ 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][3] 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][4] exactly does that: rate-limit a network interface.
wondershaper is in fact a shell script which uses [tc][5] 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][6]):
$ 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][7].
--------------------------------------------------------------------------------
via: http://xmodulo.com/limit-network-bandwidth-linux.html
作者:[Dan Nanni][a]
译者:[译者ID](https://github.com/译者ID)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创翻译,[Linux中国](http://linux.cn/) 荣誉推出
[a]:http://xmodulo.com/author/nanni
[1]:http://monkey.org/~marius/trickle
[2]:http://xmodulo.com/how-to-set-up-epel-repository-on-centos.html
[3]:http://xmodulo.com/create-desktop-shortcut-launcher-linux.html
[4]:http://lartc.org/wondershaper/
[5]:http://lartc.org/manpages/tc.txt
[6]:http://xmodulo.com/how-to-set-up-epel-repository-on-centos.html
[7]:http://www.lartc.org/lartc.html

View File

@ -0,0 +1,112 @@
怎样在 Linux 中限制网络带宽
================================================================================
假如你经常在 Linux 桌面上运行多个网络应用,或在家中让多台电脑共享带宽;(这时,)你可能想更好地控制带宽的使用。否则,当你使用下载器下载一个大文件时,交互式 SSH 会话可能会变得缓慢以至不可用;或者当你通过 Dropbox 来同步一个大文件夹时,你的室友可能会抱怨在她的电脑上,视频流变得断断续续。
在本教程中,我将为你描述两种 在 Linux 中限制网络流量速率的不同方法。
### 在 Linux 中限制一个应用的速率 ###
限制网络流量速率的一种方法是通过一个名为[trickle][1]的命令行工具。通过在程序运行时,预先加载一个 速率限制 socket 库 的方法trickle 命令允许你改变任意一个特定程序的流量。 关于 trickle 命令的一个很好的特征是 它仅在用户空间中运行,这意味着,你不必需要 root 权限来达到限制一个程序的带宽使用的目的。为了与 trickle 程序兼容这个特定程序必须使用没有静态链接库的套接字接口。当你想对一个不具有内置带宽控制功能的程序进行速率限制时trickle 可以帮得上忙。
在 UbuntuDebian 及其衍生发行版中安装 trickle
```
$ sudo apt-get install trickle
```
在 Fdora 或 CentOS/RHEL (带有 [EPEL 软件仓库][2])
```
$ sudo yum install trickle
```
trickle 的基本使用方法如下。仅需简单地把 trickle 命令(带有速率参数)放在你想运行的命令之前。
```
$ trickle -d <download-rate> -u <upload-rate> <command>
```
这就可以将 `<command>` 的下载和上传速率限定为特定值(单位 KBytes/s
例如,将你的 scp 会话的最大上传带宽设定为 100 KB/s
```
$ trickle -u 100 scp backup.tgz alice@remote_host.com:
```
如若你想,你可以使用下面的命令为你的 Firefox 浏览器设定最大下载速率e.g. , 300 KB/s通过产生一个[自定义启动器][3]的方式。
```
trickle -d 300 firefox %u
```
最后, trickle 也可以 以守护进程模式运行,在该模式下,它将会限制所有通过 trickle 启动且正在运行的程序的总的带宽和。 启动 trickle 使其作为一个守护进程(i.e., trickled):
```
$ sudo trickled -d 1000
```
一旦 trickled 守护进程在后台运行,你便可以通过 trickle 命令来启动其他程序。假如你通过 trickle 启动一个程序,那么这个程序的最大下载速率将是 1000 KB/s 假如你再通过 trickle 启动了另一个程序,则每个程序的(下载)速率极限将会被限制为 500 KB/s, 等等。
### 在 Linux 中限制一个网络接口的速率 ###
另一种控制你的带宽资源的方式是在每一个接口上限制带宽。这在你与其他人分享你的网络连接的上行带宽时尤为实用。同其他一样Linux 有一个工具来为你做这件事。[wondershaper][4]恰好执行限制网络接口速率的任务。
wondershaper 实际上是一个 shell 脚本,它使用 [tc][5] 来定义流量调整命令,使用 QoS 来处理特定的网络接口。通过放置被赋予不同的优先级的传出流量在一个队列中,达到限制传出流量速率的目的, 而传入流量通过丢包的方式来达到速率限制的目的。
事实上, wondershaper 的既定目标不仅仅是对一个接口增加其带宽上限当批量下载或上传正在进行时wondershaper 还试图去保持互动性会话如SSH 的低延迟。同样的,它还确保批量上传(e.g. , Dropbox 的同步)不会使得下载“窒息”,反之亦然。
在 Ubuntu Debian 及其衍生发行版本 中安装 wondershaper
```
$ sudo apt-get install wondershaper
```
在 Fdora 或 CentOS/RHEL (带有 [EPEL 软件仓库][2]) 中安装 wondershaper这里 链接 2 和 6 一样,可以删除其中之一)
``
$ sudo yum install wondershaper
```
wondershaper 的基本使用如下:
```
$ sudo wondershaper <interface> <download-rate> <upload-rate>
```
举个例子, 将 `eth0` 的最大下载/上传带宽分别设定为 1000Kbit/s 和 500Kbit/s:
```
$ sudo wondershaper eth0 1000 500
```
你也可以通过运行下面的命令将速率限制进行消除:
```
$ sudo wondershaper clear eth0
```
假如你对 wondershaper 的运行原理感兴趣,你可以阅读其 shell 脚本源文件(/sbin/wondershaper)。
### 总结 ###
在本教程中,我介绍了两种不同的方法,来达到如何在 Linux 桌面环境中,控制每个应用或每个接口的带宽使用的目的。 这些工具对用户都及其友好,都为用户提供了一个快速且容易的方式来调整或限制流量。 对于那些想更多地了解如何在 Linux 中进行速率控制的读者,请参考 [the Linux bible][7].
--------------------------------------------------------------------------------
via: http://xmodulo.com/limit-network-bandwidth-linux.html
作者:[Dan Nanni][a]
译者:[FSSlc](https://github.com/FSSlc)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创翻译,[Linux中国](http://linux.cn/) 荣誉推出
[a]:http://xmodulo.com/author/nanni
[1]:http://monkey.org/~marius/trickle
[2]:http://xmodulo.com/how-to-set-up-epel-repository-on-centos.html
[3]:http://xmodulo.com/create-desktop-shortcut-launcher-linux.html
[4]:http://lartc.org/wondershaper/
[5]:http://lartc.org/manpages/tc.txt
[6]:http://xmodulo.com/how-to-set-up-epel-repository-on-centos.html
[7]:http://www.lartc.org/lartc.html