Sub topic 4
Connecting with ssh
This chapter assumes you have a running Linux instance with a public IPv4 address in a public subnet. It also assumes you downloaded its key pair's private key (a .pem file) and have the AWS CLI configured. The chapter shows how to open a shell with the ssh client. Later chapters cover failures and saved configuration.
What an SSH connection needs
SSH is a protocol that opens an encrypted shell on a remote machine. For EC2, five things must line up before it works:
- The instance has a public IP address.
- The subnet's route table sends internet traffic to an internet gateway.
- The instance's security group allows inbound TCP port 22 from your address.
- You hold the private key that matches the key pair chosen at launch.
- You use the login user that your AMI expects.
Choosing the login user
The key pair gives you a key, not a user name. The user is set by the AMI.
| AMI | Default user |
|---|---|
| Amazon Linux | `ec2-user` |
| Ubuntu | `ubuntu` |
| Debian | `admin` |
| RHEL | `ec2-user` |
If you pick the wrong user, the server rejects you even though the key is correct. This looks the same as a bad key.
Opening the connection
SSH refuses a private key that other users can read. Restrict the file to owner read-only, then connect:
chmod 400 ~/.ssh/my-key.pem
ssh -i ~/.ssh/my-key.pem [email protected]The -i option names the private key. The part before @ is the login user. The part after it is the instance's public IP address or public DNS name.
On the first connection, ssh shows the host key fingerprint and asks whether to continue. Answering yes stores the fingerprint in ~/.ssh/known_hosts. Later connections compare against it. If the fingerprint changes, for example because a new instance reused an Elastic IP address, ssh warns you loudly. Remove the stale entry with ssh-keygen -R 203.0.113.25 only after you confirm the change is expected.
Limiting who can reach port 22
Allow SSH from one address instead of the whole internet:
aws ec2 authorize-security-group-ingress \
--group-id sg-0123456789abcdef0 \
--protocol tcp \
--port 22 \
--cidr 203.0.113.25/32The /32 suffix means exactly one IPv4 address. Security groups are stateful, so the reply traffic is allowed automatically and you need no outbound rule for it.
Getting more information
Add -v to see each stage of the connection:
ssh -v -i ~/.ssh/my-key.pem [email protected]The output shows which keys the client offers and where the server stops accepting them. Use -vvv for more detail. Reading this output is the main skill for the next chapter on failures.
Exercise
You run ssh -i my-key.pem [email protected] against a new Ubuntu instance. The security group allows your address on port 22, and the key matches the launch key pair. The server answers "Permission denied (publickey)". What is the most likely cause, and how do you fix it?
Show answer
The login user is wrong. Ubuntu AMIs use ubuntu, not ec2-user. Rerun the command as ssh -i my-key.pem [email protected]. If the key file also has loose permissions, run chmod 400 my-key.pem.