Please note that we can only store SSH keys on cloud servers when the server is created. For more information, see the FAQ entry "I have added my SSH key to the Hetzner Console, why can I not access my existing server via my SSH key?".
If you can no longer access your server because password authentication is blocked and you lost your SSH key, you can regain access by adding a new SSH key on the server via the rescue system.
To add a new SSH key via the rescue system, do the following:
-
Enable the rescue system
As explained in this getting started:
- Select the server
- Navigate to "Rescue" and click on "Enable rescue & power cycle"
- After the reboot, connect to the server as root user.
-
Create a chroot environment
Run
lsblkand identify the name of the root partition (e.g.sda1).Mount the root partition into
/mntand create the chroot environment:Replace
sda1with the name of your root partition.mount /dev/sda1 /mnt chroot-prepare /mnt chroot /mntYou can now navigate through the file structure of your server's primary disc.
-
Edit the SSH keys for the root user
SSH keys for the root user are stored in
/root/.ssh/authorized_keys:nano /root/.ssh/authorized_keysAdd your new public SSH key on a new line. Save the changes using
CTRL+Xand accept these withENTER.You can now run
exitto leave the chroot environment.
-
Restart the server
Restart your server to leave the rescue system:
reboot
After reboot, you should be able to log in as root using the new SSH key.