How to add a static route on macOS
Static routes can be very helpful, especially when needing to bypass a VPN. Luckily adding a static route on macOS is fairly simple using the route command. We'll go over how to add a static route temporarily and persistently.
Temporary Static Route
This static route will be removed upon rebooting your mac. If you want a permanent static route, please see Persistent Static Route.
Replace $destination_ip with the IP address of the destination service. Replace $router_ip with the IP address of your local router.
sudo route -nv add $destination_ip $router_ip
You can confirm the static route is in place with netstat:
netstat -rn |grep $destination_ip
For my example, I'm routing google.com directly to my local router:
142.250.190.14 192.168.2.1 UGHS en5Removing the route is as simple as:
sudo route -nv delete $destination_ip
Persistent Static Route
Adding a presistent static route will stay in place after reboots. It's only recommended to use this after testing with a temporary route. This setup is a bit more complex.
- Verify which interface is currently active. As you'll want to attach the route to the correct device.
ifconfig -a |grep -B8 'status: active'en5: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 1500
On my mac,
options=404<VLAN_MTU,CHANNEL_IO>
[..snipped..]
media: autoselect (1000baseT <full-duplex>)
status: activeen5is the active interface. - Determine which network adapter is used for the active interface.
networksetup -listnetworkserviceorder |grep -B1 en5(1) AX88179A
On my mac,
(Hardware Port: AX88179A, Device: en5)AX88179Ais the network adapter in use. - Add the static route persistently:
sudo networksetup -setadditionalroutes "AX88179A" $destination_ip 255.255.255.0 $router_ip - You can confirm the route is in place with netstat, via grep only search for the first 3 octets of the destation IP.
- To remove the perstitent static route, run the same command to add it, with out the IP address information:
sudo networksetup -setadditionalroutes "AX88179A"