SSH config file (~/.ssh/config): examples that work

The file ~/.ssh/config stores the address, user, port and key of each server under a short name. After that, ssh app does the work of a long command.

Last updated

Commands checked on Ubuntu 24.04.5 LTS (the ubuntu:24.04@sha256:562111b88caa79c71391c0c4d3c2114a7d5c35915c20561fd893cb1e07929e6b container image), .

Put one Host block per server in ~/.ssh/config. Under the name you choose, write the address, the user, the port and the key. From then on ssh app connects, and so do scp, sftp and rsync with the same name. The rest of the file holds defaults for every host.

A first file

Create the file with your editor. The one below has two servers and a block of defaults at the end. Only you should be able to write to it, so look at the permissions too:

$ stat -c '%a %n' ~/.ssh ~/.ssh/config
700 /home/dev/.ssh
600 /home/dev/.ssh/config
$ cat ~/.ssh/config
# Login to the app server
Host app
    HostName app.example.test
    User deploy
    IdentityFile ~/.ssh/id_ed25519

# The database host listens on a different port
Host db
    HostName db.example.test
    User admin
    Port 2222

# Defaults for every host that no block above has settled
Host *
    IdentitiesOnly yes
    ServerAliveInterval 30
    StrictHostKeyChecking accept-new

Each block starts with Host and a name. The lines under it apply when you type that name. HostName is the real address, because the name after Host is only a label. IdentityFile points to the private key for that server. Connect with the label:

$ ssh app hostname
Warning: Permanently added 'app.example.test' (ED25519) to the list of known hosts.
app

ssh took app.example.test as the address and deploy as the user without being told. The warning on the first line is the first connection to a new server, which StrictHostKeyChecking accept-new accepted and saved. If the key of a known server changes later, ssh refuses, and the fix for that warning is a separate page.

See what ssh will use

Before you connect, ask ssh what it would do. ssh -G prints the final value of every option for a name, and it does not open a connection:

$ ssh -G app | grep -E '^(hostname|user|port|identityfile|serveraliveinterval) '
user deploy
hostname app.example.test
port 22
serveraliveinterval 30
identityfile ~/.ssh/id_ed25519
$ ssh -G db | grep -E '^(hostname|user|port|serveraliveinterval) '
user admin
hostname db.example.test
port 2222
serveraliveinterval 30

app uses port 22, the ssh default. db has the port from its own block. Both have serveraliveinterval 30 from the last block, which you wrote once for all hosts. Keep-alive settings explains that option.

Settings for a group of servers

A Host line can hold a pattern. * matches any characters and ? matches one, and ! in front of a pattern excludes it. Without a HostName line, ssh connects to the name you typed. This block sets the user and the port for every server called web- and a number:

$ printf 'Host web-*\n    User deploy\n    Port 2200\n' > /tmp/pattern.conf
$ ssh -F /tmp/pattern.conf -G web-3 | grep -E '^(hostname|user|port) '
user deploy
hostname web-3
port 2200

One block now serves web-1, web-2 and every server you add later, and you did not have to list them.

The first value wins

This rule causes most of the confusion. For each option ssh uses the first value it finds, reading from the top. A block further down cannot override a block above it. Put specific hosts first and Host * last.

The command line comes before the file, so -o beats anything in it:

$ ssh -G -o User=ops app | grep '^user '
user ops

Here is the mistake in a file where Host * comes first. The config file below sets User ops for everyone and User deploy for app, in that order:

$ printf 'Host *\n    User ops\n\nHost app\n    User deploy\n' > /tmp/wrong.conf
$ ssh -F /tmp/wrong.conf -G app | grep '^user '
user ops

The user is ops, not deploy. The general block matched first and settled the option.

Split a long file

A file with forty servers is hard to read. Include pulls in other files. Put it at the top of the file, because below a Host line it applies to that block only:

$ mkdir -p ~/.ssh/config.d
$ printf 'Host cache\n    HostName cache.example.test\n    User ops\n' > ~/.ssh/config.d/cache.conf
$ sed -i '1i Include ~/.ssh/config.d/*.conf' ~/.ssh/config
$ ssh -G cache | grep -E '^(hostname|user) '
user ops
hostname cache.example.test

One file per team or per project keeps changes small. Servers behind a bastion need one more line, and the jump host article shows it. Ports that you want to reach through the connection are covered in SSH port forwarding.

Which files ssh read

When a block does not seem to apply, ask for the debug output and look at the first lines:

$ ssh -v app true 2>&1 | grep 'Reading configuration data'
debug1: Reading configuration data /home/dev/.ssh/config
debug1: Reading configuration data /home/dev/.ssh/config.d/cache.conf
debug1: Reading configuration data /etc/ssh/ssh_config

Your file comes first, then the files it includes, then the system file. That is the order in which the first value wins.

Mistakes that cost time

The file is writable by others. ssh stops and tells you, as in the last two commands below. Take the write permission away with chmod 600 ~/.ssh/config.

$ chmod 666 ~/.ssh/config
$ ssh app hostname
Bad owner or permissions on /home/dev/.ssh/config
$ chmod 600 ~/.ssh/config

Too many keys. ssh offers every key it knows, and a server closes the connection after a few failed attempts (sshd allows six by default), so the right key may never be tried. IdentitiesOnly yes makes ssh offer only the keys named with IdentityFile. It is in the defaults block above for this reason.

A Host pattern that is too wide. Host *.example.test matches every name that ends in .example.test, including the ones you did not mean. ssh -G shows you what a name receives before you rely on it.

If your servers are already in ~/.ssh/config, press Import in Valakes. It lists the hosts, guesses an environment for each one and asks you to review the list before it saves anything. Feature: Production marking

Questions

Where does ssh look for its configuration?

In three places, in this order. The command line, then ~/.ssh/config of your user, then /etc/ssh/ssh_config for everyone on the machine. For each option the first value found is used.

Do I need to restart anything after I edit the file?

No. ssh reads the file each time you start it. A connection that is already open keeps the values it started with.

What permissions must the file have?

Only you may write to it. 600 is the usual choice. If other users can write to it, ssh refuses to read it.

Can I use the same alias with scp, sftp and rsync?

Yes. They call ssh and read the same file, so scp app:/etc/hostname . and rsync -av app:/var/www/ . use the block named app.

References

The app is free.

Get notified at launch

macOS is coming to the MacĀ AppĀ Store. iOS and Android later.