Full Byte Developer.

Stop typing the full SSH command every time

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

Published here , updated · 2 sources

Typing ssh -i ~/.ssh/prod_ed25519 -p 2222 [email protected] 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].

ssh prod 1. Match Host prod first values win 2. Host * fills gaps only ServerAliveInterval 60 Resolved dial plan [email protected]:2222 IdentityFile ~/.ssh/prod_ed25519

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. Before the Host block helps, generate and install an SSH key pair. For a database that should stay off the public internet, open an SSH local forward instead of exposing the port. After the alias connects, keep a Vim quick start for remote boxes 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.

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.
One host block can turn a long login command into a short alias you actually remember.

Keep this guide

By Full Byte Developer

Filed under Dev tools

Questions about this guide

How do I check my config without connecting?

Run ssh -G <alias> with your config file. SSH prints the resolved options and exits without opening a session.

What is IdentitiesOnly for?

It tells SSH to try only the IdentityFile keys you listed, instead of offering every key loaded in the agent.

Which Host block wins when two match?

For each option, the first value found while reading the file top to bottom wins. Put specific Hosts above Host *.

Sources

  1. OpenBSD man: sshman.openbsd.org

Next in this stack