Can Gatekeeper be disabled via terminal in OS X 10.8 Mountain Lion?

How can you disable Gatekeeper via Terminal Commands in OS X 10.8 Mountain Lion? I am looking to replicate the Security & Privacy preference pane selection to allow applications downloaded from Anywhere to run:

enter image description here

By having a terminal command, I can write a script to do this task remotely rather than manually going to each Mac and changing these settings with a mouse and several clicks.


Solution 1:

The spctl tool should do the job.

sudo spctl --master-disable will turn off the Gatekeeper checks, and sudo spctl --master-enable will re-enable them (to the default setting of App Store and signed apps — it doesn't appear that setting it to App Store-only is possible).

Note that it will not throw any error if you run it without sudo permissions, but sudo is in fact required.

Solution 2:

sudo spctl --master-disable

See the man page for spctl

Solution 3:

Either of these also disables the Gatekeeper dialogs (after restarting), but they don't change the setting shown in System Preferences.

sudo defaults write /var/db/SystemPolicy-prefs.plist enabled -string no
defaults write com.apple.LaunchServices LSQuarantine -bool false

I don't know if spctl --master-disable is somehow different from disabling Gatekeeper from System Preferences, but spctl --status changes to assessments disabled after disabling Gatekeeper in System Preferences.