Typing ssh -i ~/.ssh/lab_key -p 2222 peter@192.168.1.50 gets old around the third time. The file at ~/.ssh/config turns that into ssh nas, and it does a great deal more: it can route a connection through a jump host to a machine with no direct route, and it can make repeat connections several times faster. It also has one rule that catches nearly everyone, and the order of the file is the whole of it. Everything below was tested with OpenSSH 9.6 (the version in Ubuntu 24.04) against two SSH servers in containers: a bastion reachable from the client and an internal machine reachable only through the bastion.
The basic shape
A host block
A config file is a list of Host sections, each followed by settings that apply when the name typed on the command line matches:
Host nas
HostName 192.168.1.50
User peter
Port 2222
IdentityFile ~/.ssh/lab_key
After that, ssh nas, scp file.txt nas: and rsync -a dir/ nas:backup/ all pick up the address, user, port and key. The alias is just a label; HostName holds the real address.
Checking what SSH will actually do
ssh -G <name> prints the final configuration for a host without connecting. It is the single most useful command for debugging a config file, and every precedence result below came from it.

First match wins, so order matters
The rule
The ssh_config(5) manual for Ubuntu 24.04 states it plainly: unless noted otherwise, the first value obtained for each parameter is the one used. Because of that, the manual says, more specific host declarations belong near the beginning of the file and general defaults at the end.
The test
Two files with identical lines in different orders:
# order1 # order2
Host * Host nas
User admin HostName 192.168.1.50
Port 22 User peter
Port 2222
Host nas
HostName 192.168.1.50 Host *
User peter User admin
Port 2222 Port 22
ssh -G nas returned:
| File | user | port | hostname |
|---|---|---|---|
order1 (Host * first) |
admin | 22 | 192.168.1.50 |
order2 (Host * last) |
peter | 2222 | 192.168.1.50 |
In the first file, Host * matched first and locked in User admin and Port 22. The nas block still contributed HostName, because nothing earlier had set it, but its user and port were ignored. Nothing errors. The connection simply goes to the wrong port as the wrong user, and the fix is moving one block to the bottom.
A distribution’s own defaults
The same Ubuntu manual notes that Debian’s packaging sets a few options in the system-wide /etc/ssh/ssh_config, which are not the upstream defaults: SendEnv LANG LC_*, HashKnownHosts yes and GSSAPIAuthentication yes, plus an Include of /etc/ssh/ssh_config.d/*.conf. The test confirmed the effect: with the system file skipped (-F /dev/null), ssh -G reported hashknownhosts no. The user’s own ~/.ssh/config is read first, so anything set there overrides these.
Patterns, negation and keys
Wildcards and exceptions
Host lines accept wildcards and negation. The ssh_config manual says that if a negated pattern matches, the whole Host entry is ignored no matter what else on the line matches, which makes negation useful for carving exceptions out of wildcards. A small test file:
Host web-* !web-test
User deploy
Host web-*
User tester
Host *.lab
IdentityFile ~/.ssh/lab_key
| Name typed | user | identity files offered |
|---|---|---|
| web-1 | deploy | the seven default key names |
| web-test | tester | the seven default key names |
| pi.lab | root (the local user) | only ~/.ssh/lab_key |
web-test skipped the first block because of the !web-test exception and fell through to the second.
Why IdentityFile matters
When no IdentityFile is set, ssh -G listed seven default paths, from ~/.ssh/id_rsa to ~/.ssh/id_dsa, and the client tries whichever exist. On a laptop with many keys, a server can refuse the connection after too many failed attempts before the right key is ever offered. Setting IdentityFile per host, together with IdentitiesOnly yes, makes the client offer only the named key.

ProxyJump: reaching a machine with no direct route
The lab
The internal server sat on a private container network. A firewall rule blocked the client from reaching it directly, and the attempt failed:
$ ssh -o ProxyJump=none internal hostname
ssh: connect to host 172.19.0.3 port 22: Connection timed out
The bastion had a foot in both networks. One line in the config fixed the problem:
Host bastion
HostName 172.17.0.2
User root
IdentityFile ~/.ssh/lab_key
Host internal
HostName 172.19.0.3
User root
IdentityFile ~/.ssh/lab_key
ProxyJump bastion
$ ssh internal 'hostname; echo $SSH_CONNECTION'
internal
172.19.0.2 55864 172.19.0.3 22
The SSH_CONNECTION variable on the internal server shows the connection arriving from 172.19.0.2, the bastion’s address on the private network.
What happens underneath
The manual describes ProxyJump as making an SSH connection to the jump host and then opening a TCP forward from there to the final target; several jump hosts can be chained with commas. Running with -v showed exactly that:
debug1: Setting implicit ProxyCommand from ProxyJump: ssh -v -W '[%h]:%p' bastion
debug1: Connecting to 172.17.0.2 [172.17.0.2] port 22.
Authenticated to 172.17.0.2 ([172.17.0.2]:22) using "publickey".
Authenticated to 172.19.0.3 (via proxy) using "publickey".
The client authenticated to the bastion, then authenticated to the internal machine through a tunnel. The private key never left the client. The bastion only passed encrypted bytes along, which is why ProxyJump is safer than copying keys onto a jump box or using agent forwarding.
The -J shortcut, and the catch
ssh -J bastion root@172.19.0.3 does the same from the command line. In the test it failed at first with “Permission denied (publickey,password)”: the bastion’s settings came from its config block, but the final address matched no block, so no key was specified for it. Adding -i ~/.ssh/lab_key fixed it. The manual adds a related warning: settings for the destination host are not generally applied to the jump host, so a jump host that needs its own user, port or key should get its own block in ~/.ssh/config.
ControlMaster: reusing one connection
The setting
Every new SSH connection repeats a key exchange and an authentication. ControlMaster keeps the first connection open and runs later sessions through it:
Host bastion
ControlMaster auto
ControlPath ~/.ssh/cm-%r@%h:%p
ControlPersist 60
ControlPersist 60 keeps the shared connection alive for 60 seconds after the last session closes. ssh -O check bastion confirmed a master was running, and ssh -O exit bastion closes it.
Measured
Ten connections in a row, each running the command true, on the container lab:
| Target | Without ControlMaster | With ControlMaster on the bastion |
|---|---|---|
| bastion | about 217 ms per connection | about 28 ms |
| internal, via ProxyJump | about 368 ms | about 190 ms |
Reuse made direct connections to the bastion nearly eight times faster. Through the jump, the bastion leg was reused but the internal server still needed its own handshake, so the gain was about half. Both servers were on the same machine as the client; over a real network with tens of milliseconds of round-trip time per exchange, the savings grow, because each avoided handshake costs several round trips.
The cost is a socket file that grants access to the open connection. It belongs in a directory only the user can read, which ~/.ssh already is.

Include files and Match blocks
Splitting a long config
Once a config file grows past a dozen hosts, Include keeps it readable. A test file pulled in every .conf file from a folder, then set a default user at the bottom:
Include ~/.ssh/config.d/*.conf
Host *
User admin
With a pi block (HostName 10.0.0.9, User pi) in one of the included files, ssh -G pi reported user pi. The included block counted as if it had been typed at the position of the Include line, so it came before the bottom default and won, exactly as the first-match rule predicts. An Include placed after Host * would lose the same way a host block would.
Match host checks the real address, not the alias
Match is a more flexible cousin of Host. It can test the user, the local user, whether a command succeeds, and more. It also hides one of the less obvious traps in the file. A test added this to the same config, intending to give the pi alias a different port:
Match host pi
Port 2200
ssh -G pi still reported port 22. The same manual explains why: the host criterion is matched against the target name after HostName has been substituted, so by the time Match ran, the name was 10.0.0.9, not pi. Changing the line to Match host 10.0.0.9 produced port 2200. So did Match originalhost pi, which the manual describes as matching the name exactly as it was typed on the command line.
A plain Host pi block does match the alias, which is why the difference surprises people. Anyone writing Match rules against aliases wants originalhost.
A starting config
Specific hosts first, defaults last
Host nas
HostName 192.168.1.50
User peter
Port 2222
IdentityFile ~/.ssh/lab_key
IdentitiesOnly yes
Host bastion
HostName bastion.example.com
User peter
ControlMaster auto
ControlPath ~/.ssh/cm-%r@%h:%p
ControlPersist 5m
Host *.internal
ProxyJump bastion
Host *
ServerAliveInterval 60
AddKeysToAgent yes
Running ssh -G against each name after any edit costs a second and shows the truth. The bottom block is the only place Host * belongs. Put it at the top and every carefully written block below it quietly loses the argument.
Sources
- ssh_config(5), OpenSSH client configuration file, Ubuntu 24.04 (noble) manual page: https://manpages.ubuntu.com/manpages/noble/man5/ssh_config.5.html
