LLI Wiki — My Digital Knowledge Base

Linux: simulate network latency with tc and netem

Shell script to add or remove a fixed delay on a Linux network interface with tc and netem.

This shell script adds, changes or removes a fixed delay on a Linux network interface, using the netem queueing discipline of tc.

Script

#!/bin/sh
interface="$1"
lag_millisecs="$2"

if [ "$lag_millisecs" = 0 ];then
	tc qdisc del dev "$interface" root netem &>/dev/null || echo "no lag configured"
elif [ -n "$lag_millisecs" ];then
	tc qdisc del dev "$interface" root netem &>/dev/null
	tc qdisc add dev "$interface" root netem delay "${lag_millisecs}ms"
else
	echo "Usage: $0 <interface> <delay in millisecs>"
fi

Usage

The script takes two arguments: the network interface name and the delay in milliseconds.

Argument Description
interface Network interface name (for example eth0)
lag_millisecs Delay to add, in milliseconds. The value 0 removes the existing configuration

Example:

./<script_name>.sh eth0 100
./<script_name>.sh eth0 0

Notes

Mastodon LLIMastodon SocialFosstodon