Facebooktwitterredditpinterestlinkedintumblr

SSH is a critical tool that I always use as a developer and system administrator. It allows me to securely access and manage servers remotely, which is incredibly helpful for troubleshooting, debugging, and deploying new code.

However, sometimes things can go wrong. One of the most frustrating errors I’ve encountered is the “sshd no hostkeys available” error. When this happens, I can’t connect to the SSH server, which can be a major problem.

That’s why knowing how to fix this issue quickly and efficiently is important. In this article, I’ll explain what causes this error and walk you through different solutions to get you back up and running quickly.

Understanding the Causes of sshd no hostkeys available

Before we can fix the “sshd no hostkeys available” error, it’s important to understand why it happens in the first place. SSH uses host keys to verify the identity of the server you’re connecting to.

These keys are generated when SSH is installed on a server and used every time a client connects to that server. When the client connects, SSH checks the host key against a known_hosts file to ensure it connects to the right server.

If the host key isn’t available or can’t be verified, you’ll get the “sshd no hostkeys available” error.

There are a few different reasons why this error might occur. Sometimes, the host key hasn’t been generated yet, meaning SSH has nothing to check against.

Other times, the host key may have been deleted or lost, which can also cause the error.

Finally, file permissions can also be a factor – if the authorized_keys file or the .ssh directory don’t have the right permissions, SSH won’t be able to access the host key properly.

How to Fix sshd no hostkeys available

Solution 1: Generate New Host Keys

  • Log in to your server using a non-root user with sudo privileges.
  • Open a terminal window and run the following command to generate a new RSA host key:
sudo ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_key
  • You’ll be prompted to enter a passphrase. You can leave it blank if you prefer, but adding one will provide an extra layer of security.
  • The key will be generated and saved to the specified location.
  • Next, run the following command to generate a new DSA host key:
sudo ssh-keygen -t dsa -f /etc/ssh/ssh_host_dsa_key
  • You’ll be prompted to enter a passphrase. You can use the same one you used for the previous key or create a new one.
  • Run the following command to generate a new ECDSA host key:
sudo ssh-keygen -t ecdsa -f /etc/ssh/ssh_host_ecdsa_key
  • You’ll be prompted to enter a passphrase. You can use the same one you used for the previous key or create a new one.
  • Finally, run the following command to generate a new ED25519 host key:
sudo ssh-keygen -t ed25519 -f /etc/ssh/ssh_host_ed25519_key
  • Now that you’ve generated all four keys, restart the SSH service on your server by running
sudo service ssh restart

Congratulations! You’ve successfully generated new host keys, which should fix the “sshd no hostkeys available” error.

Solution 2: Copy the Host Keys from Another Server

  • Log in to the server that has working host keys.
  • Open a terminal window and navigate to the stored host keys’ directory. On Ubuntu-based systems, this is usually /etc/ssh.
cd /etc/ssh
  • Run the following command to display the public key:
sudo cat ssh_host_rsa_key.pub
  • Highlight and copy the entire public key that’s displayed in the terminal.
  • Now, log in to the server experiencing the “sshd no hostkeys available” error.
  • Open a terminal window and navigate to the directory where the host keys are stored – again, this is usually /etc/ssh.
cd /etc/ssh
  • Use your preferred text editor to open the ssh_host_rsa_key.pub file.
sudo nano ssh_host_rsa_key.pub
  • Paste the public key you copied from the other server into this file.
  • Save and close the file.
  • Repeat steps above with the ssh_host_dsa_key.pub, ssh_host_ecdsa_key.pub, and ssh_host_ed25519_key.pub files.
  • Once you’ve added all four public keys to the appropriate files on the new server, restart the SSH service by running
sudo service ssh restart

You’re all set! The new server should now have working host keys and no longer experience the “sshd no hostkeys available” error.

Related: Fix: SSH could not resolve hostname

Solution 3: Checking File Permissions

  • Log in to your server using a non-root user with sudo privileges.
  • Open a terminal window and navigate to the .ssh directory in your home folder by running
cd ~/.ssh
  • Check the file permissions of the authorized_keys file by running
ls -la authorized_keys
  • The output should look something like this:
-rw------- 1 user user 12345 Sep 9 10:20 authorized_keys

If the file permissions are different, this may be causing the “sshd no hostkeys available” error.

  • To fix this, change the file permissions by running
chmod 600 authorized_keys
  • Next, check the file permissions of the .ssh directory itself by running
ls -la ~/.ssh
  • The output should look something like this
drwx------ 2 user user 4096 Sep 9 10:20 .ssh/

If the directory permissions differ, this may also be causing the error.

  • To fix this, change the directory permissions by running
chmod 700 ~/.ssh
  • Finally, restart the SSH service on your server by running
sudo service ssh restart

You’ve successfully checked and adjusted the file permissions for the .ssh directory and authorized_keys file, which should fix the “sshd no hostkeys available” error.

Conclusion

The “sshd no hostkeys available” error can be a frustrating issue, but several different solutions can help you get back to work quickly. You can easily troubleshoot and fix the issue by generating new host keys, copying host keys from another server, or checking and adjusting file permissions.

It’s always a good idea to keep your SSH configuration up-to-date and ensure that your host keys are properly generated and stored. This can help prevent issues like this from occurring in the first place.

If you have any questions or need further assistance, write in the comments below.

Tim Miller

Tim has always been obsessed with computers his whole life. After working for 25 years in the computer and electronics field, he now enjoys writing about computers to help others. Most of his time is spent in front of his computer or other technology to continue to learn more. He likes to try new things and keep up with the latest industry trends so he can share them with others.

Leave a Comment