7

I'm adding a route to all 192.168.1.x ips through a gateway like so: sudo route add 192.168.1.0/24 10.0.0.2 . How do I add this route permanently in High Sierra?

2 Answers2

14

I suggest to use networksetup which works persistent and also in separate network locations.

First, open your terminal of choice i.e. iTerm2.app or Terminal.app

  • list your network locations:

networksetup -listlocations

  • choose your desired network location:

sudo networksetup -switchtolocation <locationofchoice>

  • list "devices" called networkservices

networksetup -listallnetworkservices

  • list persistent routes on "device" of choice i.e. "Ethernet"

networksetup -getadditionalroutes Ethernet

  • add your route to "Ethernet"

sudo networksetup -setadditionalroutes Ethernet 192.168.1.0 255.255.255.0 10.0.0.2

  • list persistent routes on "Ethernet" again to check

networksetup -getadditionalroutes Ethernet

To see all commands:

networksetup -help or
networksetup -printcommands

Hope that helps ;)

Cellcore
  • 255
  • 1
    This is the correct answer... it's also permanent so it will survive after reboots. – Motsel Mar 01 '19 at 13:58
  • 2
    Make note that if you want to add more than one route you have to do it in a line. For example: sudo networksetup -setadditionalroutes Ethernet 192.168.1.0 255.255.255.0 10.0.0.2 10.2.0.0/16 255.255.255.0 10.0.0.2. And if you run a command without routes than you will delete existed additional routes(sudo networksetup -setadditionalroutes Ethernet) – Евгений Масленков May 21 '19 at 15:14
0

Here is how I added a permanent static route:

  1. Create a script somewhere. vi ~/path_to_script
  2. add your route like so: add route 192.168.1.0/24 10.0.0.2
  3. sudo visudo
  4. on the last line write username ALL=(ALL) /Users/username/path_to_script (or something like that, you'll figure it out!)
  5. sudo chown root path_to_script
  6. sudo chmod +x path_to_script
  7. sudo chmod -w path_to_script
  8. now create a .plist file, make sure you've got <ProgramArguments> with <string><sudo></string>and <string><path_to_script></string> .
  9. save that thing to ~/Library/LaunchAgents
  10. launchctl load ~/Library/LaunchAgents/path_to_plist
Graham Miln
  • 43,776
  • 2
    do you have an example for this script and for the .plist file? – Wim Jan 05 '18 at 13:20
  • 1
    What's the purpose of running sudo from a LaunchAgent? This should never be necessary, just have launchd launch it with the correct user. This would also avoid the security risk introduced by opening sudo for the script. – nohillside Jan 05 '18 at 18:48
  • 2
    @patix in addition to downvoting feel free to provide a helpful answer – Walrus the Cat Jan 05 '18 at 23:24
  • This method fails for example when your active network connection switches between wifi & ethernet. – Motsel Mar 01 '19 at 14:01
  • @Motsel good looking out. how would you do it? – Walrus the Cat Mar 01 '19 at 18:32
  • @WalrustheCat use networksetup -setadditionalroutes (for both Wifi and Ethernet devices), as answered by @Cellcore above. That way it always works, even after rebooting or switching between wifi & ethernet. – Motsel Mar 01 '19 at 22:07