3

I am on Amazon AWS and I have two EC2 instance on my account. Trying to send rsa key via SSH to another EC2 instance using internal private IP. But its not working.

scp -i .ssh/id_rsa.pub xxx.xxx.xxx.xxx:/root/.ssh/authorized_keys2

What am I doing wrong and how do I send file to another EC2 account with ssh.

Bart De Vos
  • 17,911
  • 6
  • 63
  • 82
Maca
  • 1,043
  • 2
  • 19
  • 30

5 Answers5

6

Here the scp format:

scp -i <key> <what_to_copy> <where_to_copy>

You are missing <what_to_copy> in the example.

Also check the security group of the target server. It should allow the connections either the the security group of the source sever or from the 10.0.0.0/8 network.

smith3v
  • 261
  • 2
  • 5
  • I guess im clueless with SCP. How do I locate the ? Is this from my local machine? I am reciveing "Identity file key.pem not accessible" – Maca Dec 02 '11 at 10:14
  • Technically, it is the 3rd argument that is missing; the source can be a remote path. – Falcon Momot Oct 23 '12 at 05:39
  • i forgot to add security groups, your answer helped. thanks – ufk Mar 04 '14 at 10:45
2

This won't completely replace the authorized_keys file, so you can continue logging in with previous access:

cat .ssh/id_rsa.pub | ssh -i key.pem USER@x.x.x.x 'cat >>.ssh/authorized_keys'

where "USER" might be "root", "ec2-user", or "ubuntu", depending on the AMI you are running.

Or, just start your EC2 instances using your personal ssh keys in the first place. Here's an article I wrote about that:

Uploading Personal ssh Keys to Amazon EC2
http://alestic.com/2010/10/ec2-ssh-keys

Eric Hammond
  • 11,163
  • 1
  • 36
  • 56
1

You are probably using the 10.x.x.x-ip of the instance. This won't work. Use the external path. (something like ec2-75-102-166-16.compute-1.amazonaws.com). Or attach an elastic IP to it and use that.

Bart De Vos
  • 17,911
  • 6
  • 63
  • 82
  • I tried using the elastic IP then SCP but I received: Permission denied (publickey). lost connection – Maca Dec 02 '11 at 07:26
  • you're trying to add the pubkey to authorized_keys by logging in with the same key pair? :) just cat id_rsa.pub on the source server and paste into the target server's authorized_keys by hand. – lunixbochs Dec 02 '11 at 07:55
  • I think I got the right command but i get permission err. /root/.ssh/id_rsa.pub: Permission denied . Im logged in as ec2-user. – Maca Dec 03 '11 at 09:44
  • It will work if you configure it the right way, will be probably faster and you won't use data transfer, that is, it won't cost you money, as opposite to using public IP address. Probably that's the reason why the OP is asking for this. – Pere May 10 '17 at 15:51
1

I've moved my key to ~/.ssh then created the file ~/.ssh/config(or appended to it) with:

IdentityFile ~/.ssh/key.pem

This will let you ssh and scp without ever specifying the key location.

nanofarad
  • 179
  • 3
  • 13
David
  • 129
  • 7
1

I had this same issue today. Here is my script for a number of files:

 #!/bin/bash

files=(

/var/www/test1.txt

/var/www/test2.txt

)

from="XXX"

to="YYY"

for i in "${files[@]}"

do

  scp -3 -i ~/.ssh/AAA.pem ubuntu@$from:$i ubuntu@$to:$i

done

I hope this helps! :)

Mark Henderson
  • 68,823
  • 31
  • 180
  • 259