I'm working with macOS Sierra and I have accidentally deleted
How can I recover this folder so I can use SSH?
The last solution is to reinstall the OS, but I want avoid this.
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) ssh_host_ecdsa_key.pub
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
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.
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 ssh_host_ed25519_key
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.