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?
Asked
Active
Viewed 2.7k times
7
Walrus the Cat
- 984
-
Related https://apple.stackexchange.com/questions/296647/how-do-i-create-a-static-permanent-ifconfig-alias – nohillside Jan 05 '18 at 18:50
2 Answers
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
-
1This is the correct answer... it's also permanent so it will survive after reboots. – Motsel Mar 01 '19 at 13:58
-
2Make 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:
- Create a script somewhere.
vi ~/path_to_script - add your route like so:
add route 192.168.1.0/24 10.0.0.2 sudo visudo- on the last line write
username ALL=(ALL) /Users/username/path_to_script(or something like that, you'll figure it out!) sudo chown root path_to_scriptsudo chmod +x path_to_scriptsudo chmod -w path_to_script- now create a
.plistfile, make sure you've got<ProgramArguments>with<string><sudo></string>and<string><path_to_script></string>. - save that thing to
~/Library/LaunchAgents launchctl load ~/Library/LaunchAgents/path_to_plist
Graham Miln
- 43,776
Walrus the Cat
- 984
-
2
-
1What's the purpose of running
sudofrom a LaunchAgent? This should never be necessary, just havelaunchdlaunch it with the correct user. This would also avoid the security risk introduced by openingsudofor 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
-
-
@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