Geavanceerd
Zoeken inHow-to-gidsen
Zo zoek je
  • Typ de taak in je eigen woorden (poort vinden, firewall openen, schijf vol) of plak een foutmelding tussen aanhalingstekens.
  • Kies "Snel antwoord" om alleen de commando's te zien, of een gebied om in één vakgebied te blijven.
  • "twee woorden" vindt precies die zin.
  • Gewone woorden vinden namen, teksten en voorbeelden.
  • ↑ ↓ bewegen · Enter openen · Esc sluiten · / zoeken
Probeer:

How-to 113System · SSH

How to manage many servers in the SSH config file

Typing ssh -i ~/.ssh/id_ed25519_prod -p 2222 deploy@203.0.113.45 every time is slow and error-prone. The client config file ~/.ssh/config gives every server a short name with its user, port, key and jump host — and scp, rsync, git and Ansible use it too. This how-to builds a clean config for many servers with patterns, a faster connection with multiplexing, and shows how to see what SSH really uses.

  • 5 Lessen
  • 5 Commando's

Dit deel is nog niet vertaald. Je ziet de Engelse tekst.

$ als gewone gebruiker# als root (of met sudo)

113.1

The quick answer

~/.ssh/config
Host web1
    HostName 203.0.113.45
    User deploy
    Port 2222
    IdentityFile ~/.ssh/id_ed25519_prod
    IdentitiesOnly yes
 
Host *
    ServerAliveInterval 60
chmod 600 ~/.ssh/config
ssh web1
scp report.pdf web1:/tmp/

113.2

Step by step

  1. One block per server or per group — Host is the short name you type; HostName is the real address.
  2. Patterns for groups — settings for many hosts at once:
~/.ssh/config
Host *.prod.example.com
    User deploy
    IdentityFile ~/.ssh/id_ed25519_prod
    ProxyJump bastion-prod
 
Host *.lab.example.com 192.168.56.*
    User alice
    StrictHostKeyChecking accept-new
  1. First match wins — for each setting, SSH uses the first value it finds. So put specific hosts at the top and Host * (defaults) at the end.
  2. The useful settings:
SettingWhy
IdentityFile + IdentitiesOnly yesUse exactly this key (no "Too many authentication failures").
ProxyJumpGo through a bastion.
ServerAliveInterval 60Keep idle sessions alive through firewalls/NAT.
ControlMaster auto + ControlPath + ControlPersist 10mReuse one connection: the 2nd ssh/scp to the same host starts instantly.
LocalForward 15432 db.internal:5432A tunnel every time you connect.
~/.ssh/config (end of file)
Host *
    ServerAliveInterval 60
    ControlMaster auto
    ControlPath ~/.ssh/cm-%r@%h:%p
    ControlPersist 10m
  1. Split big configs — one file per customer or environment:
~/.ssh/config (first line)
Include ~/.ssh/config.d/*.conf
  1. See what SSH will really use for a host (all settings after matching):
ssh -G web1 | grep -E '^(hostname|user|port|identityfile|proxyjump) '

113.3

Check that it worked

ssh web1 logs in without extra options, ssh -G web1 shows the right user, port, key and jump host, and with multiplexing a second ssh web1 opens instantly.

Commando's in deze les

113.4

Common problems

ProblemCauseFix
Bad owner or permissions on ~/.ssh/configThe file is writable by others.chmod 600 ~/.ssh/config.
A setting is ignoredAn earlier block already set it (first match wins).Move specific hosts up and Host * down; check with ssh -G.
Too many authentication failuresThe agent offers many keys.IdentitiesOnly yes with the right IdentityFile.
ControlPath too longLong host names in the socket path.ControlPath ~/.ssh/cm-%C (a short hash).
A stuck shared connectionThe master connection hangs.ssh -O exit web1, then connect again.
Ansible ignores the configAnsible uses its own SSH args.It reads ~/.ssh/config by default; check ansible.cfg ssh_args.

113.5

Go further

1Where must Host * defaults be in ~/.ssh/config, and why?

At the end — SSH takes the first value it finds for each setting.

2Which command shows the settings SSH will really use for a host?

Alle commando's in dit hoofdstuk