How to Simulate Real-World Network Conditions with Linux Netem and tc
This guide explains how to use Linux's Netem module via the tc command to emulate network conditions such as delay, loss, duplication, corruption, and packet reordering, providing practical examples and command syntax for effective testing and troubleshooting of network performance issues.
Simulating Network Conditions with Netem
In operations, the most complex problems are often network‑related, and the hardest part is reproducing them. Netem, available in Linux kernels 2.6 and later, is a powerful network emulation module that can simulate low bandwidth, latency, packet loss, and other common network faults.
Netem is controlled through the
tc(traffic control) command, which is part of the iproute2 suite.
tcmanages traffic by queuing packets at the output interface, allowing you to limit or shape outgoing traffic.
The main
tcactions for Netem are add (create a rule), change (modify an existing rule), replace (replace a rule), and del (delete a rule).
Simulating Delay
<code>tc qdisc add dev eth0 root netem delay 100ms</code>This command adds a 100 ms delay to all packets sent on
eth0. Netem also supports jitter, correlation, and distribution parameters:
JITTER : adds random variation (e.g., 10 ms) to the base delay.
CORRELATION : sets the percentage similarity between consecutive delay events.
distribution : chooses a delay distribution such as
uniform,
normal,
pareto, or
paretonormal.
Example with jitter:
<code>tc qdisc add dev eth0 root netem delay 100ms jitter 10ms</code>Example with correlation:
<code>tc qdisc add dev eth0 root netem delay 100ms 25% </code>Example with a specific distribution:
<code>tc qdisc add dev eth0 root netem delay 100ms distribution normal</code>Simulating Packet Loss
<code>tc qdisc add dev eth0 root netem loss 50%</code>This introduces a 50 % packet loss rate. Loss can also have a correlation factor:
<code>tc qdisc add dev eth0 root netem loss 50% 25%</code>Here, 50 % of packets are dropped, and the loss of the current packet is 25 % correlated with the previous one.
Simulating Packet Duplication
<code>tc qdisc add dev eth0 root netem duplicate 50%</code>This creates duplicate packets at a 50 % rate, with optional correlation similar to loss.
Simulating Packet Corruption
<code>tc qdisc add dev eth0 root netem corrupt 30%</code>This corrupts 30 % of packets, which can be observed as increased loss in tools like
ping.
Simulating Packet Reordering
Netem provides two methods to reorder packets.
<code>tc qdisc add dev eth0 root netem reorder 50% gap 3 delay 100ms</code>Every third packet is delayed by 100 ms, causing out‑of‑order delivery.
<code>tc qdisc change dev eth0 root netem reorder 50% 15% delay 300ms</code>This randomly reorders 50 % of packets with a 15 % correlation and adds a 300 ms delay to the reordered packets.
Viewing Current Netem Configuration
<code>tc qdisc show dev eth0</code>The
showcommand lists all active Netem rules, helping you verify and adjust the simulated network conditions.
Using Netem to emulate weak network environments is essential for testing and troubleshooting network‑related issues.
Efficient Ops
This public account is maintained by Xiaotianguo and friends, regularly publishing widely-read original technical articles. We focus on operations transformation and accompany you throughout your operations career, growing together happily.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.