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.
Check or create SSH config
Section titled “Check or create SSH config”First check whether ~/.ssh/config already exists. If it does not, create the file and set its permissions.
# check if config existsls -la ~/.ssh/config
# if it does not exist, create ittouch ~/.ssh/configchmod 600 ~/.ssh/configIf the file already exists, list the host aliases defined in it to avoid naming conflicts.
grep -E '^Host ' ~/.ssh/configPlace your key file
Section titled “Place your key file”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.
cp my_ec2_ssh_key.pem ~/.ssh/my_ec2_ssh_key.pemchmod 400 ~/.ssh/my_ec2_ssh_key.pemAdd the host to ~/.ssh/config
Section titled “Add the host to ~/.ssh/config”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.
Host my-ec2 HostName my_ec2_public_ip User ubuntu IdentityFile ~/.ssh/my_ec2_ssh_key.pemThe default username for Ubuntu based instances is ubuntu. Amazon Linux instances use ec2-user instead.
Connect
Section titled “Connect”Once the config entry is saved, connect using just the host alias.
ssh my-ec2