MacOS – Disable Screen sharing via the command line in mountain lion

launchdmacosscreen-sharingterminal

I want to be able to enable and disable screen sharing from the command line rather than through System Preferences. This is on 10.8.3

To enable screen sharing, I find I only need to change a dictionary value in the following plist:

/var/db/launchd.db/com.apple.launchd/overrides.plist

To be precise, to enable screen sharing, I do the following:

sudo defaults write /var/db/launchd.db/com.apple.launchd/overrides.plist com.apple.screensharing -dict Disabled -bool false

And that changes:

sudo defaults read /var/db/launchd.db/com.apple.launchd/overrides.plist com.apple.screensharing
{
    Disabled = 1;
}

To:

sudo defaults read /var/db/launchd.db/com.apple.launchd/overrides.plist com.apple.screensharing
{
    Disabled = 0;
}

Fine. I see from this site that I need to then do this (not sure why):

sudo launchctl load /System/Library/LaunchDaemons/com.apple.screensharing.plist

Now if I want to disable screen sharing I try the reverse:

sudo defaults write /var/db/launchd.db/com.apple.launchd/overrides.plist com.apple.screensharing -dict Disabled -bool true

I get:

sudo defaults read /var/db/launchd.db/com.apple.launchd/overrides.plist com.apple.screensharing
{
    Disabled = 1;
}

Which is good, but then I have no successful way to get launchctl, or whatever (not really sure) to re-read this plist.

I try

sudo launchctl load /System/Library/LaunchDaemons/com.apple.screensharing.plist

and I get:

nothing found to load

And indeed, despite the dictionary value for this screen sharing being properly set to indicate screen sharing should be off, well, I still can screen share in just fine.

So what is the proper way to disable screen sharing from the command line on 10.8.3?

Best Answer

To start screensharing:

sudo launchctl load -w /System/Library/LaunchDaemons/com.apple.screensharing.plist

To stop:

sudo launchctl unload -w /System/Library/LaunchDaemons/com.apple.screensharing.plist

The -w flag modifies the Disabled key as appropriate. It's best to let launchctl handle this, as the location where the config files are stored has changed a bit between OS versions.