如何Linux使用Bash Shell和Trickle限制特定用户Shell帐户的网络带宽

时间:2020-01-09 10:41:44  来源:igfitidea点击:

在Ubuntu Linux操作系统下 如何为所有shell程序(例如ftp,sftp,wget,curl和好友)限制自己的Internet带宽。
如何在不设置复杂的防火墙和tc规则的情况下限制bash下的网络速度?

您需要使用一种称为trickle的便携式轻量级用户空间带宽整形器。
它可以以协作模式或独立模式运行。
trickle通过利用unix加载程序的预加载来工作。

从本质上讲,它为应用程序提供了通过套接字发送和接收数据所需的功能的新版本。
然后,它基于延迟通过套接字发送和接收数据来限制流量。
细流完全在用户空间中运行,不需要root访问。

安装

在Debian/Ubuntu Linux下执行以下apt-get命令以安装trickle软件:

$ sudo apt-get install trickle

输出示例:

Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following NEW packages will be installed:
  trickle
0 upgraded, 1 newly installed, 0 to remove and 20 not upgraded.
Need to get 43.0 kB of archives.
After this operation, 180 kB of additional disk space will be used.
Get:1 http://debian.osuosl.org/debian/ squeeze/main trickle amd64 1.07-9 [43.0 kB]
Fetched 43.0 kB in 1s (30.6 kB/s)                     
Selecting previously deselected package trickle.
(Reading database ... 280975 files and directories currently installed.)
Unpacking trickle (from .../trickle_1.07-9_amd64.deb) ...
Processing triggers for man-db ...
Setting up trickle (1.07-9) ...

在CentOS/RHEL/Fedora Linux下安装trickle

首先,打开EPEL repo并执行以下yum命令以安装细流软件:

# yum install trickle

如何使用trickle?

语法为:

trickle -u uploadLimit program
trickle -d downloadLimit program
trickle -u {UPLOAD_LIMIT} -d {DOWNLOAD_LIMIT} program-binary

例子

启动ftp客户端,将其上载容量限制为100 KB/s:

trickle -u 100 ftp

启动ftp客户端,将其下载容量限制为50 KB/s:

trickle -d 50 ftp

您可以结合使用这两个选项:

trickle -u 100 -d 50 ftp

您可以将其他参数传递给ftp命令:

trickle -u 100 -d 50 ftp ftp.theitroad.local
trickle -u 100 -d 50 ftp ftp.theitroad.local 8021

使用wget命令从openbsd.org ftp服务器下载iso文件:

$ wget http://ftp.openbsd.org/pub/OpenBSD/5.2/i386/install52.iso

输出示例:

--2012-12-04 16:00:16--  http://ftp.openbsd.org/pub/OpenBSD/5.2/i386/install52.iso
Resolving ftp.openbsd.org... 129.128.5.191
Connecting to ftp.openbsd.org|129.128.5.191|:80... connected.
HTTP request sent, awaiting response... 206 Partial Content
Length: 237457408 (226M), 230422880 (220M) remaining [text/plain]
Saving to: `install52.iso'

7% [>                   ] 1,86,94,640 2.94M/s  eta 79s

现在,使用细流下载iso文件,但将容量限制为50 KB/s:

trickle -d 50 wget http://ftp.openbsd.org/pub/OpenBSD/5.2/i386/install52.iso

输出示例:

trickle: Could not reach trickled, working independently: No such file or directory
--2012-12-04 16:00:32--  http://ftp.openbsd.org/pub/OpenBSD/5.2/i386/install52.iso
Resolving ftp.openbsd.org... 129.128.5.191
Connecting to ftp.openbsd.org|129.128.5.191|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 237457408 (226M) [text/plain]
Saving to: `install52.iso.2'

0% [                    ] 2,45,760    49.9K/s  eta 77m 22s

在所有命令中限制单个shell的带宽

启动bash或ksh shell,将其上传容量限制为250 KB/s,下载容量为500 KB/s:

trickle -d 500 -u 250 bash

或者

trickle -d 500 -u 250 ksh

现在,对于在当前运行的bash或ksh shell中启动的所有程序,将遵循带宽整形器规则:

wget http://example.com/foo.iso
sftp file.mp4 [email protected]:~/Downloads/

其他选项

-h           Help (this)
	-v           Increase verbosity level
	-V           Print trickle version
	-s           Run trickle in standalone mode independent of trickled
	-d     Set maximum cumulative download rate to  KB/s
	-u     Set maximum cumulative upload rate to  KB/s
	-w   Set window length to  KB 
	-t  Set default smoothing time to  s
	-l   Set default smoothing length to  KB
	-n     Use trickled socket name 
	-L       Set latency to  milliseconds