91

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
  • 1,081
  • 1
  • 9
  • 15
  • 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](http://comments.gmane.org/gmane.comp.macosx.admin/20553)). – Gordon Davisson Sep 22 '12 at 01:47
  • 1
    This 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 Sep 22 '12 at 08:10

5 Answers5

123

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,405
  • 2
  • 10
  • 7
  • 2
    on moutain lion: `launchctl stop error: No such process` – RickyA Oct 10 '13 at 08:45
  • 1
    @RickyA: Worked fine on OSX Mavericks – neu242 Oct 31 '13 at 09:19
  • Doesn't work here (`launchctl stop error: No such process`) – Nicolas Miari Apr 25 '14 at 04:26
  • 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](https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man1/launchctl.1.html) – glenschler Apr 25 '14 at 13:06
  • 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 Mar 07 '15 at 20:01
  • 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 Mar 10 '15 at 18:14
  • 2
    This should be accepted answer. – o01 Sep 16 '20 at 05:54
  • On Mac verson `13` after stopping, it does not (re)start automatic it seems because `ps -ef` does not show `sshd`. But `launchctl list` as put by @glenschler shows `com.openssh.sshd`. So it must work. – Timo Dec 14 '22 at 14:13
61

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
  • 5,400
  • 2
  • 21
  • 23
  • This one gets me `launchctl: Error unloading: com.openssh.sshd` – Nicolas Miari Apr 25 '14 at 04:27
  • 4
    You are correct, I did misread this, that was my fault. However, this also did not answer the question as it was asked. – Matt Clark Jun 21 '16 at 16:34
  • 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 Jun 24 '19 at 14:20
5

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
  • 1,081
  • 1
  • 9
  • 15
1

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.)

Charles Jie
  • 109
  • 2
  • In my Ventura this setting is under System Settings > General > Sharing. It was already off, though, and turning it on didn't help. – Noumenon Apr 05 '23 at 16:02