Command Line SSH restart Mac OSX Mountain Lion

64

20

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.

James Webster

Posted 2012-09-21T18:09:50.763

Reputation: 801

4

What are you actually trying to accomplish? If it's something like reloading the sshd config, you don't need to -- launchd starts a new sshd process on demand when an incoming connection is received, which means that the config is automatically reread for each new connection. OTOH, if you're trying to change the listener settings, those are controlled by launchd not the sshd config at all (see here).

– Gordon Davisson – 2012-09-22T01:47:20.600

1This was just one test of debugging a password request from supposedly passwordless ssh using public keys. Turns out the thing I has missed was to turn off StrictMode – James Webster – 2012-09-22T08:10:20.077

Answers

44

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.

Ansgar Wiechers

Posted 2012-09-21T18:09:50.763

Reputation: 4 860

1@MattClark Then I suggest you stay away from production machines until you improve your reading comprehension. From my answer: *"You can STOP the service ..."* (emphasis mine). – Ansgar Wiechers – 2016-06-21T16:27:24.430

3You are correct, I did misread this, that was my fault. However, this also did not answer the question as it was asked. – Matt Clark – 2016-06-21T16:34:48.877

You can run this in one go with C-x C-e (C stands for Control). Paste it into the terminal, then C-x C-c to execute the emacs kill command, which will prompt you to save. I did this remotely and it did not kill my connection... given your config is correct. – Ray Foss – 2019-06-24T14:20:21.567

This one gets me launchctl: Error unloading: com.openssh.sshd – Nicolas Miari – 2014-04-25T04:27:12.447

96

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

Posted 2012-09-21T18:09:50.763

Reputation: 1 076

This no longer works in OS X Yosemite, attempting to stop sshd (as root) results in exit status of 3, and the service continues running. – RCross – 2015-03-07T20:01:18.590

Yes it works for me on Yosemite. Have you enabled 'Remote Login' in Preferences->Sharing? You need to confirm it is running with launchctl: sudo launchctl list | grep -i sshd. If not, perhaps you started it some other way? – glenschler – 2015-03-10T18:14:22.307

2on moutain lion: launchctl stop error: No such process – RickyA – 2013-10-10T08:45:09.590

1@RickyA: Worked fine on OSX Mavericks – neu242 – 2013-10-31T09:19:15.833

Doesn't work here (launchctl stop error: No such process) – Nicolas Miari – 2014-04-25T04:26:46.353

1

You need to sudo since that is how it is launched. Compare the running daemons/agents launchctl list | grep -i openssh vs sudo launchctl list | grep -i openssh. For reference read the launchctl link

– glenschler – 2014-04-25T13:06:53.810

4

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.

James Webster

Posted 2012-09-21T18:09:50.763

Reputation: 801