Add Your Existing SSH Key To The Root User Account

So, you’ve provisioned a server but your host didn’t install an SSH key-pair for you. Which likely means that you’re just logging in with a password.

But, you do have an SSH key-pair already and instead of generating another one unique to your server, you would like to use the existing one.

The easiest way to do this is as follows:

  • Log into your server using SSH as the user to which you want to add a key.
  • Run the following command to open up the file containing the public keys for that user:
sudo nano ~/.ssh/authorized_keys
  • Open up your existing public key on your local machine in any TEXT editor (do not open it in a Word Processor such as Microsoft Word) and copy it to the clipboard.
  • Paste it into the nano editor (usually using the right-mouse-click button). If there is an existing key in there already, paste the new public key on a new line.
  • Use ctrl-o to save the file.

Do NOT close your SSH session. Instead use a new SSH session to test your new login using your key-pair.

Note: By not closing the existing session you are still able to make changes – which is useful in case you get locked out for some reason. Once your login tests are complete and successful you can close the session.

Recommended: Prevent Root From Using A Password For Login

After you’ve confirmed that your root user can use your key-pair to login, you can disable the use of the password. This will force the root user to always use the key-pair for logins.

  • Open up the sshd_config file:
sudo nano /etc/ssh/sshd_config
  • Scroll down until you find the PasswordAuthentication option. Set it to “no”.
  • Use ctrl-o to save the file.
  • Restart the ssh server:
sudo systemctl reload ssh

Was This Article Useful? Or do you have questions or comments about it (or our products & services)? We'd love to hear from you!

Please enter your name.
Please enter a message.
You must accept the Terms and Conditions.
Please check the captcha to verify you are not a robot.

Automatic Notification Of New Articles

Sign up to get automatic notifications of new articles.  This is a different list than our standard list - you only get new articles once a week (usually on Mondays).  No other emails will be sent unless you sign up for our general list as well.

Posted in