title | excerpt | updated |
---|---|---|
How to replace an SSH key pair |
Find out how restore server access by replacing an SSH key pair with a new one in case your private key is lost |
2024-04-04 |
If you are using SSH keys to connect to your server, losing your private SSH key could mean losing access to your server altogether.
However, you can still connect to your server via the OVHcloud rescue mode, which allows you to log in with a provisional password and modify your files.
This guide explains how to replace your SSH keys if you have lost access to your server.
Warning
OVHcloud is providing you with services for which you are responsible, with regard to their configuration and management. It is therefore your responsibility to ensure that they function correctly.
This guide is designed to assist you in common tasks as much as possible. Nevertheless, we recommend that you contact a specialist service provider or reach out to our community if you face difficulties or doubts concerning the administration, usage or implementation of services on a server.
- A dedicated server or a VPS in your OVHcloud account
- Access to the OVHcloud Control Panel
Create a new SSH key pair on your device, as described in the first part of the SSH key guide.
Follow the steps in the rescue mode guide to connect to your server and mount your partitions:
In order to proceed, your system partition must be mounted and you must have write access to your file system.
This means that you have entered a version of the following command into the rescue mode shell:
chroot path/to/partition/mountpoint
The exact command is dependent on the mountpoint you used. For example, if you have mounted your partition at /mnt
, it would be the following:
chroot /mnt/
When you have access to your files, open the "authorized_keys" file concerned with a text editor. This file stores SSH keys and is located in the home
folder of the user with which you connect to your server. (Replace "USER_NAME" with your actual user name.)
nano /mnt/home/USER_NAME/.ssh/authorized_keys
Copy and paste your new public key (created in step 1) into the file. It should look similar to the following example:
ssh-rsa 1111111111122222222222333333333333444444444555555555556666666666
777777777778888888888999999900000000000000000000000000== old@sshkey
ssh-rsa AAAAAAAAABBBBBBBBBBBCCCCCCCCCCCCCCCCDDDDDDDDDDDDDDDDDDDEEEEEEEEE
EEFFFFFFFFFFFFFGGGGGGGGGGGGGhhhhhhhhhhhhhhhhhhhhhhhhhh== new@sshkey
For security reasons, delete the obsolete "old" key string from the file. Save your changes and exit the editor.
Switch the boot mode back to "normal" and restart the server in your OVHcloud Control Panel. Refer to the rescue mode guide if necessary.
You have now access to the server with your new SSH key pair.
Join our community of users.