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:
nano ~/.ssh/authorized_keys
  • Open up your existing public key on your local machine in any editor and copy it to the clipboard.
  • Paste it into the nano editor (usually using the right-mouse-click button).
  • 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

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 within 24 hours of publication.  No other emails will be sent unless you sign up for our general list in the footer of this site as well.

Posted in