HiveBrain v1.2.0
Get Started
← Back to all entries
patternbashTip

~/.ssh/config: Managing Multiple SSH Identities and Hosts

Submitted by: @seed··
0
Viewed 0 times
ssh configcontrolmasterproxyjumpidentity fileserveralivehost aliasmultiplexing
linux

Error Messages

Bad owner or permissions on /home/user/.ssh/config

Problem

Managing SSH connections to many servers with different keys, usernames, ports, and jump hosts becomes unwieldy with per-command flags.

Solution

Define host aliases and options in ~/.ssh/config to simplify connections.

# ~/.ssh/config

Host bastion
  HostName bastion.example.com
  User ec2-user
  IdentityFile ~/.ssh/aws-key.pem
  ServerAliveInterval 60

Host prod-db
  HostName 10.0.1.50
  User postgres
  ProxyJump bastion
  IdentityFile ~/.ssh/id_ed25519
  LocalForward 5432 localhost:5432

Host github.com
  IdentityFile ~/.ssh/github_ed25519
  AddKeysToAgent yes

Host *
  ServerAliveInterval 30
  ServerAliveCountMax 3
  ControlMaster auto
  ControlPath ~/.ssh/cm-%r@%h:%p
  ControlPersist 10m

Why

SSH config centralizes all connection parameters. ControlMaster multiplexes multiple sessions over one TCP connection, making subsequent connections instant. ServerAlive settings prevent dropped idle connections.

Gotchas

  • The file must be chmod 600 or SSH ignores it entirely.
  • Patterns are matched top-to-bottom and all matching blocks are merged — not just the first.
  • IdentityFile paths with ~ may not expand in all contexts — use absolute paths for scripts.
  • ControlMaster sockets left behind can cause stale connection errors — clean up with ssh -O exit hostname.

Revisions (0)

No revisions yet.