Skip to content

SSH into EC2 from Linux Host

A minimal SSH setup for connecting to an EC2 instance from Linux or WSL. The pattern defines a named host alias in ~/.ssh/config so you connect with a short command instead of specifying the key, user, and IP every time.

First check whether ~/.ssh/config already exists. If it does not, create the file and set its permissions.

Terminal window
# check if config exists
ls -la ~/.ssh/config
# if it does not exist, create it
touch ~/.ssh/config
chmod 600 ~/.ssh/config

If the file already exists, list the host aliases defined in it to avoid naming conflicts.

Terminal window
grep -E '^Host ' ~/.ssh/config

Copy your EC2 .pem key file into ~/.ssh/ and restrict its permissions. SSH will refuse to use a key file that is readable by others.

Terminal window
cp my_ec2_ssh_key.pem ~/.ssh/my_ec2_ssh_key.pem
chmod 400 ~/.ssh/my_ec2_ssh_key.pem

Open ~/.ssh/config in any editor and append the block below. Replace my_ec2_public_ip with the public IPv4 address of your instance. Find IPv4 in the EC2 console under Instance summary.

Terminal window
Host my-ec2
HostName my_ec2_public_ip
User ubuntu
IdentityFile ~/.ssh/my_ec2_ssh_key.pem

The default username for Ubuntu based instances is ubuntu. Amazon Linux instances use ec2-user instead.

Once the config entry is saved, connect using just the host alias.

Terminal window
ssh my-ec2