6

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?

  • Have you tried to bring down the ethernet interface using ifconfig en0 down then back up with ifconfig en0 up. This example uses en0, but your interface name will vary depending on which one is in use. That might force the change. – IconDaemon Mar 6 '15 at 17:20
  • Yup, tried that. Although I only tried restarting the wi-fi (which I'm sharing to) and not the ethernet (which I'm sharing from), although I doubt which interface I restarted would affect the outcome. – Stuart H Mar 6 '15 at 17:28
  • I'd try the ethernet interface, too. – IconDaemon Mar 6 '15 at 17:39
  • There is a /System/Library/LaunchDaemons/com.apple.NetworkSharing.plist Have you tried toggling this off/on? – IconDaemon Mar 6 '15 at 17:47
  • You, sir, are a wonderful being. A combination of bringing down the two interfaces and toggling the daemon between sorted the trick. I'll leave it to you to post the answer so I can give you rep. :) – Stuart H Mar 6 '15 at 19:20
3

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!

  • 1
    Can't thank you enough for this answer, been trying to find a scripted way to do this for ages; now I can finally have it switch off at night so my computer can sleep, and switch on again when I wake it. – Haravikk Nov 7 '15 at 14:09
1

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.

  • Perhaps I spoke too soon. While this works fine for disabling Internet Sharing, it doesn't seem to work too well to re-enable it again. :( – Stuart H Mar 6 '15 at 20:42
  • Have you tried toggling the daemon after you bring up the interfaces? – IconDaemon Mar 6 '15 at 22:17
  • I've tried toggling the daemon then bringing up the interfaces, the same with a delay to allow for initialisation, toggling after bringing up the interfaces, toggling the sharing interface after toggling the daemon, and even using the start option of launchctl, all to no avail. – Stuart H Mar 7 '15 at 11:52
1

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"

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .