I'm looking for a way to programatically enable Screen Sharing and Remote Login on macOS to help with automated provisioning of new machines.
I'm running the following code in a .sh script on the new machine:
# Enable screensharing for remote management
sudo defaults write /var/db/launchd.db/com.apple.launchd/overrides.plist com.apple.screensharing -dict Disabled -bool false
sudo launchctl load -w /System/Library/LaunchDaemons/com.apple.screensharing.plist
# Enable remote login for remote management
sudo launchctl load -w /System/Library/LaunchDaemons/ssh.plist
Once run, the Screen Sharing and Remote Login Services are checked as On in the System Preferences > Sharing menu
However when I initiate a screen sharing session using Connect to Server (Cmd + K in Finder) and enter an administrator password, the screen sharing connects but the remote session only sees a black screen.
The screen is displayed through the Connect to Server screen sharing session when I go to the physical machine and uncheck/check "Screen Sharing". However I would like to avoid any manual set up steps if possible.
Has anyone had luck setting up Screen Sharing and Remote Login programatically using a different command/method?
Once I can ssh, I use this snippet:
I can't recall where I learned it.
Edit July 26, 2023 I came across AdminRemoteControl from the outset project.