I need to ssh into a remote server. I set up a fresh Ubuntu install in VirtualBox for that purpose.

First of all I generated my public key:

ssh-keygen -b -4096

Then tried to ssh:

ssh remote_user@remote_server

But I am getting the following error:

remote_user@remote_server: Permission denied (publickey).

Not sure what I am missing.

edit: I ran ssh-copy-id remote_user@remote_server and got the following errors:

/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
remote_user@remote_server: Permission denied (publickey).
  • After you generated your public/private key pair, did you actually install the public key onto the remote host (e. g. ssh-copy-id user@host.example.com?) – DopeGhoti Apr 25 at 19:03
  • @DopeGhoti do you mean I should install it in my VM local machine? I just tried doing it and got some other error messages. – dabadaba Apr 25 at 19:07
  • Is the remote server a cloud VM? Who administers it? – Jesse_b Apr 25 at 19:10
  • @Jesse_b it's Digital Ocean I believe. – dabadaba Apr 25 at 19:12
  • 1
    Oh, ok, thank you. I'll let the corresponding person know and I'll come back if problems persist, hopefully not. – dabadaba Apr 25 at 19:15

If you want to configure key based authentication on your remote server you need to actually add your key to it.

You can do that using ssh-copy-id which is available on most distributions with the following syntax:

ssh-copy-id remote_user@remote_server

Note: You will need to know remote_user's password to complete this.

It will then look for your id_rsa.pub file and add it to the remote server's authorized_keys file.

If you saved your key to somewhere other than id_rsa.pub you can specify this location using:

ssh-copy-id -i /path/to/custom_key.pub remote_user@remote_server

Otherwise you can log into remote_server via some other means (password login) and manually add the contents of your public key into the authorized_keys file.

This file is in ~/.ssh/authorized_keys.

You can use the following command to perform this from your ubuntu vm (again as long as you have remote_user's password:

cat ~/.ssh/id_rsa.pub | ssh username@remote_host "mkdir -p ~/.ssh && cat >> ~/.ssh/authorized_keys"

Many cloud providers require you upload the key to your account on their web portal to have it added to the machine's authorized_keys file. They often block other methods of making this change.

In the case of Digital Ocean you must follow the instructions here.

  • I do have the remote_user's password but I am getting an error when runnin ssh-copy-id. Check out my edit. And yes, the keys are in ~/.ssh/id_rsa.pub. – dabadaba Apr 25 at 19:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.