51
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
65
 -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.

  • still asking for password sh-3.2$ scp -i ~/.ssh/id_rsa test_QA.log user@server:location user@server's password: – user122530 Mar 12 '12 at 21:23
  • @tech_learner strange, as that works for me – whitequark Mar 12 '12 at 22:04
  • 2
    You may have to use an actual path, as in /home/user/ instead of ~. That seemed to have made it work for me. – Evan Darwin Feb 17 '13 at 21:15
  • 1
    You 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 Apr 7 '16 at 17:55
  • 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 Jul 12 '18 at 11:13
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 .
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

Your Answer

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