skip to content

Why does OpenSSH's client ignore `User alice` in a Host block placed after a `Host *` block, and how do you prove which value won?

level: middleimportance: must knowfreq 46%

answer

  1. every matching block applies
  2. first value obtained wins
  3. specific blocks before Host *
  4. IdentityFile accumulates instead
  5. print the effective config

basics

~20 s

ssh keeps the first value it obtains for each directive, reading every matching block top to bottom, so a User set earlier under Host * cannot be overridden later. Put specific blocks first, Host * last, and check with ssh -G.

solid answer

~40 s

The OpenSSH client does not pick one best-matching block: it walks the command line, then `~/.ssh/config`, then `/etc/ssh/ssh_config`, applies **every** `Host` or `Match` section that matches, and for each directive keeps the **first value obtained**. A `Host *` block at the top that sets `User deploy` therefore fixes the user before the parser reaches `Host web1`, and its `User alice` is ignored. The manual's advice follows: host-specific blocks near the beginning, general defaults at the end. A few list-type directives such as `IdentityFile` add to a list instead of stopping at the first value. To see the outcome, `ssh -G web1` prints the configuration after evaluating Host and Match blocks, without connecting, so `ssh -G web1 | grep '^user '` shows which user won.

code

bash · 2 lines
bash
ssh -G web1 | grep -E '^(user|hostname|port|proxyjump) '
ssh -G -o User=alice web1 | grep '^user '

go deeper

for a junior

Recall that ssh keeps the first value it finds, so Host * belongs at the bottom of the file.

for a middle

Explain that every matching block applies, that list directives such as IdentityFile accumulate, and how ssh -G shows the result.

for a senior

Show how you debug a fleet config split across Include files by reading ssh -G output and tracing which earlier block matched first.

for a principal

Decide how a shared client config is layered and reviewed so that one team's Host * cannot silently pin values for everyone.

## The rule: first obtained value OpenSSH's `ssh_config(5)` states it directly: for each configuration directive, unless noted otherwise, **the first specified value is used**. The client reads its sources in this order: 1. options on the command line (`-l`, `-p`, `-i`, `-J`, any `-o Directive=value`); 2. the user file, `~/.ssh/config`; 3. the system file, `/etc/ssh/ssh_config`. Inside each file it goes top to bottom and applies **every** `Host` or `Match` section whose condition matches. It is not a search for the single best block; it is an accumulation in which an option, once set, stays set. That is why the manual says *more host-specific declarations should be given near the beginning of the file, and general defaults at the end*. ## The classic ordering bug ```ini Host * User deploy ServerAliveInterval 30 Host web1 HostName web1.prod.internal.example User alice ``` `ssh web1` matches both blocks. The parser reaches `Host *` first and obtains `User deploy`; when it later reaches `User alice` the option is already set, so that line has no effect. `HostName` is still taken from the web1 block, because `Host *` never set it. The fix is to move `Host *` to the end of the file. Engineers who expect "last match wins" or "most specific wins" find this surprising; both models are wrong for this file. ## Directives that accumulate Not every directive stops at its first value. The manual calls out that multiple `IdentityFile` directives **add to the list** of identities tried, which differs from other directives; `CertificateFile` behaves the same way, and forwarding declarations also add up. So an `IdentityFile` under `Host web1` plus another under `Host *` means both keys are candidates, in that order. | Directive kind | Example | Effect of a second matching value | |---|---|---| | single value | `User`, `Port`, `HostName`, `ProxyJump` | ignored, the first one stays | | list | `IdentityFile`, `CertificateFile` | appended to the list | ## Include and where it sits `Include` splices other files into the configuration at the point where the line appears; glob wildcards expand in lexical order, and relative paths are taken from `~/.ssh` (or `/etc/ssh` for the system file). Two consequences follow from the first-value rule: - An `Include` placed **after** a `Host` line belongs to that section and is read only when the section matches. Put unconditional includes at the very top of the file. - Included files are read where the `Include` line is, so an included `Host *` placed before your specific blocks wins over them, exactly as if it were written inline. Splitting a fleet into `Include conf.d/*.conf` files per environment is common; the ordering rule still applies across the merged result. ## Proving which value won: ssh -G `ssh -G destination` makes the client print its configuration **after evaluating Host and Match blocks** and exit, without opening a connection. Keys are printed in lower case, one per line, so a grep answers the question: ```bash ssh -G web1 | grep -E '^(user|hostname|port|proxyjump|identityfile) ' ``` This is the fastest way to settle an argument about precedence, to check what a teammate's `Include` actually contributes, and to confirm a change before pushing a shared config to a fleet. It shows the result, not the line that produced it, so when the value is surprising, look for an earlier block that matches the same name. ## A short debugging routine When a host connects with the wrong user, port, key or bastion: - run `ssh -G` for the exact name you typed, since a different spelling can match different blocks; - search the user file, every included file and the system file for the directive, top to bottom, and note the **first** matching block that sets it; - remember that command-line options and `-o` values beat both files, which is useful for testing a change with `ssh -G -o Directive=value host` before editing anything; - move the general block down, or narrow its pattern with a `!` exception, rather than repeating the value in every specific block.

  • If `/etc/ssh/ssh_config` sets `User admin` under `Host *` and `~/.ssh/config` sets nothing, which user does `ssh web1` use, and why can the system file not override a user file value?
    It uses `admin`, because nothing earlier set `User`. The system file is read after the user file, so any value already obtained from `~/.ssh/config` or the command line stays; the system file only fills gaps.
  • Why does an `Include` line placed below a `Host` line sometimes seem to do nothing?
    A `Host` section runs until the next `Host` or `Match` line, so an `Include` inside it is conditional and is read only when that section matches. Unconditional includes belong at the top of the file, before any `Host` or `Match` line.

  • The most specific Host block wins regardless of its position
  • The last matching value in the file overrides earlier ones
  • Only one Host block is applied per connection
  • A second IdentityFile line replaces the first one
  • ssh -G opens a connection to test the configuration