Now that we have an SSH key pair, we need to set up our virtual server to accept this key.
On your local machine, use the cat command to print out the content of your public key to the terminal and copy it to your clipboard (for example, using Ctrl + Shift + C):
$ cat ~/.ssh/id_rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACAQC0TG9QcuUeFFtcXLqZZNO6/iggvuoLkQzlZQGbnSd39M+kLjRii+ziMBq8gL1pZUOBLWZUr6c+5DiCSOQCWtduTnHq6hR7/XkRthoS3bsdplr/6SHdxW/GTkVUjAv/DWcdJ93tx5ErkFsGsWklKM2U5wRMNA1g6k3ooc1N21zftBQKp9K+vrUW/iporjvy2Y8Dicp2VRUiOZIediDLYSZUXI/mc9eLziZivhsQtFYOZQSFMuBRBX7q4RA6XTBdnjORac1oVhVHi1NlU7ZmkWeJUECEFxncrYsp976p4tAKNOijpQMDhpKYdZT4OS83r33cIA2mdnNfK1SL1zntfoYYh+s3KODbnvoZcqCn4oar6ZPxgL9E4oqOF5Td+VQv8yRdxKstwQAgV6Yu0Ll/gJ0Z0k5xxw6SS3u/9J6Wx2q85eZLJl0o1dxHcofhQ1UZrJOZ23YnUsrDhHvqZRpHjkfXCPgDOWVNzdpTQPYbUttVuHsFw5HqjfVb5Pco4HlzhS4qCG91UkC7+tDMc6zXsaal9Sh4YIQE0RDDkRV3k3fFLYLMnxK4NCydPX9E9Fcaneopr+o1mauiNvdQLjALL4t8Bz8P0KSvfIGhu0suaQEIJamrdzPFcXigQn2IK719Ur8/0sxqbXAblzRauJ0qrYyvOXx3/1G+4VywN40MyY7xdQ== dan@danyll.com
$ xclip -selection clipboard < ~/.ssh/id_rsa.pub
Now, if you haven't done so already, log in to the remote server as root using your password. Next, create the ~/.ssh directory and a ~/.ssh/authorized_keys file, if they do not already exist. The authorized_keys file lists the keys that the server accepts as valid credentials:
root@hobnob:# mkdir ~/.ssh
root@hobnob:# touch ~/.ssh/authorized_keys
Next, set the permissions on the file so that only the current user (root) can read the file:
root@hobnob:# chmod 700 ~/.ssh
root@hobnob:# chmod 600 ~/.ssh/authorized_keys
Then, append the public key you just copied to the end of the authorized_keys file (for example, using vim or nano):
root@hobnob:# vim ~/.ssh/authorized_keys
Lastly, we need to reload the SSH daemon to ensure our changes are updated:
root@hobnob:# systemctl reload ssh.service
To test that this is working, open a new terminal window and run ssh root@<remote-ip>:
$ ssh root@142.93.241.63
root@hobnob:#
This time, the server doesn't ask for your password anymore, as it is using our SSH key to authenticate.