2
votes

Currently I have one computer properly set up to SSH into my EC2 instane, however I'm trying to connect another laptop as well. When I went to the AWS console to download another key pair and use it in Terminal to SSH, I get this error: Permission denied (publickey).

I've already tried performing the command chmod 400 /path/sshkey.pem but I still get a public key error. Does anybody know why this is?

Thanks so much!

p.s. the command I'm performing to SSH to my ec2 instance is: ssh -i /path/sshkey.pem [email protected]

1
Also, I am on a mac sshing into a linux ec2Apollo

1 Answers

6
votes

I'm not totally clear on the scenario here, but I think you're saying you did things in this order:

  1. Create an EC2 instance with keypair #1
  2. Create a new keypair (#2)
  3. Put the private key from keypair #2 on the new laptop
  4. Try to log in to the instance.

If that's what you're describing, then the problem is that keypair #2's public key has never been installed on the EC2 instance. You need the private key on your client, and the matching public key on the server you're connecting to.

Once the instance already exists, creating a new keypair in AWS will not update the key on an existing instance. You'd have to log in to it (with keypair #1), and put the new public key in the proper place.

I haven't done that myself in a while, but according to this page, you'd edit ~ec2-user/.ssh/authorized_keys (a text file) and append the public key from your key pair (which is in a text format, too) to the end of the file. You'd might have to restart the sshd daemon, which the command sudo /sbin/service sshd restart should do. But try logging in with the new key first; if you make a mistake editing the file, you could lock yourself out. (It's safer to create a new account and update its .ssh/authorized_keys to avoid locking the ec2-user account out by mistake.)