Sub topic 6
Saving connection details in ~/.ssh/config
This chapter assumes you can already connect to an instance with ssh -i key.pem user@address, that you know which login user your AMI uses, and that you have a key pair file with correct permissions. It covers how to store those details so you type them once.
Why use a config file
A full SSH command repeats three values every time: the address, the login user, and the key path. These are easy to mistype, and the public IP of an instance changes when it is stopped and started unless you use an Elastic IP. The OpenSSH client reads ~/.ssh/config before each connection and fills in any option you left out on the command line.
A basic host entry
Each entry starts with a Host line that defines an alias. The indented lines under it are options that apply when you use that alias.
Host web1
HostName 203.0.113.25
User ec2-user
IdentityFile ~/.ssh/my-key.pemWith this entry, ssh web1 is equivalent to ssh -i ~/.ssh/my-key.pem [email protected]. The alias web1 is only a local label. It is not a DNS name, and it is not the instance's Name tag.
| Option | Meaning |
|---|---|
| `Host` | The alias you type after `ssh`. Patterns such as `*` are allowed. |
| `HostName` | The real address or DNS name to connect to. |
| `User` | The login user, for example `ec2-user` on Amazon Linux. |
| `IdentityFile` | Path to the private key to offer. |
How matching works
OpenSSH reads the file from top to bottom. For each option, the first value it finds wins. Later entries cannot override an earlier one. For this reason, put specific hosts first and general defaults such as Host * last.
Host web1
HostName 203.0.113.25
User ec2-user
IdentityFile ~/.ssh/my-key.pem
Host db1
HostName 10.0.2.15
User ubuntu
IdentityFile ~/.ssh/db-key.pem
Host *
ServerAliveInterval 60
IdentitiesOnly yesServerAliveInterval 60 sends a keepalive message every 60 seconds so idle sessions are less likely to be dropped by a firewall or NAT device. IdentitiesOnly yes makes SSH offer only the key named in IdentityFile. Without it, an agent holding many keys may offer several, and the server can reject you after too many failed attempts.
Where the config file helps later
The same file is where you add a ProxyCommand for SSH over Session Manager. That entry matches instance IDs, so ssh ec2-user@i-0123456789abcdef0 works without an open port 22. You will build it in a later chapter.
Checking what SSH will use
If a connection uses the wrong key or user, ask SSH to print the options it resolved for an alias. The -G flag prints the final configuration without connecting.
ssh -G web1 | grep -E '^(hostname|user|identityfile) 'For a connection attempt in progress, ssh -v web1 shows which config file was read and which keys were offered.
Common mistakes
- Indenting with the wrong layout is harmless, but a typo in an option name causes an error such as "Bad configuration option".
- A broad
Host *block placed first setsUserfor every host, and your specific entries cannot override it. - Forgetting to update
HostNameafter a stop and start leads to a timeout. Allocate an Elastic IP or use the instance's public DNS name and check it again. - Reusing an alias for two instances means only the first matching entry applies.
Exercise
Your config has Host * with User admin at the top, followed by Host web1 with User ec2-user. Why does ssh web1 log in as admin, and how do you fix it?
Show answer
OpenSSH uses the first value it finds for each option, and Host * matches web1 first, so User admin wins. Move the Host web1 block above Host * so its User ec2-user is read first.