# Stop typing the full SSH command every time

> One Host block turns a long SSH command into two words.

Source: https://fullbytedeveloper.com/blog/ssh-config
Published here: 2026-11-19, updated 2026-12-03

Typing `ssh -i ~/.ssh/prod_ed25519 -p 2222 deploy@203.0.113.7` every deploy is how you mistype a port at 1 a.m. OpenSSH reads `~/.ssh/config` (or `-F`) and expands a short `Host` alias into hostname, user, port, and key before it dials [1][2]. One block turns that long line into `ssh prod`.

## Write a Host block you can trust

Create or open `~/.ssh/config`. The man page expects the file readable by you and not world-writable [1]. A minimal production alias:

```text
Host prod
  HostName 203.0.113.7
  User deploy
  Port 2222
  IdentityFile ~/.ssh/prod_ed25519
  IdentitiesOnly yes
```

`Host` is the name you type. `HostName` is the real target. `User` and `Port` replace the `@host` and `-p` pieces. `IdentityFile` points at the private key. `IdentitiesOnly yes` stops ssh from offering every key in your agent before the one you meant [1].

After that file exists, `ssh prod` is enough. Keep private keys mode `600` and the `.ssh` directory mode `700` so OpenSSH does not refuse the files for being too open [1][2].

## How SSH merges options (first value wins)

SSH walks the config top to bottom. For each keyword, the first matching value sticks; later matches for the same keyword are ignored [1]. Put specific Hosts above `Host *` so defaults only fill gaps.

```text
Host prod
  HostName 203.0.113.7
  User deploy
  Port 2222
  IdentityFile ~/.ssh/prod_ed25519
  IdentitiesOnly yes

Host *
  ServerAliveInterval 60
```

Here `prod` sets identity settings. `ServerAliveInterval` comes from `Host *` only because `prod` never set it.

Worked conflict: if `Host *` appears first with `Port 22`, and `Host prod` later sets `Port 2222`, `ssh -G prod` still reports `port 22`. The first value already won. Flip the blocks so `prod` is above `Host *`, then `-G` shows `2222`. That single reorder is the fix for most "my alias ignores Port" tickets [1].

<figure>
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 720 260" role="img" aria-label="SSH resolves Host prod top to bottom, then dials the HostName">
  <defs>
    <marker id="sshArrow" markerWidth="10" markerHeight="10" refX="8" refY="3" orient="auto">
      <path d="M0,0 L8,3 L0,6 Z" fill="#14b8a6"/>
    </marker>
  </defs>
  <rect x="0" y="0" width="720" height="260" rx="12" fill="#0b1f3a"/>
  <text x="24" y="36" fill="#f8fafc" font-family="ui-sans-serif, system-ui, sans-serif" font-size="16" font-weight="700">ssh prod</text>
  <path d="M120 30 H168" stroke="#14b8a6" stroke-width="3" fill="none" marker-end="url(#sshArrow)"/>
  <rect x="176" y="12" width="200" height="64" rx="8" fill="#123052" stroke="#14b8a6" stroke-width="2"/>
  <text x="188" y="40" fill="#f8fafc" font-family="ui-sans-serif, system-ui, sans-serif" font-size="14">1. Match Host prod</text>
  <text x="188" y="60" fill="#f59e0b" font-family="ui-sans-serif, system-ui, sans-serif" font-size="14">first values win</text>
  <path d="M376 44 H424" stroke="#14b8a6" stroke-width="3" fill="none" marker-end="url(#sshArrow)"/>
  <rect x="432" y="12" width="260" height="64" rx="8" fill="#123052" stroke="#64748b" stroke-width="2"/>
  <text x="444" y="40" fill="#f8fafc" font-family="ui-sans-serif, system-ui, sans-serif" font-size="14">2. Host * fills gaps only</text>
  <text x="444" y="60" fill="#cbd5e1" font-family="ui-sans-serif, system-ui, sans-serif" font-size="14">ServerAliveInterval 60</text>
  <path d="M276 76 V108" stroke="#14b8a6" stroke-width="3" fill="none" marker-end="url(#sshArrow)"/>
  <rect x="176" y="116" width="400" height="100" rx="8" fill="#123052" stroke="#f59e0b" stroke-width="2"/>
  <text x="188" y="148" fill="#f59e0b" font-family="ui-sans-serif, system-ui, sans-serif" font-size="14">Resolved dial plan</text>
  <text x="188" y="172" fill="#f8fafc" font-family="ui-monospace, monospace" font-size="14">deploy@203.0.113.7:2222</text>
  <text x="188" y="196" fill="#f8fafc" font-family="ui-monospace, monospace" font-size="14">IdentityFile ~/.ssh/prod_ed25519</text>
</svg>
</figure>

*First matching Host values stick; Host * only supplies keywords the specific block left unset.*

## Prove the resolve without opening a session

`ssh -G` prints the effective configuration for an alias and exits. Pair it with `-F` to test a scratch file [2].

```bash
mkdir -p /tmp/ssh-demo
cat > /tmp/ssh-demo/config <<'EOF'
Host prod
  HostName 203.0.113.7
  User deploy
  Port 2222
  IdentityFile /tmp/ssh-demo/prod_ed25519
  IdentitiesOnly yes

Host *
  ServerAliveInterval 60
EOF
ssh -F /tmp/ssh-demo/config -G prod | egrep '^(user|hostname|port|identityfile|identitiesonly|serveraliveinterval) '
```

Captured output:

```text
user deploy
hostname 203.0.113.7
port 2222
identitiesonly yes
serveraliveinterval 60
identityfile /tmp/ssh-demo/prod_ed25519
```

If `hostname` still shows the alias string, the `Host` / `HostName` pair never matched. Fix the file before you dig into network errors. Only then point `IdentityFile` at a real key and move the block into `~/.ssh/config`.

