Sub topic 4
Connecting with ssh
This chapter covers the connection itself: what the ssh client does, which values you pass, and how to read what it prints. It assumes the following:
- You are comfortable with a Linux or macOS shell, or the OpenSSH client on Windows.
- You know what an EC2 instance, an AMI, and a VPC subnet are.
- The AWS CLI is installed and configured with credentials.
- The instance is running in a subnet with a route to an internet gateway and has a public IPv4 address.
- Its security group allows inbound TCP port 22 from your address.
- You still have the private key file for the key pair chosen at launch.
The previous chapters cover the network and key pair requirements. Here we put them together.
What happens when you run ssh
An SSH connection has three stages: a TCP connection, a check of the server's identity, and a check of yours.
If the TCP connection never completes, the problem is on the network path (route, security group, or network ACL). If it completes but authentication fails, the problem is the key or the login user. This split is the most useful thing to keep in mind when something breaks.
Gather the three values you need
Every connection needs an address, a login user, and a private key.
To get the address, ask the EC2 API:
aws ec2 describe-instances \
--instance-ids i-0123456789abcdef0 \
--query "Reservations[].Instances[].PublicIpAddress" \
--output textYou can also use PublicDnsName. Either value changes when the instance is stopped and started, unless you attached an Elastic IP address.
The login user depends on the AMI, not on the key pair. The key pair only supplies the public key that is installed for that user.
| AMI family | Default login user |
|---|---|
| Amazon Linux 2 and 2023 | `ec2-user` |
| Ubuntu | `ubuntu` |
| Debian | `admin` |
| RHEL | `ec2-user` |
| SUSE Linux | `ec2-user` |
| Fedora | `fedora` |
| CentOS | `centos` |
If you are unsure, check the AMI's documentation or its AWS Marketplace listing. Some AMIs are customized and use a different user.
Make the first connection
SSH ignores a private key that other users on your machine can read, so restrict the file first. Then connect:
chmod 400 ~/.ssh/my-key.pem
ssh -i ~/.ssh/my-key.pem [email protected]-i names the private key (the identity file). The part before the @ is the login user, and the part after it is the instance address. You never type a password. The instance stores only the public half of the key.
On the first connection, the client has never seen this host and prompts you:
The authenticity of host '203.0.113.25 (203.0.113.25)' can't be established.
ED25519 key fingerprint is SHA256:abc123...
Are you sure you want to continue connecting (yes/no/[fingerprint])?This is the server-identity check. If you answer yes, the client saves the host key to ~/.ssh/known_hosts. On later connections, the client compares the key the server presents with the saved one. Some AMIs print their host key fingerprints to the instance's console output on first boot. If you want to be certain you are talking to your own instance, compare that value with the prompt. You can read the output with aws ec2 get-console-output.
Host key warnings after an IP address is reused
Public IP addresses are recycled. If you stop an instance, or terminate it and later launch another one that receives the same address, the host key at that address is different. The client then refuses to continue:
WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!This warning is correct behavior. SSH is telling you the identity at this address changed. When you know why (you replaced the instance), remove the stale entry and reconnect:
ssh-keygen -R 203.0.113.25Avoid the common shortcut of setting StrictHostKeyChecking no and UserKnownHostsFile /dev/null in your config. It silences the warning, and it also silences the case where someone is impersonating your server.
Working with more than one key
If ssh-agent holds many keys, the client offers them one after another. The server counts each rejected offer as a failed attempt and disconnects after a few (often six, set by MaxAuthTries). You then see Too many authentication failures even though the correct key is on disk. To make the client offer only the key you named, add IdentitiesOnly:
ssh -o IdentitiesOnly=yes -i ~/.ssh/my-key.pem [email protected]To load a key into the agent so you are not typing the path each time, use ssh-add ~/.ssh/my-key.pem. The agent keeps the key in memory for your session.
On Windows 10 and later, the built-in OpenSSH client accepts the same command. The key file's permissions must allow only your account. PuTTY cannot use a .pem file directly. It needs a .ppk file converted with PuTTYgen.
Beyond the interactive shell
The same connection settings work for single commands and file transfer.
Run one command and return to your local shell:
ssh -i ~/.ssh/my-key.pem [email protected] 'uptime'Copy a file to the instance with scp, which takes the same -i option:
scp -i ~/.ssh/my-key.pem ./app.tar.gz [email protected]:/tmp/Other options you will use often:
-p 2222connects to a non-default port. This only works ifsshdon the instance listens there and the security group allows it.-vprints each step of the connection. Add morevcharacters (-vv,-vvv) for more detail.-N -L 8080:localhost:80forwards a local port through the SSH connection without opening a shell. Session Manager offers a similar feature, covered later.
Reading the first signs of trouble
Each failure message points to a different layer. The dedicated troubleshooting chapter goes deeper, so this is only a first pass.
| What you see | Layer | First thing to check |
|---|---|---|
| Hangs, then `Connection timed out` | Network | Security group rule for port 22, route table, public IP |
| `Connection refused` | Instance | `sshd` is not running or listens on another port |
| `Permission denied (publickey)` | Authentication | Login user, key file, or the key pair the instance was launched with |
| `UNPROTECTED PRIVATE KEY FILE` | Local file | Run `chmod 400` on the key |
| `Too many authentication failures` | Local client | Use `IdentitiesOnly=yes` |
Run ssh -v to see which stage fails. If the output stops before Server host key, the TCP connection is the problem. If it reaches Offering public key and then fails, the problem is authentication.
Summary of the working model
The ssh client needs a reachable address, the AMI's login user, and the private key that matches the public key installed on the instance. It verifies the server against known_hosts first and then proves your identity with the key. Failures before the host key exchange are network problems. Failures after it are about the user or the key.
Exercise
You launch an Ubuntu instance and run ssh -i my-key.pem [email protected]. The connection reaches the server, but you get Permission denied (publickey). The key file is correct. 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, so the server finds no matching authorized key for ec2-user. Run ssh -i my-key.pem [email protected].
Exercise
You terminated an instance and launched a new one that received the same public IP address. ssh now prints WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED! and refuses to connect. Why does this happen, and what is the safe response?
Show answer
The saved entry in ~/.ssh/known_hosts holds the old instance's host key, and the new instance presents a different one. Because you know you replaced the instance, remove the stale entry with ssh-keygen -R 203.0.113.25 and reconnect, then accept the new fingerprint. Do not disable host key checking globally.