I can ssh into EC2 from the cloud shell, but not from my Mac terminal. Any troubleshooting tips?

0

Hello! I'm by no means an AWS expert, so I'm throwing this out there with the hopes someone knows the answer.

I have been trying to SSH into my EC2 instance from the Mac terminal, but keep getting "connection refused". When I do this via Cloud Shell however, everything works fine. In both environments, I follow the same steps:

  1. created a private/public key via ssh-keygen
  2. add the public key to my security credentials
  3. run the ssh command: ssh ec2-<blah blah>.compute-1.amazonaws.com

I have a feeling something might be wrong on my Mac. Do I need to clear any settings? Looks like the ssh-agent has no stored keys.

asked a year ago1305 views
2 Answers
0

Is the error message just "Connection refused" or is there any more to it? Does this come up right away, or does it wait for a while?

You'll likely need the username as part of your command, this varies according to the OS (on Amazon Linux and RHEL it's ec2-user, CentOS it's centos, Ubuntu it's ubuntu) and also specify your private key in the command (I know you say you've saved it to your profile, but no harm in explicitly stating it just for troubleshooting). So something like ssh -i /path/to/mykeyfile.pem ec2-user@ec2-<blah blah>.compute-1.amazonaws.com

if ithis still fails, try verbose mode to get more detailed error messages ssh -vvv -i ....

This AWS doc may help too https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/AccessingInstancesLinux.html#AccessingInstancesLinuxSSHClient

profile picture
EXPERT
Steve_M
answered a year ago
0

Its probably the Security Group attached to EC2 Instance.

  1. Check the security group settings for your EC2 instance, and see if the inbound rules allow SSH traffic from your IP address ( or for testing alone set it to 0.0.0.0/0) . You can check this in the EC2 console under the "Security Groups" section.

  2. Set the correct permissions to your private key :

 chmod 400 /path/to/private/key.

If you're using a different username on the EC2 instance than your local username, you'll need to specify the correct username when you SSH. For example, if your EC2 instance uses the username "ec2-user", you would run the following command:

ssh -i /path/to/private/key ec2-user@ec2-<blah blah>.compute-1.amazonaws.com.

to get more details of your issue you can try -v flag for verbose.

ssh -v -i /path/to/private/key ec2-<blah blah>.compute-1.amazonaws.com.
answered a year 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