54

I am trying to copy few files to a target system using scp and then login to the system and install those files. I used scp and ssh commands here with ssh keys for passwordless authentication.

The ssh key was created on the source system as below. Is this the correct and secured way of creating an ssh key?

~]# ssh-keygen -t rsa -N "" -f ~/.ssh/mytest.key

The key was copied from the source to target system with executing below command.

~]# ssh-copy-id -i ~/.ssh/mytest.key 

Now, the SSH login works fine without prompting for a password, however the scp is still not working.. it still prompts for a password. Should I specify the key path when using scp? If so how do I specify the keypath along with scp command?

Here is the ssh command used

~]# ssh -i ~/.ssh/mytest.key root@192.168.1.1
user3721640
  • 777
  • 2
  • 11
  • 15

4 Answers4

115

Assume your case for scp from 192.168.1.1 try below command.

scp -i ~/.ssh/mytest.key root@192.168.1.1:/<filepath on host>  <path on client>

make sure the key file should have permission 600 or 400.

Mahattam
  • 5,405
  • 4
  • 23
  • 33
  • How can i skip already existing files? – TrevorDeTutor Sep 09 '22 at 08:46
  • 1
    scp will overwrite the file, if want to skip already existing file. I think rsync will work, refer https://unix.stackexchange.com/questions/127352/specify-identity-file-id-rsa-with-rsync this could be helpful – Mahattam Sep 10 '22 at 11:18
  • Surely 8 years and 100 users should have been sufficient to spot that that this would copy the file **FROM** not "TO a target system" :) – mirekphd Mar 05 '23 at 08:57
3

You can follow below procedure as well:

  1. Generate ssh key using below command
~]$ ssh-keygen

It will ask you for details like file name, file location, passphrase etc.

  1. Make sure keep location and file name as default which will be
/home/user/.ssh/rsa_id

After confirming passphrase, private and public key files will be created.

  1. Create file with name authorized_keys in same .ssh folder as in step 2. Copy contents of public key in this file.

You are done! Works for both ssh and scp the only password required will your passphrase (if you keep one).

damndemon
  • 331
  • 3
  • 11
0

another option to copy ssh key:

ssh-copy-id root@192.168.1.1

after that you can use scp:

scp ./file root@192.168.1.1:/path_on_host/

result:

 [file] -transfer-> [192.168.1.1:/path_on_host/]
ksemele
  • 3
  • 3
-1

First, change chmod of the file on the remote before scp....

sudo chmod 775 'file-name'

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Nov 08 '21 at 00:23