How do I enable SSH server on startup through Terminal?

Curious as to how I can enable the sshd to start on startup solely using terminal. The GUI is great and everything but it doesn't go well with my bash scripts.


Solution 1:

The short answer on how to enable sshd from the terminal is doing this:

sudo systemsetup -setremotelogin on

The long answer on how to execute daemons or scripts on startup is a bit longer. There are two ways (that I know of) to do this:

  • The first one is through Startup Items, which are executed at the final phase of bootup, these are deprecated and shouldn't be used unless you need compatibility with Mac OS X 10.3 or earlier. You can take a look at how to do it this way in here.

  • The second one is launchd, a replacement for init and rc (among others). This is the recommended way and it's a bit trickier than just putting a script on a folder like in Linux. There are several ways to launch daemons, the preferred way it to launch them on demand (so they will be ready to launch but you won't see them in a ps for example, unless you are using them). The explanation is a bit lengthly and the list of options is long, you can take a look at the documentation here. You can take a look in /Library/LaunchAgents for examples, if you have Chrome installed, there is a file in there for the Google Software Updater called com.google.keystone.agent.plist, that runs at load time, you can start there.

Solution 2:

I was going to make this a comment, but it would be too lengthy. It goes a bit into more detail about my particular issue and a different way to enable the SSH server from the terminal, as well as to make sure it persists across reboots.

I came across this question because my Remote Login (sshd) was getting disabled after each reboot. None of the answers satisfied my question (ie. I did not have FileVault enabled, etc., etc.).

Just check /System/Library/LaunchDaemons/ssh.plist and make sure <key>Disabled</key> is not set to <true/>. Additionally, to make sure it starts on boot every time, I made the partial entry look like this:

....
<dict>
    <key>Disabled</key>
    <false/>
    <key>RunAtLoad</key>
    <true/>
    <key>Label</key>
    <string>com.openssh.sshd</string>
....

Then just run the following to make sure the new settings stick:

sudo launchctl load -w /System/Library/LaunchDaemons/ssh.plist
sudo reboot