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

Write one block per server in ~/.ssh/config so that ssh app replaces a long command. Examples for HostName, User, Port, IdentityFile and shared defaults.

Source: https://valakes.com/learn/ssh-config-file/

Last updated: 2026-11-25

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.

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

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:

```console
$ 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:

```console
$ 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](https://valakes.com/learn/host-key-changed/) 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:

```console
$ 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](https://valakes.com/learn/ssh-keep-alive/) 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:

```console
$ 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:

```console
$ 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:

```console
$ 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:

```console
$ 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](https://valakes.com/learn/ssh-jump-host/) shows it. Ports that you want to reach through the connection are covered in [SSH port forwarding](https://valakes.com/learn/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:

```console
$ 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`.

```console
$ 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.

## In Valakes

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](https://valakes.com/features/production-marking/)

[Read the docs: Getting started](https://docs.valakes.com/getting-started/)

## 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

- [ssh_config(5) on Ubuntu 24.04, the client configuration file](https://manpages.ubuntu.com/manpages/noble/man5/ssh_config.5.html)
- [ssh_config(5) at man7.org](https://man7.org/linux/man-pages/man5/ssh_config.5.html)
- [ssh(1) on Ubuntu 24.04, the order of the configuration sources](https://manpages.ubuntu.com/manpages/noble/man1/ssh.1.html)
- [OpenSSH manual pages](https://www.openssh.org/manual.html)

## Related articles

- [How do I SSH through a jump host (bastion)?](https://valakes.com/learn/ssh-jump-host/): Add -J user@bastion to the ssh command, or write ProxyJump bastion in the block of the server. ssh connects to the bastion first and opens the connection to the server through it.
- [How do I stop SSH from dropping with "Broken pipe"?](https://valakes.com/learn/ssh-keep-alive/): Add ServerAliveInterval 30 to the Host * block of ~/.ssh/config. ssh then sends a small message every 30 seconds, so a router that forgets idle connections sees traffic, and ssh notices when the server stops answering.
- [How does SSH port forwarding work? Examples of -L, -R and -D](https://valakes.com/learn/ssh-port-forwarding/): ssh -L 9000:127.0.0.1:8080 app opens port 9000 on your machine and sends every connection to port 8080 on the server app. -R does the reverse, and -D turns the connection into a SOCKS proxy.
- [How do I fix "REMOTE HOST IDENTIFICATION HAS CHANGED" in SSH?](https://valakes.com/learn/host-key-changed/): The server now shows a different host key than the one saved in your known_hosts file. Check the new fingerprint with the server's owner, remove the old entry with ssh-keygen -R, and connect again.
