Error `could not load host key` when trying to recreate SSH host keys

Emil Sommer

I am trying to recreate the ssh-server host keys.

I have at least two ways to do this:

  • With dpkg-reconfigure

    dpkg-reconfigure openssh-server
    

    This works fine, but I cannot give the key length then. I want for example 4096 for the RSA key.

  • Manually with ssh-keygen

    sudo ssh-keygen -f /etc/ssh/ssh_host_rsa_key -N 'myverylongpasswordhere' -b 4096 -t rsa
    

    This recreates me the keys, but after restarting the server, I receive the following error message:

    could not load host key: /etc/ssh/ssh_host_rsa_key
    

    so I checked the sshd_config file whats in there:

    HostKey /etc/ssh/ssh_host_rsa_key
    

    matches perfectly. So, I checked the owner and rights to all my keys

    -rw------- 1 root root 3326 Mär 24 08:57 ssh_host_rsa_key
    

    When I remove all keys and recreate them with dpkg-reconfigure openssh-server, the keys are smaller and having the same file-rights like above.

Question: How can I use dpkg-reconfigure with keylengh 4096 for RSA?

LiveWireBT
sudo ssh-keygen -f /etc/ssh/ssh_host_rsa_key -N 'myverylongpasswordhere' -b 4096 -t rsa

recreates me the keys. but, after restarting the server, i recieve

could not load host key: /etc/ssh/ssh_host_rsa_key

You create a hostkey with a password. Is there any customization to unlock that hostkey? If not, then I think that is what is to be expected: the script that manages the service starts up, tries to load the hostkey, and fails. As far as I know you shouldn't create hostkeys protected with passwords.

If you are interested in hardening your SSH server then I recommend reading https://stribika.github.io/2015/01/04/secure-secure-shell.html the command used to create the hostkey in that document is:

ssh-keygen -t rsa -b 4096 -f ssh_host_rsa_key

But you should read the entire document before making any changes.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Git error: "Host Key Verification Failed" when connecting to remote repository

Unable to point to ssh keys in ~/.ssh on Jenkins host

Multiple SSH Keys for same user on same host

Paramiko cannot open an ssh connection even with load_system_host_keys + WarningPolicy

Type Error when trying to socket due to python reading host as a tuple

"SocketException: Could not resolve host" when connecting to SFTP server with SSH.NET

Failed to connect to the host via ssh: Host key verification failed

Azure Functions: host error has occurred during startup operation Could not load file

SSH host key keeps changing

How to roll over ssh host keys?

SSH config - same host but different keys and usernames

Why does SSH hosts keys differ when connecting if the host_keys are the same?

SSH connection problem with "Host key verification failed..." error

SSH 'Host key verification failed' error

Is there any way to automatically accept SSH host keys—even when keys are changed—in PuTTY?

SSH Connection Error: No route to host

Could not load host key: /etc/ssh_host_*_key

SSH host: Could not resolve hostname

Host key verification failed with SSH

Could not load host key: /etc/ssh/ssh_host_ed25519_key in /var/log/auth.log

SSH fails with host key error when sending a command, but works without one

Windows Scipt Host Error when trying to run vbs script

When trying to convert an SSH file, I got this error: Couldn't load private key (SSH-2 public key (OpenSSH format))

Installation error when trying to deploy TeamViewer host via Intune

Host Key verification failed when ssh from windows command prompt?

crispy-forms gives error when trying to run on local host

Existing host keys found error when ssh in gcp vm instance

"Could not resolve host" when trying to connect to Stripe's API

When are host keys for the server generated