Optimizing the .ssh/config File for Advanced SSH Functions
The .ssh/config file is a powerful tool for making SSH connections more efficient. With a bit of configuration, you can save time and tighten up security. Here are a few advanced optimizations I use.
1. Alias names for SSH hosts
Server addresses are often long and cumbersome. You can simplify them with alias names:
Host myserver
HostName 192.168.1.100
User myuser
Now, you can connect simply with ssh myserver instead of typing the full address.
2. Automatic authentication with SSH keys
If you use SSH keys, you can explicitly specify the path to your private key:
Host myserver
IdentityFile ~/.ssh/my_private_key
That way the correct key gets used for this connection, without you having to specify it every time.
3. Default values for multiple hosts
If you have many servers with similar settings, you can group them:
Host *.mycompany.local
User defaultuser
Port 2222
IdentityFile ~/.ssh/id_rsa_company
Any connection to a server within this domain will now automatically use this configuration.
4. Reuse connections with ControlMaster
If you frequently open multiple SSH connections to the same server, you can enable connection reuse:
Host *
ControlMaster auto
ControlPath ~/.ssh/sockets/%r@%h-%p
ControlPersist 10m
New connections to the same host reuse the existing socket instead of re-authenticating from scratch.
5. Keep connections alive
To prevent connection drops, you can enable KeepAlive:
Host *
ServerAliveInterval 60
ServerAliveCountMax 3
This pings the server every 60 seconds and drops the connection after 3 missed replies, instead of leaving you with a frozen session.
That's most of what I use day to day. If you've got other .ssh/config tricks, I'd like to hear about them.