Ubuntu – SCP over SSH with no password – cannot get it to work

scpssh

I have an Ubuntu 16.04 server with SSH, I have configured a keypair so I can SSH on to a remote system with no password, this works fine.

I am trying to SCP from Local (system1) to ther remote (system2), the SCP works fine but asks for a password. I want to use the keypair for non-autheticated copying.

I have followed numerous guides from the web on how to do this, but it basically seems to boils down to the following:

scp -i <path the PRIVATE key file>  filetocopy  user@remote.co.uk:~

Everything I can find on the internet says this should work – but it keeps asking for the password for the remote (2nd) system, even though I know the keypair works OK because I can use it for SSH.

Annoyingly – the same command as above will work perfectly with no password request if run as root – either by way of 'sudo' or from an elevated terminal…

so:

sudo scp -i <path the PRIVATE key file>  filetocopy  user@remote.co.uk:~

Works fine, but:

scp -i <path the PRIVATE key file>  filetocopy  user@remote.co.uk:~

does not.

There are no files in the roots' .ssh folder except for the known_hosts files (i.e. no key files)

What should I do?

Best Answer

It seems like you have copied the root key to the remote server, while the regular user key wasn't copied correctly.

ssh and scp should provide the same feedback (with the difference of login-into-remote-server vs. copy-file-into-remote-server)

When the key-pair will be copied as required to the remote server, the scp command will be:

scp local-file-name user@remote-host:/full/path/remote-file-name

In order to copy the user key to the remote server, you can use ssh-copy-id

Note: You should replace the mykey in the below command, with the filename of your key-file

Copy the key to a server

Once an SSH key has been created, the ssh-copy-id command can be used to install it as an authorized key on the server. Once the key has been authorized for SSH, it grants access to the server without a password.

Use a command like the following to copy SSH key:

ssh-copy-id -i ~/.ssh/mykey user@host

This logs into the server host, and copies keys to the server, and configures them to grant access by adding them to the authorized_keys file. The copying may ask for a password or other authentication for the server.

Only the public key is copied to the server. The private key should never be copied to another machine.

Test the new key

Once the key has been copied, it is best to test it:

ssh -i ~/.ssh/mykey user@host

The login should now complete without asking for a password. Note, however, that the command might ask for the passphrase you specified for the key. Troubleshooting