EC2 – ssh configuration with .pem keys

How can I ssh to my aws ec2 instance? That is quite often asked question.

This short article explains how to connect to EC2 server using your ssh command and how to configure ssh to avoid passing additional parameters every single time.

Connecting to Amazon EC2

Before you get connected you will have to download Amazon EC2 .pem file and copy it into your secured location. Good place to store the key would be your local ssh folder:

/Users/your_user_name/.ssh/ec2.pem or /home/your_user_name/.ssh/ec2.pem

To test ssh connection you would need to run below command from your console, replacing with your server name.

If you got connected certification worked as it should. Otherwise make sure you are running this command from the right folder and you passing right location to your file. Also remember that EC2 standard user is not root but ec2-user.

Configuring shortcut for even quicker access

Open or create ~/.ssh/config file using your favorite editor like pico, nano or vim.

and then paste following line replacing with the id you want to use and location to .pem file with correct one.

Next save config changes and set right permissions of your PEM file to 700 to (1) protect it from unauthorized eyes and (2) to give your ssh client exactly what  is required.

You are ready to go. Just type:

If you need root access use:

November 19th, 2012 by

Leave a Reply

24 Comments on "EC2 – ssh configuration with .pem keys"

10 months 13 days ago

$ chmod 700 ~/.ssh/ec2.pem

Why 700? Better is this one:

$ chmod 400 ~/.ssh/ec2.pem

10 months 27 days ago

nice post – thanks for sharing!

Marcus LeFlore
1 year 4 months ago

This is a very good article on SSH login without password. One that worked for me when I first started doing this. It’s very simple, concise and easy to understand.

1 year 4 months ago

The step for adding the “User ec2-user” to the ~/.ssh/config helped me tremendously. I was trying to locally run a git fetch against a remote git repository that is hosted on my EC2.

Thanks again!

1 year 5 months ago

The above post demonstrates how to use a single .pem for multiple users. It may fit your use case.