I'm working with macOS Sierra and I have accidentally deleted /private/etc/ssh.

How can I recover this folder so I can use SSH?

The last solution is to reinstall the OS, but I want avoid this.

  • 2
    Time Machine... – Tetsujin Feb 8 '17 at 9:00
  • 1
    You may be able to recreate the keys with ssh-keygen. See discussions.apple.com/thread/461831 – lhf Feb 8 '17 at 9:16
  • /etc is linked to /private/etc, so there is only one folder /private/etc/ssh. @lhf You should leave that as an answer? – grg Feb 8 '17 at 14:02

After deleting your ssh folder and no backup you can reinstall some content using the macOS installer and recreate the keys yourself:

The default content of /private/etc/ssh in El Capitan or Sierra are these files:

moduli                          ssh_host_ed25519_key
ssh_config                      ssh_host_ed25519_key.pub
(ssh_config~orig)               ssh_host_rsa_key
ssh_host_dsa_key                ssh_host_rsa_key.pub
ssh_host_dsa_key.pub            sshd_config
ssh_host_ecdsa_key              (sshd_config~previous)            

Three of the files are part of the macOS installer and can be extracted and installed from the Sierra OS X Install ESD with Pacifist: moduli, ssh_config and sshd_config.

All other files can be created by using ssh-keygen or copying files:

sudo cp /etc/ssh/ssh_config /etc/ssh/ssh_config~orig #just a backup
sudo cp /etc/ssh/sshd_config /etc/ssh/sshd_config~orig #just a backup

Key creation:

sudo ssh-keygen -t rsa -b 2048 -f /etc/ssh/ssh_host_rsa_key
sudo ssh-keygen -t dsa -b 1024 -f /etc/ssh/ssh_host_dsa_key
sudo ssh-keygen -t ecdsa -b 521 -f /etc/ssh/ssh_host_ecdsa_key
sudo ssh-keygen -t ed25519 -f /etc/ssh/ssh_host_ed25519_key

This may invalidate some services relying on the old keys!


The contents of /private/etc/ssh/ssh_config in Sierra is as follows:

Host *
        SendEnv LANG LC_*

The contents of /private/etc/ssh/sshd_config in Sierra is as follows:

AuthorizedKeysFile  .ssh/authorized_keys
UsePAM yes
AcceptEnv LANG LC_*
Subsystem   sftp    /usr/libexec/sftp-server

You can use ssh-keygen to create keys as per lhf's answer.

  • Yeah in Mavericks the ssh folder doesn't exist at all and all ssh related files (keys/conf/moduli) reside directly in /etc – klanomath Feb 8 '17 at 16:26
  • @klanomath Thanks for that information! Perhaps that's what I was thinking of then, oops! – grg Feb 8 '17 at 16:27

In my El Captain machine, /etc/ssh contains these files (but some may be leftover from previous OS versions):

moduli                          ssh_host_ed25519_key.pub
ssh_config                      ssh_host_key
ssh_config~orig                 ssh_host_key.pub
ssh_host_dsa_key                ssh_host_rsa_key
ssh_host_dsa_key.pub            ssh_host_rsa_key.pub
ssh_host_ecdsa_key              sshd_config
ssh_host_ecdsa_key.pub          sshd_config~previous

You may be able to recreate the keys with ssh-keygen. See https://discussions.apple.com/thread/461831 .

I'm not sure what to do about the config files. Perhaps someone with a pristine Sierra installation can share those with you.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .