Sub topic 10
Simplifying Connections with `~/.ssh/config`
This chapter assumes that you can use a Linux or macOS command line, connect to a remote Linux server with ssh, and use Unix file permissions. You should also have a working SSH key pair and a public key installed in the remote account's ~/.ssh/authorized_keys.
Typing a full SSH command is easy to get wrong:
ssh -i ~/.ssh/id_ed25519 -p 2222 [email protected]The SSH client reads settings from ~/.ssh/config. A configuration entry lets you replace that command with a short host alias:
ssh myserverThe alias is only a local shortcut. It does not change the server's hostname, username, port, or authentication method.
Create a host alias
Create the file if it does not exist:
mkdir -p ~/.ssh
touch ~/.ssh/config
chmod 700 ~/.ssh
chmod 600 ~/.ssh/configAdd an entry like this:
Host myserver
HostName server.example.com
User alice
Port 2222
IdentityFile ~/.ssh/id_ed25519
IdentitiesOnly yesEach setting has a specific purpose:
Host myserverdefines the name you type afterssh.HostNamegives the real DNS name or IP address.Userselects the remote account.Portselects the SSH server port. The default is22, so omit this line when the server uses the default.IdentityFileselects the private key on your computer.IdentitiesOnly yestells SSH to offer the named key instead of trying many keys from the agent.
The indentation is optional, but indentation makes the file easier to read. Option names are not case-sensitive, but use the usual capitalization so other administrators can understand the file.
Test the alias:
ssh myserverThe first connection may ask you to verify the server's host key. Confirm the fingerprint through a trusted channel before accepting it. SSH stores accepted host keys in ~/.ssh/known_hosts. A host key identifies the server, while your private key identifies you. They solve different security problems.
Use multiple hosts and patterns
You can define several aliases in one file:
Host web-prod
HostName 203.0.113.10
User deploy
IdentityFile ~/.ssh/id_ed25519_work
IdentitiesOnly yes
Host database-prod
HostName db.example.com
User admin
IdentityFile ~/.ssh/id_ed25519_admin
IdentitiesOnly yesUse separate keys when different systems or trust boundaries require them. A stolen work key should not automatically grant access to unrelated personal servers.
A Host pattern can apply shared defaults:
Host *.example.com
ServerAliveInterval 60
ServerAliveCountMax 3
Host app-prod
HostName app.example.com
User deploy
IdentityFile ~/.ssh/id_ed25519_work
IdentitiesOnly yesSSH applies the first obtained value for most options. Put specific entries before broad patterns when both entries set the same option. The Host name used on the command line is matched against the patterns, not necessarily against HostName.
Inspect the effective configuration
Use ssh -G to print the settings SSH will use without connecting:
ssh -G app-prod | grep -E '^(hostname|user|port|identityfile|identitiesonly) 'For connection problems, add verbose logging:
ssh -v app-prodCheck that SSH selected the expected hostname, username, port, and private key. If the output shows a different identity, inspect overlapping Host entries and confirm that IdentitiesOnly yes is in the intended block.
Exercise
Write a ~/.ssh/config entry for the alias backup. It must connect as operator to backup.example.com on port 22 using ~/.ssh/ided25519backup, and it must not offer other agent keys.
Show answer
Use: Host backup HostName backup.example.com User operator Port 22 IdentityFile ~/.ssh/ided25519backup IdentitiesOnly yes Then test it with ssh backup and inspect its resolved settings with ssh -G backup.