-1

I've added my public SSH key into AWS EC2 instance's authorized_keys and I already double checked that the format and values are correct. The problem is I'm unable to login to terminal using following command without a .pem file but I can login with the .pem file. The issue is why I'm not able to login without a .pem file.

Not working with this command:

sudo ssh ubuntu@publicIPAddress

Error:

Permission denied (publickey).

working with command:

sudo ssh -i mypemfile.pem ubuntu@publicIPAddress
Zain Khan
  • 1,644
  • 5
  • 31
  • 67

1 Answers1

1

ssh needs to know which key to use to connect to a given host.

In order to drop the -i option you need to tell ssh where to look for the .pem file.

From man ssh the -i:

Selects a file from which the identity (private key) for public key authentication is read. You can also specify a public key file to use the corresponding private key that is loaded in ssh-agent(1) when the private key file is not present locally. The default is ~/.ssh/id_rsa ...

Here's what you can do to use ssh <host name>:

  • Copy your .pem file to the ~/.ssh folder
  • Create a config file with vim ~/.ssh/config

Then add the following:

Host this_is_your_short_name_for_the_ec2
HostName ec2.full.server.address
User ec2-user (or any other valid user name e.g. ubuntu)
IdentityFile "~/.ssh/<your_key>.pem"

If all goes well, you should be able to log in with just:

ssh my-ec2-host-name
baduker
  • 19,152
  • 9
  • 33
  • 56
  • what if I want a 3rd party service to login to my EC2? eg: Bitbucket – Zain Khan May 30 '23 at 15:36
  • What do you mean? How Bitbucket is supposed to connect to your EC2? As a user? Or do you want to *execute* scripts on the EC2 *from* Bitbucket? – baduker May 30 '23 at 15:39
  • Yes, I want to execute the script from Bitbucket. I already did that but don't why I'm getting this error at this time.. It's working on other places well. – Zain Khan May 30 '23 at 20:58
  • https://stackoverflow.com/questions/68906490/how-to-execute-some-script-on-the-ec2-instance-from-the-bitbucket-pipelines-yml – baduker May 30 '23 at 21:01