Advanced Linux: Programmatic Bandwidth Throttling using iproute2 and tc
This guide teaches advanced Linux users how to use the iproute2 utility and the traffic control (tc) subsystem to precisely limit or shape the bandwidth available to specific processes or network interfaces.
Direct Diagnostic & Remediation Summary
- Prerequisite Check: Ensure you have root privileges:
sudo su -. - Add Root Qdisc: Apply a root queuing discipline (qdisc) to the target interface:
sudo tc qdisc add dev eth0 root handle 1: htb default 10. - Create Class Structure: Define bandwidth classes using 'htb' for shaping:
sudo tc class add dev eth0 parent 1: classid 1:1 parent 1:1 classid 1:10 htb rate 1mbit ceil 2mbit. - Apply Filter (Optional): Direct traffic matching specific criteria to the limited class ID.
How to Use iproute2 and tc for Process Bandwidth Throttling on Linux
Network traffic shaping is a critical skill for server administration, ensuring fair resource usage among multiple applications or services. This tutorial walks you through using the iproute2 suite and Traffic Control (tc) to implement granular bandwidth limitations.
Initial Triage & Environment Isolation
Before applying any rules, you must identify your target interface and ensure no existing restrictive policies interfere. We will use the ip command to list interfaces.
- Action: Identify the primary network interface name (e.g., eth0, ens33).
- Command Check: Execute
ip a. Note the name of the active connection.
Next, we flush any existing traffic control rules to ensure a clean slate for our new configuration.
sudo tc qdisc del dev eth0 rootEstablishing the Root Queuing Discipline (qdisc)
We use the Hierarchical Token Bucket (HTB) qdisc as it provides robust parent/child rate limiting capabilities. This must be attached to the root of the interface's traffic flow.
- Action: Add the HTB qdisc to the root handle (handle 1:). We set a default class ID that will govern general traffic.
- Command Example (Assuming eth0):
sudo tc qdisc add dev eth0 root handle 1: htb default 10.
This establishes the overall container for all future rate limits.
Creating Rate-Limited Child Classes
Now we define specific classes representing different traffic groups (e.g., 'Critical App' vs. 'Background Download'). We will create a parent class structure under the root.
- Action: Define a specific rate limit for a group (Class ID 1:10). Here, we cap it at 500 Kbps guaranteed, with a ceiling of 2 Mbps.
- Command Example:
sudo tc class add dev eth0 parent 1: classid 1:10 htb rate 500kbit ceil 2mbit.
For process isolation, you typically need to combine HTB with filtering (like u32 or fw) that matches source/destination IPs or ports associated with the target process.
# Example: Filter traffic destined for port 80 from a specific IP range and direct it to our limited class ID
sudo tc filter add dev eth0 protocol ip parent 1: prio 1 u32 match ip dport 80/tcp flowid 1:10Applying Process Association (Advanced Filtering)
Directly limiting a process is complex as the kernel network stack operates on packets, not PIDs. The most reliable method involves identifying traffic streams associated with that process's connection endpoints (IP/Port).
- Concept: Use the
u32orfwmatch type in a filter rule. - Hypothetical Example (If PID 1234 uses port 5000): You would need to know the source/destination IPs and ports used by that process. The filter rule targets these L3/L4 characteristics, not the PID directly.
- Verification Check: Run
sudo tc qdisc show dev eth0to confirm all rules are active and correctly nested under the root handle.
Common Pitfalls & Advanced Diagnostics
If throttling does not appear to work, check these common failure points:
- Pitfall 1: Missing Root Qdisc. You cannot add child classes without a parent root qdisc in place. Check if
tc qdisc show dev eth0shows the expected structure. - Pitfall 2: Filter Mismatch. The filter rule must precisely match the traffic (protocol, ports, IPs) generated by the process you wish to limit. A single wrong port will cause the traffic to bypass your rules entirely.
- Pitfall 3: Privilege Escalation. All
tccommands require root privileges (usesudo).
Verification, Results & Long-Term Stability Validation
To verify the throttling is active, you must generate controlled traffic while monitoring the system.
- Test Setup: Run a bandwidth-intensive tool (e.g., iPerf3) from the machine, and simultaneously monitor the interface statistics.
- Monitoring Command: Use
watch -n 1 ip -s link show eth0to observe packet counts. If throttling is working correctly, sustained high traffic rates will stabilize at or below the defined 'rate' limit (e.g., 500 Kbps). - Cleanup: When finished, always remove all custom rules to restore default networking behavior:
sudo tc qdisc del dev eth0 root.
Frequently Asked Questions
Q: Can I throttle bandwidth per user instead of per process?
A: Yes. Instead of complex process filtering, you can often use firewall rules (iptables/nftables) combined with tc to limit traffic based on the source IP address assigned to a specific user session or machine.
Q: What is the difference between 'rate' and 'ceil' in HTB?
A: The rate defines the guaranteed minimum bandwidth (the sustained rate). The ceil defines the absolute maximum bandwidth that can ever be used, even if the link is otherwise idle. Traffic will always flow at a rate no lower than 'rate' if capacity permits.
Q: Does this work across different Linux distributions?
A: The core tc functionality is standard in modern Linux kernels (2.6+). However, the exact syntax or recommended companion tools might vary slightly between RHEL/CentOS derivatives and Debian/Ubuntu.
sudo tc qdisc del dev eth0 root
sudo tc qdisc add dev eth0 root handle 1: htb default 10
sudo tc class add dev eth0 parent 1: classid 1:10 htb rate 500kbit ceil 2mbit
sudo tc filter add dev eth0 protocol ip parent 1: prio 1 u32 match ip dport 80/tcp flowid 1:10
sudo tc qdisc show dev eth0
sudo tc qdisc del dev eth0 root
Frequently Asked Questions
What is the best interface name to use for testing?
Use a dedicated virtual interface (like veth pair) or an interface that carries non-critical test traffic, never your primary management link unless you fully understand the rollback procedure.
How do I check if tc rules are applied correctly?
Run <code>sudo tc qdisc show dev eth0</code>. You should see a clear hierarchy starting with 'qdisc htb' at the root level.
What happens if I use incorrect IP addresses in my filter rule?
The filter will simply fail to match any traffic, and your bandwidth throttling rules will be effectively bypassed for that specific stream.
Need help implementing this solution?
Our cybersecurity and infrastructure engineers can audit, harden, or configure your systems directly.
Community Discussion & Confirmations (0)
Technical questions, hardware findings, and verified fix reports.
No reader questions posted yet. Have you tried this solution or experienced an edge case? Post below.