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 ec2.server.name.com 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 my.host 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:

Freelance developer, IT enthusiast, blogger with entrepreneurial spirit and passion for making games

Tagged with:
, , , , ,
Posted in
Technical
Categories