14

I have a ubuntu Natty instance on EC2, and I can SSH into it by

ssh -v -i ec2-keypair ubuntu@ubuntu@XXXX.compute-1.amazonaws.com

But I'd like to set up password less sshing. So I tried these options and nothing is working:

 $ ssh-copy-id -i ~/.ssh/id_rsa.pub ubuntu@XXXX.compute-1.amazonaws.com
Permission denied (publickey).

 $ ssh-copy-id -i ~/.ssh/ec2-keypair ubuntu@XXXX.compute-1.amazonaws.com
/usr/bin/ssh-copy-id: ERROR: No identities found

 $ ssh-copy-id -i ~/.ssh/id_rsa.pub root@XXXX.compute-1.amazonaws.com
Permission denied (publickey).
Jeremy Smith
  • 507
  • 1
  • 4
  • 10

4 Answers4

26

I needed to run

ssh-add ~/.ssh/ec2-keypair
Jeremy Smith
  • 507
  • 1
  • 4
  • 10
  • I have done this for both postgres and my admin normal user still when I run `ssh-copy-id -f -i /var/lib/postgres/.pubfilename ubuntu@ec2-domain` still getting permission denied error. – Piyush S. Wanare Mar 20 '17 at 07:48
  • Note in some environments, you'll need to run `eval "$(ssh-agent)"` if you see `Could not open a connection to your authentication agent` – 4Z4T4R Oct 13 '17 at 23:04
3

Just do:

scp -i ec2-keypair ~/.ssh/id_rsa.pub ubuntu@XXXX.compute-1.amazonaws.com:~/

Then

ssh -i ec2-keypair ubuntu@XXXX.compute-1.amazonaws.com

And when you logged in:

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

Now you can log with

ssh ubuntu@XXXX.compute-1.amazonaws.com
Sathyajith Bhat
  • 61,504
  • 38
  • 179
  • 264
1

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" ubuntu@XXXX.compute-1.amazonaws.com

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.
Sergio
  • 11
  • 1
1

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.

Steve Tjoa
  • 111
  • 3
  • Beware though that changing the PasswordAuthentication from 'no' to 'yes' can lock you out of your EC2 instance. – Kingz Jul 08 '14 at 23:57