Skip to content

Instantly share code, notes, and snippets.

@trongthanh
Created September 6, 2011 04:37
Show Gist options
  • Save trongthanh/1196596 to your computer and use it in GitHub Desktop.
Save trongthanh/1196596 to your computer and use it in GitHub Desktop.
Emulate slow Internet connection speed on localhost with netem (Ubuntu)
#Refer: http://www.linuxfoundation.org/collaborate/workgroups/networking/netem#Delaying_only_some_traffic
#Refer: http://www.bomisofmab.com/blog/?p=100
#Refer: http://drija.com/linux/41983/simulating-a-low-bandwidth-high-latency-network-connection-on-linux/
#Setup the rate control and delay
sudo tc qdisc add dev lo root handle 1: htb default 12
sudo tc class add dev lo parent 1:1 classid 1:12 htb rate 56kbps ceil 128kbps
sudo tc qdisc add dev lo parent 1:12 netem delay 200ms
#Remove the rate control/delay
sudo tc qdisc del dev lo root
#To see what is configured on an interface, do this
sudo tc -s qdisc ls dev lo
#Replace lo with eth0/wlan0 to limit speed from wide lan
@dougmolineux
Copy link

Awesome stuff, thank you, just to confirm, this simulates a 56K connection?

@canteroalejandro
Copy link

Works awesome bro.!!

@lautarobock
Copy link

is posible to do this only for localhost?

@edsonhoraciojunior
Copy link

Thank you very much! This was of great help to me!

I used it for emulating slow internet to test how some requests behave in Ionic / AngularJS mobile app.

#Replace lo with eth0/wlan0 to limit speed from wide lan

In my case it was different values: enp3s0 or wlp2s0. You can check you're own possible values using ifconfig if you're on Ubuntu.

@ismdcf
Copy link

ismdcf commented Sep 11, 2018

I'm getting the following error when running both the above commands
RTNETLINK answers: Operation not permitted

@vasica38
Copy link

@ismdcf, do sudo

@skaspi
Copy link

skaspi commented Jun 10, 2019

Works great!!Thanks!!!

@trongthanh
Copy link
Author

OMG. I literally forgot I created this gist and missed so many comments here because Gist previously never notify owner. Today is the first time I receive notifications from Gist. Thanks for the comments, folks.

@pemato
Copy link

pemato commented May 27, 2023

With some usability addition:

#!/bin/bash

# Refer: http://www.linuxfoundation.org/collaborate/workgroups/networking/netem#Delaying_only_some_traffic
# Refer: http://www.bomisofmab.com/blog/?p=100
# Refer: http://drija.com/linux/41983/simulating-a-low-bandwidth-high-latency-network-connection-on-linux/
# Refer: https://gist.github.com/trongthanh/1196596

case "$1" in                                                                                                                                                  
  start)
    # Setup the rate control and delay
    sudo tc qdisc add dev lo root handle 1: htb default 12
    sudo tc class add dev lo parent 1:1 classid 1:12 htb rate 3000kbps ceil 6000kbps
    sudo tc qdisc add dev lo parent 1:12 netem delay 15ms
  ;;      
 
  stop)
    # Remove the rate control/delay
    sudo tc qdisc del dev lo root
  ;;                                  
                                                           
  status)                                                                            
    # To see what is configured on an interface, do this 
    tc -s qdisc ls dev lo
  ;;
       
  *)                               
    echo "usage: $0 <start|stop|status>"
  ;;
esac

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment