Command Linux TC: A Comprehensive Guide to Traffic Control
The tc command in Linux stands for "traffic control" and is a powerful tool used to manage network traffic, bandwidth, and Quality of Service (QoS). It’s an essential utility for network administrators, system administrators, and anyone dealing with network traffic optimization and shaping. In this guide, we’ll walk you through the basics of the tc command and give you practical examples of how to use it effectively on your Linux system. So buckle up and let's dive into the world of traffic control!
What is the Linux TC Command?
The tc command is part of the iproute2 suite of utilities in Linux. It is primarily used for managing the traffic control subsystem of the Linux kernel, which controls how data is queued, scheduled, and routed through the network interfaces of a system. By using the tc command, you can shape, schedule, and filter network traffic to improve network performance, guarantee bandwidth, or even simulate network conditions for testing purposes.
Traffic control involves managing the flow of data across network interfaces. tc allows administrators to control the flow of packets, prioritize certain traffic types, and enforce rules that ensure fair distribution of network resources. Whether you're looking to prioritize HTTP traffic over FTP traffic, create custom QoS rules, or simply test how your network performs under various conditions, the tc command is an invaluable tool.
How Does Traffic Control Work in Linux?
Linux traffic control uses a system of queues and schedulers to manage network traffic. There are a few key components involved in this system:
- Queuing Discipline (qdisc): A queuing discipline is a set of rules that governs how packets are enqueued (queued) for transmission. A qdisc can be used to implement traffic shaping, scheduling, or prioritization. Examples of qdiscs include pfifo_fast, htb, and tbf.
- Classes: Classes allow the organization of traffic into different groups. You can assign specific traffic types to different classes, each with its own set of rules.
- Filters: Filters allow you to classify traffic based on criteria such as IP address, port number, or protocol type. Once traffic is filtered, it is assigned to the appropriate queue or class.
By combining qdiscs, classes, and filters, you can build a sophisticated traffic control system tailored to your network's specific needs.
Installing and Using the TC Command
Most modern Linux distributions come with the tc command pre-installed as part of the iproute2 package. However, if you don’t have it installed, you can install it easily using your package manager:
- On Debian/Ubuntu:
sudo apt update && sudo apt install iproute2
- On CentOS/RHEL:
sudo yum install iproute
- On Fedora:
sudo dnf install iproute
Once installed, you can start using the tc command to manage traffic on your system.
Basic Syntax of the TC Command
The basic syntax of the tc command is as follows:
tc qdisc [add|change|del] dev[options]
Let’s break down this syntax:
- qdisc: Refers to the queuing discipline that you want to add, modify, or delete.
- dev
: Specifies the network interface on which you want to apply the traffic control settings (e.g., eth0, wlan0). - [options]: These are additional parameters and configurations specific to the qdisc you are using, such as bandwidth limits or class configurations.
Examples of Using the TC Command
Now that you have a basic understanding of the tc command, let's look at some practical examples of how to use it to manage network traffic.
1. Setting Up a Simple Queueing Discipline
One of the simplest ways to control traffic is by using a basic queueing discipline (qdisc). For example, the pfifo_fast qdisc is a simple First In, First Out (FIFO) scheduler that is used by default on most Linux systems. To add this qdisc to an interface, use the following command:
sudo tc qdisc add dev eth0 root pfifo_fast
This command applies the pfifo_fast qdisc to the eth0 interface. Now, packets will be queued and transmitted in the order they arrive, with no prioritization or shaping applied.
2. Traffic Shaping with the Token Bucket Filter (TBF)
If you want to limit the bandwidth available to a specific interface, you can use the TBF qdisc. The TBF allows you to set a rate limit on traffic, simulating a slower connection. For example, if you want to limit the bandwidth of eth0 to 1 Mbps, you can use the following command:
sudo tc qdisc add dev eth0 root tbf rate 1mbit burst 32kbit latency 400ms
This command applies the TBF qdisc to eth0, limiting the rate to 1 Mbps. The burst parameter defines how much data can be transmitted in a burst, while latency sets the maximum delay.
3. Prioritizing Specific Traffic Using HTB
Suppose you want to prioritize specific types of traffic, such as HTTP traffic, over other types of traffic. You can use the HTB (Hierarchical Token Bucket) qdisc for this. Here’s how to set up HTB on the eth0 interface:
sudo tc qdisc add dev eth0 root handle 1: htb default 20
This command creates a root HTB qdisc on the eth0 interface. Next, you can create classes to define different types of traffic:
sudo tc class add dev eth0 parent 1: classid 1:1 htb rate 10mbit
This creates a class that allows traffic with a maximum rate of 10 Mbps. You can continue to add different classes for different traffic types and set up filters to assign packets to these classes based on IP address, port, or protocol.
4. Viewing Traffic Control Settings
To view the current traffic control settings on an interface, you can use the following command:
tc -s qdisc show dev eth0
This command displays the current qdisc, classes, and statistics for the eth0 interface. You can also use the -s flag to show statistics related to the traffic control settings.
Advanced Features of TC
Beyond basic traffic shaping and prioritization, the tc command has a wide range of advanced features, including:
- Advanced queuing: Create complex queuing hierarchies to manage multiple traffic types and ensure fair resource allocation.
- Network simulation: Simulate network conditions like latency, packet loss, and bandwidth throttling for testing purposes.
- Packet filtering: Use filters to classify traffic and direct it to specific classes or queues.
Conclusion
The tc command is an incredibly powerful tool for managing and controlling network traffic in Linux. By understanding how to use its various features, you can improve the performance of your network, prioritize critical applications, and ensure fair bandwidth distribution. Whether you're a network administrator looking to optimize your system or a developer testing network conditions, the tc command is an indispensable tool for anyone working with Linux networking.
Experiment with the examples in this article, and try out different queuing disciplines, filters, and bandwidth limits. Once you get the hang of it, you’ll be able to fine-tune your network and provide the best possible service for all your users. Happy traffic shaping!

Komentarze (0) - Nikt jeszcze nie komentował - bądź pierwszy!