# SSH config file shortcuts

> The SSH config file stores per-host settings such as user, port and key, so a short alias replaces a long ssh command.

URL: https://levelrail.com/learn/server-basics/ssh-config-file-shortcuts/
Updated: 2026-11-27

With the SSH config file, a short entry in `~/.ssh/config` turns a long command into `ssh myserver`. Once you manage two or three servers, commands like `ssh -p 2222 -i ~/.ssh/work_ed25519 deploy@203.0.113.10` get tiresome and easy to mistype. The same details also have to match in `scp` and `rsync`. A short entry in your SSH config file turns all of that into `ssh myserver`.

## How it works

When you run `ssh <name>`, the client reads the [SSH config file](https://levelrail.com/learn/glossary/#ssh-config-file)) `~/.ssh/config` on your own computer, then the system file `/etc/ssh/ssh_config`. It collects every `Host` block whose pattern matches the name you typed. For each option, the first value found wins. This is why specific entries go at the top and the catch-all `Host *` goes last.

The alias is only a label on your side. `HostName` holds the real address that is used for the connection. Everything you put in the entry (user, port, key) is used as if you had passed it on the command line, and a flag you type directly still overrides the file.

Because `scp`, `rsync -e ssh` and `git` over SSH all run `ssh` underneath, they use the same file. One entry works for all of them.

Useful options:

| Option | Meaning |
| --- | --- |
| `HostName` | The real IP address or domain |
| `User` | Login name |
| `Port` | SSH port, if not 22 |
| `IdentityFile` | Private key to use |
| `IdentitiesOnly yes` | Offer only the named key, not every key in the agent |
| `ProxyJump` | Reach the host through another SSH host |
| `ServerAliveInterval` | Send a keepalive every N seconds |

## Step 1: Create or open the file

On your own computer, not the server:

```bash
mkdir -p ~/.ssh
nano ~/.ssh/config
```

What you should see: an empty editor if the file is new, or your existing entries.

## Step 2: Add a host entry

```text
Host myserver
    HostName 203.0.113.10
    User deploy
    Port 22
    IdentityFile ~/.ssh/id_ed25519
    IdentitiesOnly yes
```

What each line does:

- `Host`: the alias you type.
- `HostName`: the real IP address or domain.
- `User`: the login name.
- `Port`: the SSH port, only needed if it is not 22.
- `IdentityFile`: the private key to use.
- `IdentitiesOnly`: use only that key, which avoids trying many keys from your agent.

The indented lines belong to the `Host` above them. Indentation is for readability, but a blank line or new `Host` ends the block.

## Step 3: Set permissions

SSH refuses a config file that others can write to (see [file permissions](https://levelrail.com/learn/server-basics/linux-file-permissions-explained/))):

```bash
chmod 600 ~/.ssh/config
```

What you should see: no output.

## Step 4: Add shared defaults (optional)

Settings under `Host *` apply to every host. Put it last, because the first value found for each option wins:

```text
Host *
    ServerAliveInterval 60
    AddKeysToAgent yes
```

`ServerAliveInterval` helps keep idle sessions from being dropped. `AddKeysToAgent yes` adds a key to your agent the first time you unlock it.

## Step 5: Reach a private server through a jump host (optional)

If a server has no public address, connect through one that does:

```text
Host private-db
    HostName 10.0.0.5
    User deploy
    ProxyJump myserver
```

Now `ssh private-db` logs in to `myserver` first and then opens the real connection from there. Your private key stays on your own computer.

## Check it worked

```bash
ssh myserver
```

You should land in a shell on the server with no need to type the user, IP or key. To see exactly what SSH will use, run:

```bash
ssh -G myserver | grep -E '^(hostname|user|port|identityfile) '
```

The output should show the values from your entry, each in lower case on its own line.

## Edge cases and gotchas

- **Order matters, first value wins.** If a general block above your entry sets `User`, it beats the user in the entry below. Keep specific hosts first and `Host *` last.
- **Patterns match the alias, not the real name.** `Host web-*` matches what you typed after `ssh`, not the `HostName`. A pattern like `*.example.com` matches only if you typed that full name.
- **Too many keys cause failures before yours is tried.** An agent holding many keys can hit the server's attempt limit. `IdentitiesOnly yes` fixes it.
- **Windows and WSL use different home folders.** The file lives under the user profile of whichever environment runs `ssh`. An entry in WSL is invisible to the Windows client, and the reverse.
- **IPv6 `HostName` values need no brackets.** Write `HostName 2001:db8::10` here. Brackets are only for `scp` and `rsync` targets on the command line.
- **A changed host key still warns.** The config does not hide host key changes. See [What is SSH](https://levelrail.com/learn/server-basics/what-is-ssh-and-how-does-it-work/)) for what to do.
- **Do not store secrets in the file.** It holds paths and names, which is fine. Never paste a private key or a password into it.

## Troubleshooting

| Symptom | Likely cause | Fix |
| --- | --- | --- |
| `Bad owner or permissions on /home/you/.ssh/config` | File is writable by others, or owned by another user | Run `chmod 600 ~/.ssh/config` and check the owner |
| `Could not resolve hostname myserver` | Typo in `Host`, or you are editing on the wrong machine | Check the spelling and that the file is on the computer you connect from |
| `Permission denied (publickey)` | Wrong `IdentityFile`, or the public key is not on the server | Fix the path, see [Log in with SSH keys](https://levelrail.com/learn/server-basics/log-in-with-ssh-keys/)) |
| `Too many authentication failures` | The agent offers many keys before the right one | Add `IdentitiesOnly yes` |
| Settings seem ignored | An earlier block already set that option | Run `ssh -G myserver` to see the effective value and reorder |
| `/home/you/.ssh/config: line 4: Bad configuration option` | Typo in an option name | Fix the named line |
| `ssh: connect to host ... port 22: Connection refused` using the alias | `HostName` or `Port` in the entry is wrong | Run `ssh -G myserver` and check both |
| The alias works in the terminal but not in a GUI tool | The tool does not read `~/.ssh/config` | Use the full host, user and key in the tool |

## Real-world scenario

You manage a web server, a database host that has no public address, and a staging box. Typing the full command each time led to mistakes, once with the wrong key.

You write three entries in `~/.ssh/config`: `web`, `staging`, and `db` with `ProxyJump web`. A `Host *` block at the bottom sets `ServerAliveInterval 60`. You run `chmod 600 ~/.ssh/config`.

Now `ssh web`, `ssh db` and `scp backup.sql db:/tmp/` all work. When something breaks you run `ssh -G db` and read the user, port and key it will use, which finds the typo in seconds.

## Quick reference

| Option | Example |
| --- | --- |
| `Host` | `Host myserver` |
| `HostName` | `HostName 203.0.113.10` |
| `User` | `User deploy` |
| `Port` | `Port 2222` |
| `IdentityFile` | `IdentityFile ~/.ssh/id_ed25519` |
| `IdentitiesOnly` | `IdentitiesOnly yes` |
| `ProxyJump` | `ProxyJump web` |
| Check | `ssh -G myserver` |

## Related lessons

- [Log in with SSH keys](https://levelrail.com/learn/server-basics/log-in-with-ssh-keys/)): create the key the entry points to
- [Copy files with scp and rsync](https://levelrail.com/learn/server-basics/copy-files-with-scp-and-rsync/)): both use your aliases
- [Linux file permissions explained](https://levelrail.com/learn/server-basics/linux-file-permissions-explained/)): why the config needs mode 600

## Next

You have finished the Server basics track. Continue with [Install Docker on Ubuntu](https://levelrail.com/learn/docker/install-docker-on-ubuntu/)).

## Frequently asked questions

### Does the config file work with scp and rsync?

Yes. They use SSH underneath, so scp file.txt myserver:/tmp/ and rsync -av ./dir/ myserver:/tmp/dir/ both use your aliases.

### Can I use one alias for several servers?

Use a pattern such as Host web-* for shared settings, and define each server with its own Host block for the HostName.

### Why do I get Too many authentication failures?

Your SSH agent offers every key it holds and the server gives up after a few. Add IdentitiesOnly yes to the host entry so SSH offers only the key named by IdentityFile.

### Do I put this file on the server?

No. It is the client config, read on the computer you connect from. The server's settings live in /etc/ssh/sshd_config, which is a different file.


## Sources

- [OpenSSH manual, ssh_config](https://man.openbsd.org/ssh_config)
