SSH Key Lost: Unable to Connect to Hetzner Server
Losing your SSH key can be a frustrating experience, especially when you can't connect to your Hetzner server. In this article, we will cover the key concepts and steps required to regain access to your server. We will also discuss some related topics, such as resetting the root password and generating new SSH keys.
What is an SSH Key?
SSH keys are a secure way of logging into your server. They consist of a private key, which is kept secret, and a public key, which is placed on the server. When you try to connect to the server, the server uses the public key to verify that the client has the corresponding private key. If the keys match, the client is allowed to connect.
Why Can't I Connect to My Server?
If you have lost your SSH key, you will not be able to connect to your server using that key. Additionally, if someone else has access to your private key, they could potentially use it to connect to your server without your permission. In either case, it is important to take action to secure your server and regain access.
Resetting the Root Password
One solution to this problem is to reset the root password for your server. This can be done through the Hetzner Robot, which is a web-based interface for managing your server. To reset the root password, follow these steps:
- Log in to the Hetzner Robot.
- Select your server from the list.
- Click on the "Reset Root Password" button.
- Enter a new password and confirm it.
- Click on the "Reset" button.
Once the password has been reset, you can use the new password to log in to your server.
Generating a New SSH Key
If you would prefer to use SSH keys to log in to your server, you can generate a new key pair. This can be done using the ssh-keygen command in a terminal:
$ ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/Users/username/.ssh/id_rsa): [press enter]
Created directory '/Users/username/.ssh'.
Enter passphrase (empty for no passphrase): [type a passphrase]
Enter same passphrase again: [type the same passphrase]
Your identification has been saved in /Users/username/.ssh/id_rsa.
Your public key has been saved in /Users/username/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:[key fingerprint] username@username-MacBook-Pro
The key's randomart image is:
+---[RSA 3072]----+
| .oo+o |
|.o=o+ |
|.o.o. |
|=o..o . |
|o.o.. S . |
|.o.+ . . |
|.o.. . . |
|.E. . . |
|oo+. . |
+----[SHA256]-----+Once you have generated the new key pair, you will need to add the public key to your server. This can be done using the ssh-copy-id command:
$ ssh-copy-id -i ~/.ssh/id_rsa.pub user@server
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
user@server's password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh 'user@server'"
and check to make sure that only the key(s) you wanted were added.- SSH keys are a secure way of logging into your server.
- If you have lost your SSH key, you will not be able to connect to your server using that key.
- You can reset the root password for your server through the Hetzner Robot.
- You can generate a new SSH key pair using the
ssh-keygencommand.
References
-
SSH keys: SSH.com - Generating SSH Keys
-
Resetting the root password: Hetzner - How do I reset the root password of my dedicated root server?
-
Generating a new SSH key: DigitalOcean - How To Set Up SSH Keys