How To SSH into EC2

If it has been a while since I’ve connected to my AWS Linux instance via ssh, I’ll sometimes find myself searching the web for these steps. I’m using a mac so these file locations apply to mac.

If you need to connect to an existing EC2 and you have the .pem file (private key)

  1. Get the public IP address for your EC2 instance.
  1. Find the location of your .pem fiIe. It would usually be located at
/Users/<username>/.ssh/<keypairname>.pem
  1. SSH with the following command.
ssh -i /Users/<myusername>/.ssh/<mypemfile>.pem ec2-user@<my.public.ip.address 

If you are creating a new EC2 and will need to connect

  1. Check if you have a .pem file to reference in your ssh command. It would usually be located at
/Users/<username>/.ssh/<keypairname>.pem
  1. If you don’t have one, then you’ll need to create a new key pair to get one. In the AWS console, go to EC2 under the Network and Security section on the left, select Key Pairs.
  1. Click ‘Create Key Pair’. Select .pem unless you use PuTTY. When you click to create the key pair, the .pem file will automatically download to your computer.
  1. Place it in directory
 /Users/<username>/.ssh/
  1. Change the permissions on the .pem file.
 chmod 400 /User/<username>/.ssh/myprivatekey.pem
  1. Get the public IP address for your instance from the AWS console, EC2 instance details.
  1. SSH into the instance.
ssh -i /Users/<myusername>/.ssh/<mypemfile>.pem ec2-user@<my.public.ip.address

If you lost your .pem file (private key) and need to SSH into your server

Try this suggestion