Console9

How to use SSH config for multiple servers

Define connection shortcuts for multiple servers with custom ports, usernames, and keys in the SSH config file.

Create named aliases for SSH connections by defining hostnames, ports, usernames, and identity files in the ~/.ssh/config file.

Prerequisites

  • SSH client installed
  • Terminal access
  • At least one remote server to configure

Step-by-Step: Use SSH Config for Multiple Servers

1. Create the SSH Config File

The SSH config file lives at ~/.ssh/config. Create it if it does not exist:

touch ~/.ssh/config
chmod 600 ~/.ssh/config

The SSH client reads this file before every connection. The file permissions must be 600 (owner read/write only) — SSH refuses to use a config file with lax permissions.

2. Add a Server Entry to the SSH Config File

Each entry in the SSH config file defines a connection alias with its parameters:

Host production
    HostName 203.0.113.50
    User admin
    Port 2222
    IdentityFile ~/.ssh/prod_key

The Host directive defines the alias name. The HostName sets the actual server IP or domain. The User sets the login username. The Port overrides the default SSH port 22. The IdentityFile specifies which private key to use.

3. Add Multiple Server Entries

The SSH config file supports unlimited entries. Add separate blocks for each server:

Host staging
    HostName staging.example.com
    User deploy
    IdentityFile ~/.ssh/staging_key

Host database
    HostName 10.0.1.50
    User dbadmin
    ProxyJump production

The ProxyJump directive routes the SSH connection through another host — SSH connects to production first, then jumps to database. This eliminates the need for manual two-step SSH connections through bastion hosts.

4. Connect Using the SSH Config Alias

SSH resolves the alias and applies all configured parameters automatically:

ssh production

The SSH client expands this to ssh -i ~/.ssh/prod_key -p 2222 admin@203.0.113.50 based on the config file entry.

How to Verify the SSH Config Works

Test the connection with verbose output to confirm SSH reads the config:

ssh -v production 2>&1 | grep "Reading configuration data"

Common Issues When Using SSH Config

SSH ignores the config file— The file permissions are too open. SSH requires ~/.ssh/config to be 600. Fix with chmod 600 ~/.ssh/config.

"Bad configuration option" error— The directive is misspelled or not supported by the installed SSH version. Check man ssh_config for supported options.