Skip to content
BENIBIX Learn

The SSH Config File

Level 2 · Keys and shortcuts

How ~/.ssh/config turns a long ssh command into a short name. How to set the user, address, port and key once for each server you use.

Course contentsLesson 5 of 9

After this lesson you can

  • Write a Host block in ~/.ssh/config
  • Log in with a short name
  • Know which tools read the same file

10 mincovers 1 glossary entries

Worth reading first: SSH Agent and Passphrases

How to stop typing the same long command

The SSH config file, ~/.ssh/config on your laptop, gives each server a short name. Under the name you write its address, your user, the port and the key to use. Then ssh shop does the same as ssh -p 2222 -i ~/.ssh/id_ed25519 lucy@203.0.113.10. The file is plain text, and you can write it by hand.

Each server gets a block that starts with Host and the short name. The lines under it are indented. HostName is the real address. User is your user name there. Port is only needed if it is not 22. IdentityFile points at the key.

The short name works for more than ssh. scp, sftp and rsync read the same file. So rsync -av site/ shop:/var/www/ copies to the right server, as the right user, with the right key.

A Host line can also match many names at once. Host * applies to every server, so it is the place for settings you want everywhere. Put it at the end of the file.

For each setting, ssh uses the first value it finds and ignores later ones. So a specific block must come before Host *. If a setting seems to be ignored, look for an earlier block that matched first.

This file is your own, on your laptop. It changes nothing on any server. The file that sets rules for the server itself is a different one, with a similar name, and it lives on the server.

A common mistake is to put a password in the config file. There is no setting for it, on purpose. Use a key, and let the agent hold it.

console
$ cat ~/.ssh/config
Host shop
    HostName 203.0.113.10
    User lucy
    Port 2222
    IdentityFile ~/.ssh/id_ed25519
$ ssh shop
lucy@shop:~$
One block in the config file, and the long command becomes two words.
You type the short name. ssh fills in the rest from the config file.

Worked example

Jack's six servers

Jack looks after 6 servers, each with its own address, user and port. He keeps the commands in a text file and pastes them in.

He moves them into ~/.ssh/config as 6 Host blocks. Now he types ssh db or ssh web2, and his copy jobs use the same short names.

The words for this lesson1

Check yourself

A setting in Host shop is ignored. What is the likely cause?

Finished this lesson?

Marking it complete also marks its 1 glossary entries learned, which updates your roadmap and your saved page. 0 are already marked.