Limit macOS Network Speed

Again, starting writing something … Let’s begin with some easy stuff.

Days ago, I would like to limit my macOS network speed. In Linux, we can use tc command to do that, but in macOS we will use dnctl+pfctl. For tc usage, checkout with this post

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#!/bin/bash

# Reset dummynet to default config
dnctl -f flush

# Compose an addendum to the default config: creates a new anchor
(cat /etc/pf.conf &&
echo 'dummynet-anchor "my_anchor"' &&
echo 'anchor "my_anchor"') | pfctl -q -f -

# Configure the new anchor
cat <<EOF | pfctl -q -a my_anchor -f -
no dummynet quick on lo0 all
dummynet out proto tcp from any to any pipe 1
EOF

# Create the dummynet queue, we can tune this number, say, make as 409600000 ~ 400MB
# which could means, unlimited for network speed
dnctl pipe 1 config bw 4096byte/s

# Activate PF
pfctl -E

# to check that dnctl is properly configured:
sudo dnctl list

Also remeber to reset this network speed control:

1
2
3
sudo dnctl -f flush
sudo pfctl -f /etc/pf.conf
sudo dnctl list

Here is a full list of function to be invoked in .bashrc / .bash_profile / .zshrc:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
speed_limit() {
local direction=${1-"up"}
# clear
sudo dnctl -f flush
sudo pfctl -f /etc/pf.conf
sudo pfctl -E

(cat /etc/pf.conf &&
echo 'dummynet-anchor "my_anchor"' &&
echo 'anchor "my_anchor"') | sudo pfctl -q -f -

cat <<EOF | sudo pfctl -q -a my_anchor -f -
no dummynet quick on lo0 all
dummynet out proto tcp from any to any pipe 1
EOF

if [[ "$direction" == "up" ]]; then
sudo dnctl -f flush
sudo pfctl -f /etc/pf.conf
sudo pfctl -E
sudo dnctl list
else
sudo dnctl pipe 1 config bw 4096byte/s
sudo pfctl -E
sudo dnctl list
fi
}

0%