Linux tc netem: Simulate Latency, Packet Loss and Jitter - 夜莺博客

Linux tc netem: Simulate Latency, Packet Loss and Jitter

Before an application meets a real WAN, it should meet tc netem — the Linux kernel's built-in network emulator. netem is a queuing discipline (qdisc) that deliberately delays, drops, corrupts, duplicates or reorders packets on an interface, turning a healthy host into a convincing stand-in for a congested transatlantic link. It is the standard tool for testing retry logic, timeouts and failover before users find the bugs. This guide covers the core netem commands and the discipline of scoping them so you do not break your own SSH session.

Adding Latency and Jitter

# Fixed 100ms delay on all egress traffic
sudo tc qdisc add dev eth0 root netem delay 100ms
# 100ms + 20ms jitter (uniform)
sudo tc qdisc add dev eth0 root netem delay 100ms 20ms
# Normal distribution, more realistic
sudo tc qdisc add dev eth0 root netem delay 100ms 20ms distribution normal
# 25% correlation: consecutive packets get similar delays (bursty)
sudo tc qdisc add dev eth0 root netem delay 100ms 20ms 25%

netem is egress-only and one-directional: 100ms of egress delay on one host adds ~100ms to ping RTT (which sums both directions), not 200ms.

Packet Loss, Corruption, Duplication, Reordering

# 5% random loss
sudo tc qdisc add dev eth0 root netem loss random 5%
# Bursty loss with the Gilbert-Elliott model (realistic for wireless)
sudo tc qdisc add dev eth0 root netem loss gemodel 1% 10% 70% 0.1%
# Corrupt 5% of packets (single bit flip)
sudo tc qdisc add dev eth0 root netem corrupt 5%
# Duplicate and reorder
sudo tc qdisc add dev eth0 root netem duplicate 3%
sudo tc qdisc add dev eth0 root netem delay 50ms reorder 25% 50%

Scope It: Don't Break Your Own Session

A root qdisc shapes everything, including your SSH replies — scope the impairment to one destination or port with an intermediate class (htb or prio) and a filter:

# Class-based: impair only traffic to the database server's port
sudo tc qdisc add dev eth0 root handle 1: htb default 10
sudo tc class add dev eth0 parent 1: classid 1:1 htb rate 1gbit
sudo tc class add dev eth0 parent 1:1 classid 1:20 htb rate 1gbit
sudo tc qdisc add dev eth0 parent 1:20 handle 20: netem delay 100ms loss random 5%
sudo tc filter add dev eth0 parent 1: protocol ip prio 1 u32 match ip dport 5432 0xffff flowid 1:20

Now PostgreSQL traffic to port 5432 experiences 100ms delay and 5% loss while your SSH and monitoring stay clean. If you only need to impair a single IP, a prio qdisc with a match filter is enough for most labs.

Verify and Clean Up

ping -c 100 TARGET | tail -5          # check added latency/loss
sudo tc -s qdisc show dev eth0        # drop/delay counters
watch -n1 'tc -s qdisc show dev eth0'
# remove everything
sudo tc qdisc del dev eth0 root

Combined with iperf3 (see our bandwidth testing guide), netem lets you prove how throughput and retransmissions degrade under loss, and with tcpdump you can watch TCP react. Remember to remove the qdisc when the test is over — a forgotten 300ms delay on a production interface is a classic self-inflicted incident.

原文链接:https://oneuptime.com/blog/post/2026-03-04-simulate-network-latency-packet-loss-tc-netem-rhel-9/view