97

How can I restart the SSH service via the command line on Mac OSX Mountain Lion please?

Using ps aux | grep 'ssh', I was able to deduce that the process is most likely /usr/sbin/sshd.

From here I searched the sshd documentation for references to 'restart' but found none.

I don't know what my next step should be.

5 Answers5

127

There is no reason to 'unload' the sshd service, when instead you can just 'Stop' the sshd service. It will restart on it's own.

sudo launchctl stop com.openssh.sshd
glenschler
  • 1,524
68

See this answer to a similar question on ServerFault. The command should be the same in Mountain Lion.

You can stop the service using the 'unload' subcommand.

sudo launchctl unload  /System/Library/LaunchDaemons/ssh.plist

Update suggested by @MattClark: To restart the service use load after unload:

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

Note that existing SSH sessions will be terminated, so you need to run this independent of the current user session.

6

I couldn't confirm Ansgar's answer worked as there were no messages / obvious signs though I'm confident it did.

I also found killall sshd which kills and restarts sshd processes with the disadvantage that any connections are stopped.

4

You can restart sshd on macOS by running:

sudo launchctl kickstart -k system/com.openssh.sshd

This command slightly improves on launchctl stop because this does not rely on sshd automatically starting after you stop it. However, similar to launchctl stop this preserves existing ssh connections.

0

The question and answers seen here are too old (2012-2013). I tried them in Ventura (released in 2022) but none do work.

I conclude my testing on my two mac's (10.11.6/El Capitan@2016 and 13.1/Ventura@2022): both provide a GUI switch in Preferences|Common|Shares|RemoteLogin.

You just turn it off and on. sshd will be restarted and read the sshd_config file to reflect your changes.

(Although there are corresponding command-line commands for the two actions, I don't think you bother to know them.)