touch ~/.ssh/config
chmod 600 ~/.ssh/configAppendix B — SSH config
Every connection in the book starts with a long ssh command: a key file, a user, an IP address, and sometimes a couple of -o options. SSH can read all of these from a file at ~/.ssh/config, so the command shrinks to ssh <alias>. This appendix pulls together the SSH settings from AWS Server Login, Managing SSH Key Pairs, and Linux Commands in one place.
Creating the file
The config file lives on our local machine (not the EC2 instance). Create it if it doesn’t exist, then restrict it to our own account:
SSH applies the same permission rules here as it does to keys (see SSH directory and file permissions). If ~/.ssh/config is writable by other users, SSH refuses to read it and prints Bad owner or permissions on ~/.ssh/config.
A basic entry
Each entry starts with a Host line followed by indented settings:
Host aws-dev
HostName YOUR_PUBLIC_IP_ADDRESS
User ec2-user
IdentityFile ~/.ssh/aws-dev.pem| Setting | Replaces | Meaning |
|---|---|---|
Host |
(nothing) | The alias we type after ssh |
HostName |
@YOUR_PUBLIC_IP_ADDRESS |
The public IP or Public DNS name of the instance |
User |
ec2-user@ |
The login user for the AMI |
IdentityFile |
-i ~/.ssh/aws-dev.pem |
The private key to present |
With the entry saved, these two commands do the same thing:
ssh -i ~/.ssh/aws-dev.pem ec2-user@YOUR_PUBLIC_IP_ADDRESS
ssh aws-devThe alias also works with scp, so copying files from Upgrade to Amazon Linux 2023 becomes scp aws-dev:~/.bashrc ./bashrc-al2.
Forcing a single key
IdentityFile (like -i) only suggests a key. SSH still offers every key held by ssh-agent, and it offers them first. Two things can go wrong:
If the agent holds a key the server also accepts, that key wins and the key file is never tested.
If the agent holds several keys the server doesn’t accept, the server stops listening after its limit (six attempts by default) and closes the connection with
Too many authentication failuresbefore our key gets a turn.
On the command line we fixed this with two options (see Linux Commands):
ssh -i ~/.ssh/aws-dev.pem -o IdentitiesOnly=yes -o IdentityAgent=none ec2-user@YOUR_PUBLIC_IP_ADDRESSIn the config file, the same options become two more lines in the entry (written without the =):
Host aws-dev
HostName YOUR_PUBLIC_IP_ADDRESS
User ec2-user
IdentityFile ~/.ssh/aws-dev.pem
IdentitiesOnly yes
IdentityAgent noneIdentitiesOnly yesrestricts SSH to the key named inIdentityFile.IdentityAgent noneturns offssh-agentfor this host, so no agent keys are offered.
Several entries
One file can hold an entry for each user and key we use. With the keys from Managing SSH Key Pairs and the test-user account from Linux Users, the file on the System76 machine might look like this:
# Recovery access with the AWS key pair
Host aws-dev
HostName YOUR_PUBLIC_IP_ADDRESS
User ec2-user
IdentityFile ~/.ssh/aws-dev.pem
IdentitiesOnly yes
IdentityAgent none
# Routine access with our own key
Host my-ec2
HostName YOUR_PUBLIC_IP_ADDRESS
User ec2-user
IdentityFile ~/.ssh/<your-key-name>
IdentitiesOnly yes
# Non-root user
Host my-ec2-test
HostName YOUR_PUBLIC_IP_ADDRESS
User test-user
IdentityFile ~/.ssh/<your-key-name>
IdentitiesOnly yesOn the MacBook, the same aliases point to the machine specific key (IdentityFile ~/.ssh/<your-key-name>-macbook). Private keys never leave the machine that made them; only the config entries look alike.
The routine entries keep the agent on (no IdentityAgent none), which is useful when the key has a passphrase that the agent remembers. IdentitiesOnly yes still stops the agent from offering its other keys.
When the IP changes
A stopped instance gets a new public IP when it starts (unless it has an Elastic IP), so HostName goes stale. Look up the new address from the local machine:
aws ec2 describe-instances \
--instance-ids YOUR_INSTANCE_ID \
--query "Reservations[*].Instances[*].PublicIpAddress" \
--output text \
--profile dev-deployThen edit the HostName line of each entry for that instance. That’s the only change; the aliases, users, and keys stay the same. The Public DNS name from Using the Public DNS works as a HostName too, but it encodes the IP, so it changes at the same time.
Two other things go stale with a new IP:
SSH asks to confirm the host key again, because
~/.ssh/known_hostsstores keys by address (see Host key verification).If our own home IP changed, the port 22 rule in the security group no longer matches and
sshends withConnection timed out. That’s a security group problem, not a config problem (see Security Group doesn’t allow SSH (port 22)).
Only a stop followed by a start changes the IP. Rebooting from inside the instance (sudo reboot) keeps the same address.
Looking up the IP automatically
Editing HostName after every start gets old fast. Instead, a small shell function can look up the current IP and hand it to ssh, which makes the HostName line in the config irrelevant. The function below goes in ~/.bashrc (or ~/.zshrc on macOS, where zsh is the default shell):
awsdev() {
local ip
ip=$(aws ec2 describe-instances --profile dev-deploy \
--instance-ids YOUR_INSTANCE_ID \
--query 'Reservations[].Instances[].PublicIpAddress' \
--output text)
ssh -o HostName="${ip}" aws-dev "$@"
}-o HostName= overrides the value in the aws-dev entry, so the user, key, and IdentitiesOnly settings still come from ~/.ssh/config. "$@" passes any extra arguments along to ssh. After reloading the shell (source ~/.bashrc), awsdev connects at whatever IP the instance has now, and awsdev -v shows the debug output.
The function needs a running instance. If the instance is stopped, the lookup returns None and ssh fails, so start the instance first.
Checking the config
ssh -G prints the settings SSH would use for an alias without connecting:
ssh -G aws-dev | grep -E '^(hostname|user|port|identityfile|identitiesonly|identityagent) 'user ec2-user
hostname YOUR_PUBLIC_IP_ADDRESS
port 22
identitiesonly yes
identityagent none
identityfile ~/.ssh/aws-dev.pemIf a value is missing or wrong, the entry has a typo or an earlier block already set it. To watch the settings in use during a real connection, add -v (ssh -v aws-dev); the debug lines show the config file being read and each key SSH offers.
Recap
~/.ssh/configlives on the local machine, needs600permissions, and turns a longsshcommand intossh <alias>.IdentitiesOnly yesandIdentityAgent nonemake SSH use only the key inIdentityFile, which preventsToo many authentication failures.One entry per user and key keeps recovery (
.pem) and routine access separate.When the instance gets a new IP, update
HostName(nothing else in the entry changes), or let a shell function likeawsdev()look up the IP for us.