Can't SSH to EC2 instance

0

Hello, I'm encountering an issue with an SSH to EC2 instance that I was unable to resolve. When trying the Private IPv4 addresses I'm getting "Connection timed out." When trying the Public IPv4 address I'm getting "Permission denied (publickey)." The instances' security group has an inbound rule permitting SSH access using TCP protocol through my IP address and I tried also with a rule allowing access via 0.0.0.0/0. My laptop's firewall should allow for outgoing calls ("Default outgoing policy changed to 'allow'"). I've checked the network gateway connectivity as instructed here and used a user data script as instructed here. I did not experience any such issues in the past but did not use my account since March this year. I'm unable to connect via my laptop (Ubuntu 22.04.1 LTS) or via "EC2 Instance Connect." Is there any idea that I did not try which may help me connect to the instance?

asked a year ago867 views
4 Answers
2

Hi,

The private ipv4 address is limited to be used with the AWS VPC where your instance resides, we cannot connect to that IP directly from outside the AWS network hence the timeout error. To connect to an instance we need to use the public IP address.

Assuming that you have created a key pair during the EC2 instance creation. The error "Permission denied (publickey)" indicates that the connection was denied due to the authentication failure with the private key file provided while trying to connect to the instance.

First, check if you are using the correct key file to authenticate the SSH connection and try connecting to the network -> https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/AccessingInstancesLinux.html

Additionally, for EC2-Connect to work properly you need to allow inbound SSH traffic from the EC2 Instance Connect IP range in addition to your IP address. Please refer to the Configure network access to an instance section -> https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ec2-instance-connect-set-up.html

profile picture
Bisina
answered a year ago
  • Many thanks Bisina, I've set up a new instance and key pair to test your suggestion and can now SSH to my instance

  • Unfortunately, I'm still getting permission issues. I was able connect to a new instances with a new set of key pairs, install software, but when trying to connect later, via my laptop or CloudShell I got:

    ssh -i ~/.aws/kp.pem ubuntu@ec2-xx-xxx-xxx-xxx.compute-1.amazonaws.com ubuntu@ec2-xx-xxx-xxx-xxx.compute-1.amazonaws.com: Permission denied (publickey). [cloudshell-user@ip-10-6-111-134 ~]$ ssh -i kp.pem ubuntu@ec2-54-146-178-153.compute-1.amazonaws.com ssh: connect to host ec2-xx-xxx-xxx-xxx.compute-1.amazonaws.com port 22: Connection timed out

    The Inbound rules allow for access from anywhere for testing purposes. When launching yet another machine with another key pair, I am able to connect. Incidentally, even when I was able to access the machine, I was frequently disconnected with a "broken pipe" message.

1

In addition to the two docs that you have linnked to, and the docs linked in other answers, there is this one that deals exactly with the error message that you are getting - Permission denied (publickey) https://repost.aws/knowledge-center/ec2-linux-fix-permission-denied-errors

Is any of the advice in this document of any assistance?

profile picture
EXPERT
Steve_M
answered a year ago
  • Hi RWC, I checked the methods proposed in the issue you referred me to and tried method 4 which is more readily accessible than the others. I was able to repair SSH permissions with a user data script but when I deleted it (to prevent data deletion in the next reboot), the issue resumed. The other methods are more complex than trying a different a different cloud provider.

1

Can you try connecting with CloudShell? It's the little prompt icon in the top right: https://aws.amazon.com/cloudshell/

profile pictureAWS
answered a year ago
0

I'm experiencing the same issue, with my macbook pro m1 terminal, as well as aws cloudshell

answered 8 months ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions