Configuring bandwidth restriction on CentOS can optimize network resource allocation, ensure the stable running of critical services, and prevent bandwidth abuse. Therefore, whether you use US servers for internal server applications, hosting websites, streaming media services or running games, reasonable management bandwidth can help improve the overall network performance of US servers.
The Traffic Control (TC) tool that comes with the Linux kernel can be used to effectively restrict the network traffic of the server. The TC tool, which is part of the iproute2 package, manages data traffic via qdisc, specifying upload and download rates for specific ports or IP addresses.
The TC tool is installed on the server. By default, the tool is included in the CentOS system. However, you can run the following command to check and install the tool:
sudo yum install iproute -y
You can then use the HTB (Hierarchical token bucket) algorithm to limit the bandwidth, such as limiting the maximum upload bandwidth of the eth0 interface to 10Mbps:
sudo tc qdisc add dev eth0 root handle 1: htb default 30
sudo tc class add dev eth0 parent 1: classid 1:1 htb rate 10mbit burst 15k
If you want to further restrict traffic on a specific port, such as limiting the download rate of port 80 (HTTP traffic) to 5Mbps, you can combine iptables and TC to achieve this:
sudo iptables -A OUTPUT -t mangle -p tcp --sport 80 -j MARK --set-mark 10
sudo tc filter add dev eth0 protocol ip parent 1:0 prio 1 handle 10 fw flowid 1:1
In addition, it is an easy way to use the WonderShaper tool, which provides a more intuitive way to configure bandwidth limits, install and use the following:
sudo yum install wondershaper -y
sudo wondershaper eth0 10000 5000 # Limits downloads to 10Mbps and uploads to 5Mbps
If you want these restrictions to take effect even after the server is restarted, you can add the commands to the /etc/rc.local or systemd service.
Configure bandwidth limits for different users or IP addresses
In some scenarios, it may be necessary to limit the bandwidth of a specific user or IP address to ensure network fairness between different users. For example, to limit the bandwidth of IP 192.168.1.100 to 2Mbps, you can use the following command:
sudo tc class add dev eth0 parent 1:1 classid 1:2 htb rate 2mbit
sudo tc filter add dev eth0 protocol ip parent 1:0 prio 1 u32 match ip src 192.168.1.100 flowid 1:2
Similarly, data traffic for specific users can be marked by iptables and then restricted in tc rules.
Monitor and adjust bandwidth limits
To see your current bandwidth usage in real time, you can use tools such as iftop, nload, or iptraf. For example, install iftop and monitor with the following command:
sudo yum install iftop -y
sudo iftop -i eth0
If you need to adjust the bandwidth limit, delete an existing rule and add a new one. For example, delete all bandwidth management rules:
sudo tc qdisc del dev eth0 root
Then re-apply the new bandwidth limiting policy.
Application scenario of bandwidth restriction
Web server: Prevents a single user from occupying too much bandwidth and ensures the fairness of website access.
Game server: Limit players with high bandwidth usage and ensure smooth experience for all players.
Streaming media service: Prevent individual users from occupying the bandwidth and affecting the video playback of other users.
Intranet: Ensure that critical applications have priority in obtaining bandwidth to prevent unnecessary high traffic.
Ensure that the performance of the CentOS server is not affected, configure the bandwidth limit properly, and effectively allocate the network resources of the US server to improve stability and security. Using tools such as tc, iptables, and wondershaper, you can easily implement bandwidth management solutions in different scenarios. Whether enterprise, webmaster or game service provider, bandwidth management can be used to optimize network resources, improve user experience and reduce operating expenses.