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?
-
Related apple.stackexchange.com/questions/296647/… – nohillside♦ Jan 5 '18 at 18:50
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 ;)
-
1This is the correct answer... it's also permanent so it will survive after reboots. – Motsel Mar 1 at 13:58
-
1Make 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 at 15:14
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_script
sudo chmod +x path_to_script
sudo chmod -w path_to_script
- now create a
.plist
file, 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
-
2
-
What's the purpose of running
sudo
from a LaunchAgent? This should never be necessary, just havelaunchd
launch it with the correct user. This would also avoid the security risk introduced by openingsudo
for the script. – nohillside♦ Jan 5 '18 at 18:48 -
2@patix in addition to downvoting feel free to provide a helpful answer – Walrus the Cat Jan 5 '18 at 23:24
-
This method fails for example when your active network connection switches between wifi & ethernet. – Motsel Mar 1 at 14:01
-