brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: tc netem adds fake latency so you can test a slow network on Linux

Linux
~/tips/tip-linux-tc-netem-fake-latency

tc qdisc add dev eth0 root netem delay 100ms

Make a fast link behave like a slow or lossy one to see how your app copes.

Everything feels fast on a local network, which hides how an app behaves on a bad mobile connection. The netem queue discipline in tc lets the kernel slow down your own traffic on purpose.

Add a delay to everything that leaves an interface:

tc qdisc add dev eth0 root netem delay 100ms

From then on, every packet that goes out of eth0 waits 100 ms. You can make it worse while it runs, with jitter and a bit of packet loss on top:

tc qdisc change dev eth0 root netem delay 200ms 20ms loss 1%
tc qdisc show dev eth0

The second number after delay is the jitter, so packets now take somewhere around 180 to 220 ms. tc qdisc show tells you what is active, which is worth checking before you start blaming the app.

When you are done, remove it again:

tc qdisc del dev eth0 root

netem only touches outgoing traffic on the interface you name. If you are logged in over SSH on that same interface, your own session gets slow too, and a high loss value can make it hard to type the del command. I would test on a second interface, a VM, or the loopback device lo when the client and server run on the same machine. The setting is not permanent; a reboot also clears it.

Further reading: tc-netem(8) manual page, tc(8) manual page

More Linux tips a2query -m rewrite renice -n 10 -p 4242 cat config.yml | xsel -ib xvfb-run -a your-program --some-flag dpkg --get-selections > packages.txt All Linux tips (261) All topics

Link