Sometimes you Mac is connected with one Ethernet interface and one WiFi interface, you want to force some route to specific gateways with the macOS Command Line Interface.
Here is our setup:
192.168.1.0/24
192.168.1.252
172.16.1.10/24
You want add a static route temporarily for a specific ip range with the route
command:
moore@my-mac ~ % sudo route -n add -net 172.16.1.10/24 192.168.1.252
You can verify the route you have added:
moore@my-mac ~ % netstat -rn | grep 172.16.1.
To add a static route permanently you will have to use networksetup
.
First list your devices:
moore@my-mac ~ % networksetup -listnetworkserviceorder
An asterisk (*) denotes that a network service is disabled.
(1) Belkin USB-C LAN 2
(Hardware Port: Belkin USB-C LAN, Device: en11)
(2) Belkin USB-C LAN
(Hardware Port: Belkin USB-C LAN, Device: en5)
(3) Linux for Tegra
(Hardware Port: Linux for Tegra, Device: en10)
(4) USB Controls
(Hardware Port: USB Controls, Device: usbmodem904NTVJD14201)
(5) Somfy Set&Go io
(Hardware Port: Somfy Set&Go io, Device: usbmodem6104402002)
(6) HERO10 Black
(Hardware Port: HERO10 Black, Device: en15)
(7) Wi-Fi
(Hardware Port: Wi-Fi, Device: en0)
(8) iPhone USB
(Hardware Port: iPhone USB, Device: en16)
(9) Bluetooth PAN
(Hardware Port: Bluetooth PAN, Device: en7)
(10) Thunderbolt Bridge
(Hardware Port: Thunderbolt Bridge, Device: bridge0)
Add the route with the networksetup
command:
moore@my-mac ~ % sudo networksetup -setadditionalroutes "Belkin USB-C LAN 2" 172.16.1.10 255.255.255.0 192.168.1.252
Verify the route you added:
moore@my-mac ~ % netstat -rn | grep 172.16.1.