Linux & Command Line ADVANCED Linux โฑ๏ธ 5 min read ๐Ÿ“… Updated: 23 Sep 2026

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.

Gurveer Singh
Gurveer Singh โœ“ Verified Specialist
Founder & Chief Architect ยท hSECURITIES Labs
โšก FAST-TRACK PROTOCOL

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.
๐Ÿ› ๏ธ Tools & Prerequisites: A Linux distribution with root/sudo access. Understanding of networking fundamentals (QoS, qdisc). The target network interface name (e.g., eth0, enpXsY).

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.

๐Ÿ” Root Cause & Diagnostic Overview: Bandwidth saturation, Denial of Service (DoS) simulation testing, or ensuring fair resource allocation among containerized services often requires Layer 2/3 traffic control. The kernel's Traffic Control subsystem allows administrators to police and shape outbound and inbound packets using queuing disciplines (qdiscs). Incorrect configuration can lead to dropped packets or total network connectivity loss on the targeted interface.
STEP 1

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 root
Warning: Running this command flushes ALL QoS settings on the interface. Proceed only if you intend to reconfigure traffic shaping.
STEP 2

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

STEP 3

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:10
STEP 4

Applying 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 u32 or fw match 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 eth0 to confirm all rules are active and correctly nested under the root handle.
TROUBLESHOOTING

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 eth0 shows 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 tc commands require root privileges (use sudo).
FINAL STEP

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 eth0 to 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.
Resolution Complete: You have successfully implemented bandwidth shaping using HTB and traffic filtering. Remember that process isolation is achieved by matching the network characteristics (L3/L4) of the traffic, not directly by Process ID (PID). Always clean up rules when done to prevent performance degradation or connectivity loss.

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.

COMMAND LINE / TERMINAL
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.

Engineering Assistance

Need help implementing this solution?

Our cybersecurity and infrastructure engineers can audit, harden, or configure your systems directly.

Request Technical Assistance โ†’
Gurveer Singh
Gurveer Singh โœ“ Verified Author
Founder & CEO at hSECURITIES | Cybersecurity Educator | Security Researcher | Ethical Hacking Enthusiast | Technology Mentor
Meet the Founder โ†’

Gurveer Singh Saggu is the Founder and CEO of hSECURITIES, a cybersecurity and technology education platform focused on practical learning, digital security awareness, hardware diagnostics, Linux administration, and real-world cybersecurity training.

With a strong passion for technology, ethical hacking, and security education, he has spent years helping students, professionals, and organizations understand modern cyber threats and develop practical technical skills. Through hSECURITIES, he creates hands-on learning resources, technical guides, security awareness content, and community-driven initiatives that make cybersecurity more accessible.

His mission is to bridge the gap between theoretical knowledge and practical implementation by providing step-by-step learning experiences, technical troubleshooting resources, and security-focused educational platforms.

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.

Ask a Question or Share Solution