Linux – How to limit the upload/download bandwidth on the CentOS server

linuxtctraffic-shaping

How can I limit the upload and download bandwidth on my CentOS server? This is a box with a single interface, eth0.
Ideally, I would like a command-line solution (I've been trying to use tc), something that I could easily switch on and off in a script.

So far I've been trying to do something like tc filter add dev eth0 protocol ip prio 50 u32 police rate 100kbit burst 10240 drop but I'm obviously missing a lot of knowledge and information. Can somebody help with a quick one-liner?

Many thanks,
Dan

Best Answer

I'm having trouble adding the script code for you here, it is available via this gist - https://gist.github.com/akrasic/7242498#file-limit-tc-sh

The script is using TC and Hierarchical Token Bucket (HTB) to define the rules and lastly the filters that would act as a catch-all rule.

You would need to modify the interface var if it's different than eth0˙and the interface_speed .

Save the script on your server, and start it via: ḃash limit-tc.sh start

To stop: bash limit-tc.sh stop

View status: bash limit.tc status

Status output would look something like this:

qdisc htb 1: root refcnt 2 r2q 10 default 30 direct_packets_stat 1  
 Sent 535666 bytes 6461 pkt (dropped 172, overlimits 7641 requeues 0)  
 rate 0bit 0pps backlog 0b 0p requeues 0  
qdisc sfq 10: parent 1:10 limit 127p quantum 1514b perturb 10sec  
 Sent 535624 bytes 6424 pkt (dropped 172, overlimits 0 requeues 0)   
 rate 0bit 0pps backlog 0b 0p requeues 0  
qdisc sfq 20: parent 1:20 limit 127p quantum 1514b perturb 10sec  
 Sent 0 bytes 0 pkt (dropped 0, overlimits 0 requeues 0)  
 rate 0bit 0pps backlog 0b 0p requeues 0