Why can't I ssh-copy-id to an EC2 instance?

Solution 1:

I needed to run

ssh-add ~/.ssh/ec2-keypair

Solution 2:

I had the same problem: ssh-copy-id gives the error Permission denied (publickey) on an AWS EC2 instance. I was sure that I set all the permissions correctly using chmod.

In addition, I needed to change this line in /etc/ssh/sshd_config from

PasswordAuthentication no

to

PasswordAuthentication yes

I guess that's because ssh-copy-id asks for your password.

Then the error disappeared.

Solution 3:

Just do:

scp -i ec2-keypair ~/.ssh/id_rsa.pub [email protected]:~/

Then

ssh -i ec2-keypair [email protected]

And when you logged in:

cat id_rsa.pub >> .ssh/authorized_keys
rm id_rsa.pub
exit

Now you can log with

ssh [email protected]

Solution 4:

It seems like ssh-copy-id is confused about connecting with a key in order to copy another key.

My solution:

ssh-copy-id -f "-o IdentityFile ec2-keypair.pem" [email protected]

Breakdown:

  • -o IdentityFile ec2-keypair.pem: I'm using a "raw" ssh option to connect using the AWS-generated key.
  • -f: ssh-copy-id tries to copy the AWS-generated key again, so force mode is necessary to copy the other key.