HowTo: Linux Limit A Specific User’s Shell Account Network Bandwidth Using Bash Shell

by on December 4, 2012 · 10 comments· LAST UPDATED December 5, 2012

in , ,

I am using a bash shell under Ubuntu Linux operating system. Sometime I need to restrict my own Internet bandwidth for all my shell applications such as ftp, sftp, wget, curl and friends. How do I limit the network speed under bash without setting up a complicated firewall and tc rules as described here?

You need to use a portable lightweight userspace bandwidth shaper called trickle. It can run in in collaborative mode or in stand alone mode. trickle works by taking advantage of the unix loader preloading.
Tutorial details
DifficultyIntermediate (rss)
Root privilegesYes (for installation)
Requirementstrickle
Estimated completion timeN/A
Essentially it provides, to the application, a new version of the functionality that is required to send and receive data through sockets. It then limits traffic based on delaying the sending and receiving of data over a socket. trickle runs entirely in userspace and does not require root access.

Installation

Type the following apt-get command under Debian / Ubuntu Linux to install trickle software:
$ sudo apt-get install trickle
Sample outputs:

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

Install trickle under CentOS / RHEL / Fedora Linux

First, turn on EPEL repo and type the following yum command to install trickle software:
# yum install trickle
Sample outputs:

Loaded plugins: auto-update-debuginfo, protectbase, rhnplugin
0 packages excluded due to repository protections
Setting up Install Process
Resolving Dependencies
--> Running transaction check
---> Package trickle.x86_64 0:1.07-9.el6 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
=============================================================
 Package      Arch        Version            Repository
                                                        Size
=============================================================
Installing:
 trickle      x86_64      1.07-9.el6         epel       41 k
Transaction Summary
=============================================================
Install       1 Package(s)
Total download size: 41 k
Installed size: 89 k
Is this ok [y/N]: y
Downloading Packages:
trickle-1.07-9.el6.x86_64.rpm         |  41 kB     00:00
Running rpm_check_debug
Running Transaction Test
Transaction Test Succeeded
Running Transaction
  Installing : trickle-1.07-9.el6.x86_64                 1/1
  Verifying  : trickle-1.07-9.el6.x86_64                 1/1
Installed:
  trickle.x86_64 0:1.07-9.el6
Complete!

How do I use trickle?

The syntax is:

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

Examples

Start ftp client limiting its upload capacity to 100 KB/s:
trickle -u 100 ftp
Start ftp client limiting its download capacity at 50 KB/s:
trickle -d 50 ftp
You can combine both options:
trickle -u 100 -d 50 ftp
You can pass other args to the ftp command:
trickle -u 100 -d 50 ftp ftp.cyberciti.biz
trickle -u 100 -d 50 ftp ftp.cyberciti.biz 8021

Use the wget command to download an iso file from openbsd.org ftp server:
$ wget http://ftp.openbsd.org/pub/OpenBSD/5.2/i386/install52.iso
Sample outputs:

--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 

Now, use trickle to download iso file but limit capacity at 50 KB/s:
trickle -d 50 wget http://ftp.openbsd.org/pub/OpenBSD/5.2/i386/install52.iso
Sample outputs:

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

Limit bandwidth in a single shell for all commands

Launch bash or ksh shell limiting its upload capacity to 250 KB/s, and download capacity at 500 KB/s:
trickle -d 500 -u 250 bash
OR
trickle -d 500 -u 250 ksh
Now, for all programs launched inside currently running bash or ksh shell will follow bandwidth shaper rules:
wget http://example.com/foo.iso
sftp file.mp4 user@server1.cyberciti.biz:~/Downloads/

Other options

From the man page:

	-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

Check out related media



(Video 01: Linux / Unix Internet Bandwidth Throttling For A Specific Shell Using Trickle Utility)

See also:
TwitterFacebookGoogle+PDF versionFound an error/typo on this page? Help us!

{ 10 comments… read them below or add one }

1 Damir December 5, 2012 at 4:28 am

Thank you for this HowTo, very usefull, but can I limit network bandwidth for multiple ip address in my local network, which use a common Internet channel with use trickle software?

Reply

2 nixCraft December 5, 2012 at 10:16 am

Noop. Try iptables+tc.

Reply

3 Rinku December 5, 2012 at 6:08 am

Thanx

Reply

4 Timothy1 December 5, 2012 at 8:54 am

Very nice userland app, thanks but unfortunatelly `wget` is not a good example since it already has a builtin –not-too-long option called –limit-rate eg.

$ wget –limit-rate=120.5k ftp://mycloudshare.com/WeddingBlueray25gigs.iso

Wget usually doesn’t upload much so he doesn’t have a –limit-upload-rate option :).

At the begining of this article i thought that bash extended his ulimit built-in command (see: `man 1 bash`) but indeed it was an other trick :)

Reply

5 nixCraft December 5, 2012 at 10:15 am

I should have used ftp or any other tool as an example. I appreciate your feedback and comment.

Reply

6 Philippe Petrinko December 5, 2012 at 9:41 am

Vivek, a typo here (copy & paste but not edited),
You wrote 3 times:
trickle -u 100 -d 50 ftp

Instead of:
trickle -u 100 ftp
trickle -d 50 ftp
trickle -u 100 -d 50 ftp

;-)
but very nice topic to me. Well done again!

Reply

7 nixCraft December 5, 2012 at 10:12 am

Philippe,

Thanks for the heads up!

Reply

8 Paul February 2, 2013 at 5:16 pm

Thanks! I found trickle on the first site I’ve visited along with many manuals, but it didn’t work. I was trying to limit bandwidth on jhbuild, but it was launching new independent processes to download files. You gave me the trick with “bash”.

Reply

9 jcubic February 13, 2014 at 1:25 pm

If you run trickle without -s you’ll got the warning:

trickle: Could not reach trickled, working independently: No such file or directory

you need to run it with -s option or run trickled server first (call trickled command).

Reply

10 gardolom March 26, 2014 at 1:42 pm

Thanks for this tutorial.

Pardon me, where should i place this line

trickle -d 500 -u 250

Should I put it in the pureftpd.conf or proftpd.conf file? I am using cPanel on Centos and everytime I let cPanel run its automatic remote backup, I got nullrouted beucase my provider has 100 Mbps limit and it always go beyond the limit.

Reply

Leave a Comment

Tagged as: , , , , , ,

Previous Faq:

Next Faq: