scp'ing using key file as a parameter, How can I do that if possible?

54

10

scp -i ~/.ssh/id_rsa.pub events*$date*_QA.log $remote_user@$remote_server:$remote_location

Is the aforementioned script incorrect? Am I not doing it correctly?

I went to the .ssh directory and saw that the remote server is in the known_hosts file.

But, when I do ordinary scp without any file as parameter, it is still asking for password

scp events*$date*_QA.log $remote_user@$remote_server:$remote_location

How can I include the key file in my command?

sh-3.2$ grep server ~/.ssh/*
/home/user/.ssh/known_hosts:server....com,ip_addr ssh-rsa Asine=jhjsdhfjsadhfjkashdfjhasdjfhksadjfhasjdfhj

user122530

Posted 2012-03-12T19:37:26.067

Reputation:

Answers

70

 -i identity_file
         Selects the file from which the identity (private key) for public
         key authentication is read.  This option is directly passed to
         ssh(1).

Use ~/.ssh/id_rsa.

whitequark

Posted 2012-03-12T19:37:26.067

Reputation: 14 146

1You want the local user's ~/.ssh/id_rsa.pub content placed in the remote server's authorized_keys file. It's usually in ~/.ssh/authorized_keys. You also need to be very particular about the file permissions on files in ~/.ssh, making them minimally accessible. sshd is very particular about the file permissions. – JR Lawhorne – 2016-04-07T17:55:26.153

On Mac OS X it only worked if -i ~/.ssh/id_rsa was the first option. So this worked: scp -i ~/.ssh/mykey.pem -r dir user@remote_server:~/ . This didn't work: scp -r -i ~/.ssh/mykey.pem dir user@remote_server:~/ – asmaier – 2018-07-12T11:13:45.133

still asking for password sh-3.2$ scp -i ~/.ssh/id_rsa test_QA.log user@server:location user@server's password: – None – 2012-03-12T21:23:57.663

@tech_learner strange, as that works for me – whitequark – 2012-03-12T22:04:05.933

2You may have to use an actual path, as in /home/user/ instead of ~. That seemed to have made it work for me. – Evan Darwin – 2013-02-17T21:15:02.170

1

This might help another noob.

[I know this is a redundant circular example but its good to illustrate] Scenario:

  1. ssh from Mac -> Ubuntu
  2. scp files from Mac -> Ubuntu
  3. close ssh and scp files from Ubuntu -> Mac

I had only created ssh keys on my Mac (via ssh-keygen) and shared them with the machine running Ubuntu via (ssh-copy-id). So I could copy files, while logged into my Mac from the machine running Ubuntu, but not the other way around.

Solution: I had to create ssh keys on the Ubuntu Machine and share them with my Mac. then I could successfully run the following command on the Ubuntu Machine

Mac IP: 192.168.1.40
Ubuntu IP: 192.168.1.38

On Mac

ssh-keygen
ssh-copy-id ubuntu@192.168.1.38

ssh ubuntu@192.168.1.38

# Now on Ubuntu
ssh-keygen
ssh-copy-id MAC@192.168.1.40

And now the following command should copy the file without asking for password to MAC

sudo scp -i /home/ubuntu/.ssh/id_rsa MAC@192.168.1.40:~/Documents/Fluff/Version-Control/tools/pull.sh .

Patrick Walter

Posted 2012-03-12T19:37:26.067

Reputation: 11

0

Tested and corrected via these instructions: https://askubuntu.com/questions/46930/how-can-i-set-up-password-less-ssh-login

The instructions here worked flawlessly when I tested on my boxes (CentOS/CentOS). I imagine the issue is your ssh keys are not tied to a username.

Example: cat authorized_keys # on Box I'm sshing/scpin' to ssh-rsa BLAHBLAHBLAHBLAH/zAcS4kD9pyPAjD3/gd5D1rcQa6IztCMR9yMXiGFnxviWsT8/oYevZw25k4yREuA8ibLKC9peH1X4LK1E+n7gq4TETexWkZbQ2XGLOX44eglra3MB4FShPg0cZXGcJWltPQ/y0Ay2A/KmaC14YrDfqwm7+ibTiUp4hOO8I6eIPmwwGn/2hs0SewJXisGqUx2v my_user@machine.local #username is tied to the key and is an authorized host

Max Humphrey

Posted 2012-03-12T19:37:26.067

Reputation: 1