Start/Stop Internet Sharing via Terminal in Yosemite without AppleScript

Okay, I see this question has been asked a few times before (here, here, here and here), but none of those really answer the question for me.

I don't want to use the AppleScript suggestions provided to enable/disable Internet Sharing since future updates to the System Preferences GUI may break the AppleScript, as was the case in the linked posts above, and the solution seems a little clumsy, to be honest.

On Yosemite, sudo launchctl load -w /System/Library/LaunchDaemons/com.apple.InternetSharing.plist no longer works as there's no longer a .plist called InternetSharing.

I tried sudo defaults write /Library/Preferences/SystemConfiguration/com.apple.nat NAT -dict-add Enabled -int 1 which I saw somewhere else, but that only toggles the actual checkbox in System Preferences and doesn't actually enable IS.

So ultimately, my question is this. What processes do I have to call from the command line to enable Internet Sharing, or conversely, what processes do I have to kill to disable Internet Sharing?


I stumbled across a seemingly stupidly easy way to enable/disable Internet Sharing the other day. There's just a bit of prep work to be done first.

  1. Open System Preferences > Network
  2. Highlight the interface you want to share your connection FROM and click the gear below the interface list
  3. Select 'Duplicate Service...'
  4. Give the duplicated service a name and save it
  5. Set the service to 'DHCP with manual address' (because some DHCP servers will not assign an additional address to a MAC) and input a valid IP

That's the prep work done. Now you just need to change your IS settings to share from the new service instead of the existing one. Then, you can use networksetup -setnetworkserviceenabled <networkservice> <on off> to toggle the network service you just created on or off. Doing so disables IS as there is no service to receive internet access from, and you still retain access on your machine because there is still an active interface connected to your network!


I did the following:

  • Duplicate the "Automatic" location. I gave the duplicated the name "Shared".
  • On the "Shared" location, rename the Ethernet interface to "Shared Ethernet".
  • Configure the shared internet to use the "Shared Ethernet" interface.

With this setup, when switch from the "Automatic" to the "Shared" location, the "Shared Ethernet" interface becomes available/unavailable and therefore the Internet Sharing can be enabled/disabled.

From here to using command line, is just a matter of calling

networksetup -switchtolocation "Shared"

or

networksetup -switchtolocation "Automatic"


First, bring down any active internet interfaces using the terminal command:

ifconfig [interface*] down

Then a launch daemon found here:

/System/Library/LaunchDaemons/com.apple.NetworkSharing.plist

needs to be toggled with the launchctl command.

then bring up the interfaces with the command

ifconfig [interface*] up

*Note: en0 is the wired ethernet interface, while en1 is the wifi interface. Executing the ifconfig command by itself lists all interfaces, active or inactive.