limit network bandwidth in linux

How to Limit Network Bandwidth in Linux

System administrators need to often control the available network bandwidth between users and applications. Otherwise, it may happen that one application takes up all bandwidth. In this article, we will learn how to limit network bandwidth in Linux using trickle bandwidth shaper.


How to Limit Network Bandwidth in Linux

Trickle is a network bandwidth shaper tool that allows you to control the upload & download speeds of applications to prevent any of them from overusing your total available bandwidth. There are other network shapers also that control per user bandwidth. Trickle controls bandwidth for each application.

Trickle acts as a proxy between an application and the socket/port it uses. It even allows users to set their own traffic limits. It even allows you to set overall system limits that users cannot exceed.

We will use ncftpput and ncftpget, both present in ncftp which we will install on client 54.43.32.21. We will also install vsftpd on server 54.43.32.22. We will basically upload file from client to server ftp and measure upload speeds using trickle.


1. Install ncftp and vsftpd

Open terminal on client and run the following command.

# yum update && sudo yum install ncftp		[On RedHat based systems]
# aptitude update && aptitude install ncftp	[On Debian based systems]	

Open terminal on server, and run the following command to install vsftpd.

# yum update && yum install vsftpd 	[On RedHat based systems]
# apt update && apt install vsftpd 	[On Debian based systems]

Open vsftpd configuration file on server in a text editor.

$ sudo nano /etc/vsftpd/vsftpd.conf
OR
$ sudo /etc/vsftpd.conf

Make following changes.

anonymous_enable=NO
local_enable=YES
chroot_local_user=YES
allow_writeable_chroot=YES

Save and close the file. Run the following commands to start vsftpd and enable it to autostart during system reboot.

# systemctl start vsftpd 		[For systemd-based systems]
# systemctl enable vsftpd
# service vsftpd start 			[For init-based systems]
# chkconfig vsftpd on


2. Install Trickle

Open terminal on client system and run the following command to install trickle.

# yum -y update && yum install trickle 	[On RedHat based systems]
# apt -y update && apt install trickle 	[On Debian based systems]


3. Run Trickle

You can run trickle in standalone or supervised mode. In standalone mode, trickle is used to set the speed of specific command or application but in supervised mode, we define global bandwidth limits in a configuration file which is applied to all applications that use trickle. Here is the basic syntax of trickle in standalone mode

# trickle -s -d [download rate in KB/s] -u [upload rate in KB/s] command

In the above command -s stands of standalone more, -u is for upload speed and -d is for download speed.

For example, if you run the following command, to upload the file from client to ftp server,

# ncftpput -u username -p password 54.43.32.22  /testdir LinuxFun.pdf 

You will see the following output.

LinuxFun.pdf:                                        	2.79 MB   52.02 MB/s

As you can see, the upload speed is 52.02Mbps.

If you run the same command using trickle, where we specify upload speed as 5kbps using -u 5,

# trickle -s -u 5 ncftpput -u username -p password 54.43.32.22 /testdir LinuxFun.pdf 

You will see the following output.

LinuxFun.pdf:                                        	2.79 MB	4.94 kB/s

Here you can see that the upload speed has been reduced by trickle to avoid bandwidth throttle.

If you want to set upload and download speed of all applications, you can do so tricked command. Here is an example to limit upload speed to 10kbps and download speed to 50kbps.

# trickled -d 50 -u 10

tricked command will use the trickle daemon and apply bandwidth limits across all applications on the system.

Alternatively, you can open trickle configuration file /etc/trickled.conf in text editor and update its parameters to define bandwidth limits.

Now if you simply run a command within trickle, without specifying upload/download limits, it will automatically apply the limits defined in trickled command above. For example, if you run the following command.

# trickle ncftpput -u username -p password 54.43.32.22 /testdir LinuxFun.pdf 

you will see the following output.

LinuxFun.pdf:                                        	2.79 MB	9.94 kB/s

As mentioned earlier, you can also edit /etc/trickled.conf configuration file which consists of sections for each service. Here is the typical format for each service.

[service] #name of service 
Priority = <value> # priority of service, lower value is higher priority
Time-Smoothing = <value> # time interval to allow data transfer for service
Length-Smoothing = <value> # smoothing based on amount of data transferred, instead of amount of time.

Here is a sample configuration to set limits for ssh and ftp services.

[ssh]
Priority = 1
Time-Smoothing = 0.1
Length-Smoothing = 2

[ftp]
Priority = 2
Time-Smoothing = 1
Length-Smoothing = 3

In this configuration SSH has priority over FTP.

In this article, we have learnt how to limit network bandwidth in Linux. You can customize it as per your requirement. Trickle is very easy to install and use for system administrators to control network bandwidth in Linux.

Also read:

How to Exclude Files & Folders From Copying
How to Copy Files to CD in Linux
How to Merge Folders & Directories in Python
How to Merge Folders & Directories in Linux
How to Split Folder into Subfolders in Linux

Leave a Reply

Your email address will not be published. Required fields are marked *