How to Connect to Amazon EC2 Remotely Using SSH

Most of the new Amazon web service (AWS) users will be stuck with permission issue while launching an EC2 instance, I have hit with the same before 6 years back. But why? Because of the lack of reading AWS documentation cause it. This may help someone to resolve the permission issue like One of user posted the same issue in Unix.StackExchange, Refer link at the bottom of the post.

Amazon EC2 Instance
  • Download the .pem file.
  • In Amazon Dashboard choose “Instances” from the left sidebar, and then select the instance you would like to connect to.
  • Click on “Actions“, then select “Connect
  • Click on “Connect with a Standalone SSH Client
  • Open up a Terminal window

Create .ssh directory

# mkdir -p ~/.ssh

Move the downloaded .pem file to the .ssh directory we just created. In case if you place the key anywhere other than .ssh directory we need to use “-i” option during SSH.

# mv ~/Downloads/ec2private.pem ~/.ssh

Change the PEM file to appropriate Permission

Change the permissions of the .pem file to 400 only the respective user should read the PEM file. Amazon recommended permission should fix everything.

# chmod 400 ~/.ssh/ec2private.pem

Create a config file

Create a config file under .ssh folder for all instance and identified by anyone of key and user.

# vim ~/.ssh/config

Enter the following text into that config file

Host *amazonaws.com
IdentityFile ~/.ssh/ec2private.pem
User ec2-user

Save the file.

Use the ssh command with your public DNS hostname to connect to your instance. eg:

# ssh ec2-54-23-23-23-34.example.amazonaws.com

You will be good now by accessing EC2 Instance.

Stack Exchange Reference

What is the right file permission for a .pem file to SSH and SCP

Conclusion:

Many new AWS users will come across this issue, Maybe this could help someone to save their time.

2 thoughts on “How to Connect to Amazon EC2 Remotely Using SSH

Comments are closed.