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

Posted on in Categories , , , last updated September 18, 2015

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


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

  trickle.x86_64 0:1.07-9.el6                                


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


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
trickle -u 100 -d 50 ftp 8021

Use the wget command to download an iso file from ftp server:
$ wget
Sample outputs:

--2012-12-04 16:00:16--
Connecting to||: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
Sample outputs:

trickle: Could not reach trickled, working independently: No such file or directory
--2012-12-04 16:00:32--
Connecting to||: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
trickle -d 500 -u 250 ksh
Now, for all programs launched inside currently running bash or ksh shell will follow bandwidth shaper rules:
sftp file.mp4 [email protected]:~/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:

Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin and a trainer for the Linux operating system/Unix shell scripting. He has worked with global clients and in various industries, including IT, education, defense and space research, and the nonprofit sector. Follow him on Twitter, Facebook, Google+.

11 comment

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

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

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

  3. 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!

  4. 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”.

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

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

Leave a Comment