Every time you type ssh -i ~/.ssh/mykey.pem -p 2222 admin@192.168.1.50, you’re wasting keystrokes. The SSH config file exists to eliminate this friction.
Basic Host Aliases
Create or edit ~/.ssh/config:
Now just:
| |
Common Options
| Option | What it does |
|---|---|
Port | Non-standard SSH port |
IdentityFile | Which key to use |
ForwardAgent | Forward your SSH agent (for jumping through servers) |
ServerAliveInterval | Send keepalive every N seconds (prevents timeout) |
ServerAliveCountMax | Disconnect after N missed keepalives |
Wildcard Patterns
Apply settings to multiple hosts:
The second block is useful for ephemeral VMs where host keys change often.
Jump Hosts (ProxyJump)
Access internal servers through a bastion:
Now ssh internal-db automatically routes through the bastion. No manual tunneling.
For older SSH versions, use ProxyCommand:
Port Forwarding Shortcuts
Set up tunnels without remembering the syntax:
| |
Dynamic SOCKS Proxy
Route all traffic through an SSH server:
| |
Multiplexing (Connection Sharing)
Reuse connections for faster subsequent logins:
Create the socket directory:
| |
First ssh prod takes normal time. Subsequent connections are instant because they reuse the existing socket.
Per-Host Compression
Enable compression for slow connections:
Or disable for fast local networks where compression adds overhead:
Agent Forwarding (Careful!)
Forward your SSH keys to the remote server:
⚠️ Security warning: Only enable this for trusted servers. A compromised server with agent forwarding can use your keys.
Safer alternative — use ProxyJump instead of agent forwarding when possible.
Default Settings
Apply to all hosts with Host *:
IdentitiesOnly yes prevents SSH from trying every key in your agent — only uses explicitly specified keys.
Includes
Split config into multiple files:
Then organize:
Real Example
Here’s a practical config:
With this config:
ssh work-prodjumps through bastion automaticallyssh nasconnects to local NASssh aws-whateveruses the right user and key- All connections stay alive and multiplex
Debugging
See what SSH is actually doing:
| |
Check which config options apply:
| |
File Permissions
SSH is picky about permissions:
| |
If permissions are wrong, SSH will ignore your config or refuse to use keys.
Five minutes setting up ~/.ssh/config saves hours over a year. Every ssh -i key -p port user@host you don’t type is cognitive load you don’t spend.