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)
- Get the public IP address for your EC2 instance.
- Find the location of your .pem fiIe. It would usually be located at
/Users/<username>/.ssh/<keypairname>.pem
- 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
- Check if you have a .pem file to reference in your ssh command. It would usually be located at
/Users/<username>/.ssh/<keypairname>.pem
- 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.
- 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.
- Place it in directory
/Users/<username>/.ssh/
- Change the permissions on the .pem file.
chmod 400 /User/<username>/.ssh/myprivatekey.pem
- Get the public IP address for your instance from the AWS console, EC2 instance details.
- SSH into the instance.
ssh -i /Users/<myusername>/.ssh/<mypemfile>.pem ec2-user@<my.public.ip.address