● Problem
Therefore, I need to add a static route on my MacBook Pro.
● Solution
As you see below, I received the 192.168.2.0/23 route from the VPN, and it routes through the tunnel interface, utun3. First, disconnect the VPN and add a static route as described below.
To view the routing table:
You can use the following command "netstat -rn" and use 'grep' to filter with a specific network on the Terminal.macOS:/Users/analysisman% netstat -rn | grep 192.168.2.
192.168.2/23 1.1.1.1 UGSc utun3
▶ Method 1. Add a static route temporarily
To add a static route:
macOS:/Users/analysisman% sudo route -n add -net 192.168.2.0/24 192.168.1.1
add net 192.168.2.0: gateway 192.168.1.1
To verify the route you added:
macOS:/Users/analysisman% netstat -rn | grep 192.168.2.
192.168.2 192.168.1.1 UGSc en10
192.168.2/23 1.1.1.1 UGSc utun3
Now, 192.168.2.0/24 routes through my Ethernet interface, en10.
To delete a static route:
macOS:/Users/analysisman% sudo route -n delete 192.168.2.0/24
Password:
delete net 192.168.2.0
▶ Method 2. Add a static route persistently
To verify the route your interface:
macOS:/Users/analysisman% ifconfig -a
…snipped…
en10: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 1500
options=6407<RXCSUM,TXCSUM,VLAN_MTU,CHANNEL_IO,PARTIAL_CSUM,ZEROINVERT_CSUM>
ether 00:24:9b:33:44:55
inet6 fe80::3f:e0d:4455:1ebe%en10 prefixlen 64 secured scopeid 0xb
inet 192.168.1.103 netmask 0xffffff00 broadcast 192.168.1.255
nd6 options=201<PERFORMNUD,DAD>
media: autoselect (1000baseT <full-duplex>)
To list devices (network adapters):
macOS:/Users/analysisman% networksetup -listallnetworkservices
An asterisk (*) denotes that a network service is disabled.
FT232R USB UART
USB 10/100/1000 LAN 2
USB 10/100/1000 LAN
USB 10/100/1000 LAN 3
USB 10/100/1000 LAN 4
USB 10/100/1000 LAN 5
Belkin USB-C LAN
Wi-Fi
iPhone USB 2
Bluetooth PAN
Thunderbolt Bridge
GlobalProtectDo
GlobalProtectDo 2
Or
To list devices with the interface number:
I prefer this command because it also shows the ethernet number (e.g. en10).
macOS:/Users/analysisman% networksetup -listnetworkserviceorder
An asterisk (*) denotes that a network service is disabled.
(1) FT232R USB UART
(Hardware Port: FT232R USB UART, Device: usbserial-AI06J8P5)
(2) USB 10/100/1000 LAN 2
(Hardware Port: USB 10/100/1000 LAN, Device: en8)
(3) USB 10/100/1000 LAN
(Hardware Port: USB 10/100/1000 LAN, Device: en10)
To add a static route permanently:
Usage:
networksetup -setadditionalroutes <networkservice> [ <dest> <mask> <gateway> ]*
macOS:/Users/analysisman% sudo networksetup -setadditionalroutes "USB 10/100/1000 LAN" 192.168.2.0 255.255.255.0 192.168.1.1
To verify the route you added:
macOS:/Users/analysisman% netstat -rn | grep 192.168.2.
192.168.2 192.168.1.1 UGSc en10
To delete this permanent route:
Use sudo networksetup -setadditionalroutes interface-name without the address, netmask, and gateway.
macOS:/Users/analysisman% sudo networksetup -setadditionalroutes "USB 10/100/1000 LAN"
To see all commands:
networksetup -help
Or
networksetup -printcommands
6 comments:
Thanks!
Hi AnalysisMan
Would you be able to help me with where/which file stores the route information that is displayed when we execute netstat -ln
Regards
Vinay Chandran
In Macbook that is
I refer to this page every so often, I appreciate you keeping this up. Very helpful!
Thanks for this post. It was very useful. Just my additional 2 cents -
We can also get the configured permanent static routes using: networksetup -getadditionalroutes "USB 10/100/1000 LAN" .
Also, if we want to add multiple routes permanently then we can add additional tuples on the same command. Eg., sudo networksetup -setadditionalroutes "Wi-Fi" 192.168.230.0 255.255.255.0 192.168.20.82 192.168.68.0 255.255.255.0 192.168.20.82
Life saver hack! Thanks a lot!
Post a Comment