Command-line flags still override the file for that one invocation. `ssh -p 2200 prod` dials port 2200 even when the Host block says 2222 [2]. Use that for one-off experiments; put the permanent value in the file so scripts stop carrying four flags.

## Include is read in place

`Include` inserts other files at the point where the directive appears [1]. First value still wins across the combined stream. If your personal Hosts must beat a company-wide `Host *`, those Hosts have to appear before the Include that pulls the company file in (or the company file must not set the keywords you care about).

Personal aliases first, shared defaults after:

```text
Host laptop-tunnel
  HostName 127.0.0.1
  Port 2222
  User kamil

Include ~/.ssh/work.d/*.conf
```

Company defaults first (only when you want them to win on conflicts):

```text
Include ~/.ssh/work.d/*.conf

Host laptop-tunnel
  HostName 127.0.0.1
  Port 2222
  User kamil
```

The second form lets `work.d` set a keyword before `laptop-tunnel` can. Pick the order on purpose, then confirm with `ssh -G laptop-tunnel`.

A common laptop layout is personal Hosts, then `Include ~/.ssh/work.d/*.conf`, then a trailing `Host *` for keepalives you own. That way work wildcards fill gaps without stealing your `User` or `IdentityFile` on personal aliases [1].

## Match patterns without turning Host into a regex mess

`Host` accepts space-separated patterns, including `*` and `?` [1]. `Host *.internal prod` can share settings across names, but a later more-specific `Host prod` still cannot override a keyword the first match already set. When two patterns both match, remember first-value-wins across the whole file, not "most specific wins". If you need prod-only overrides, put the `Host prod` block above the broader pattern.

Negation with `!` excludes a name from a pattern [1]. `Host *.internal !prod.internal` applies shared settings to every internal box except `prod.internal`, which you then configure in its own earlier block. Pattern tricks fail quietly when you forget that order still decides each keyword.

## Patterns that pay rent on real boxes

**Jump hosts.** Put `ProxyJump` on the private host's block so the hop order lives with the alias [1].

```text
Host db-internal
  HostName 10.0.4.12
  User deploy
  ProxyJump bastion
```

Define `Host bastion` above or below as its own alias with its own key. `ssh db-internal` then opens the jump chain without a second command [1].

**Per-repo deploy keys.** Separate `IdentityFile` paths per Host, plus `IdentitiesOnly yes`, keep a GitHub deploy key from being offered to personal hosts [1].

**Keepalives.** `ServerAliveInterval` on `Host *` reduces idle NAT drops on sessions you still need [1]. Pair with `ServerAliveCountMax` when the path is especially lossy; the man page documents both [1].

**LocalForward.** A tunnel alias often adds `LocalForward 5432 127.0.0.1:5432`. After connect, your laptop's localhost port reaches the remote side. Confirm with `ssh -G db-tunnel` that `localforward` appears exactly once with the ports you meant, then open the session. Wrong order of bind address versus remote target is a common copy-paste bug; the `-G` dump shows the resolved forwarding line before you lock yourself out of a busy port [1].

**ControlMaster multiplexing.** For many short connections to the same host, `ControlMaster auto` with a `ControlPath` under `~/.ssh/` reuses one TCP session [1]. Start with one Host you trust, confirm sockets appear under that path, and remove stale sockets after a crash before blaming the server.

## Mistakes that waste an afternoon

1. Putting `Host *` above specific hosts so shared defaults win first and your `prod` `Port` never applies.
2. Omitting `IdentitiesOnly` while the agent holds five keys. Some servers disconnect after a few failed publickey attempts before your intended key is tried.
3. Using a relative `IdentityFile` path that depends on cwd. Prefer `~/.ssh/...` [1].
4. Testing with `ssh user@ip` while debugging an alias. That bypasses the Host block you meant to verify.
5. Putting `Include` at the top while claiming personal Hosts win first. Include is not deferred; order is the whole rule [1].
6. Leaving the config world-readable or the private key group-readable so OpenSSH refuses to use them [1][2].

## When the alias is enough

When `ssh -G prod` shows the hostname, user, port, and identity you expect, muscle-memory the short command. Keep the long form in a password manager only for machines that do not have your config yet. Verbose `ssh -v prod` is for the day `-G` looks right and the server still says `Permission denied (publickey)` [2].

Host blocks are not magic aliases only for humans. Scripts that call ssh prod inherit the same IdentityFile, Port, and User you typed once. When a deploy fails at 1 a.m., run ssh -G prod and grep hostname user port identityfile before you rewrite the command line. The resolve output is the dial plan OpenSSH will use [1]. Keep personal Host blocks above broad Host * fills so first-value wins stays predictable.

If GitHub rejects an old RSA key, [fix the SSH key type and move to ED25519](/blog/gh-ssh-rsa). Before the Host block helps, [generate and install an SSH key pair](/blog/ssh-keys-for-developers). For a database that should stay off the public internet, [open an SSH local forward instead of exposing the port](/blog/sshtunnel). After the alias connects, keep a [Vim quick start for remote boxes](/blog/vim-quick-start-remote-box) handy so a strange editor does not end the session.

Send this to the friend who pastes a four-flag ssh line into every deploy script.

Image: A long tangled command-wire being gathered into a short blank Host-block card, which a small two-part key token can trigger, opening a clean teal tunnel to a remote cube, rendered as unmarked geometric solids on a dark navy field with no typography or logos.
https://fullbytedeveloper.com/media/ssh-config/ssh-config-venice.webp
One host block can turn a long login command into a short alias you actually remember.

## Sources

1. [OpenBSD man: ssh_config](https://man.openbsd.org/ssh_config)
2. [OpenBSD man: ssh](https://man.openbsd.org/ssh)
