How to Manage Multiple SSH Sessions and Config Profiles

If you regularly connect to multiple servers, typing full SSH commands with IPs, usernames, and key paths every time gets tedious and error-prone. SSH's config file solves this cleanly.

The Problem

ssh -i ~/.ssh/server1_key -p 2222 [email protected]

Remembering and typing this for every server you manage is inefficient and easy to get wrong — especially with multiple servers using different ports, users, or keys.

Step 1 — Create or Edit Your SSH Config File

nano ~/.ssh/config

Step 2 — Define a Host Entry

Host myserver
    HostName 203.0.113.10
    User admin
    Port 2222
    IdentityFile ~/.ssh/server1_key

Step 3 — Connect Using the Short Name

ssh myserver

All the connection details are now handled automatically — a dramatic simplification for servers you connect to regularly.

Defining Multiple Servers

Host production
    HostName 203.0.113.10
    User deploy
    IdentityFile ~/.ssh/prod_key

Host staging
    HostName 203.0.113.20
    User deploy
    IdentityFile ~/.ssh/staging_key

Host bastion
    HostName 198.51.100.5
    User admin
    IdentityFile ~/.ssh/bastion_key

Using Wildcards for Shared Settings

Host *.mycompany.com
    User admin
    IdentityFile ~/.ssh/company_key

Host production staging
    ServerAliveInterval 60

Settings apply to any matching Host pattern; more specific entries later in the file can override earlier wildcard matches for shared settings.

Connecting Through a Bastion/Jump Host

Host internal-server
    HostName 10.0.0.10
    User deploy
    ProxyJump bastion

See How to Set Up a Bastion/Jump Host for Secure SSH Access for the broader architecture this config pattern supports.

Keeping Connections Alive (Avoiding Timeouts)

Host *
    ServerAliveInterval 60
    ServerAliveCountMax 3

Sends a keep-alive signal every 60 seconds, preventing idle connections from being dropped by network equipment or firewalls with aggressive timeout settings.

Using SSH Config with SCP and rsync Too

scp file.txt myserver:/remote/path/
rsync -avz /local/dir/ myserver:/remote/dir/

The named host entries work seamlessly with other SSH-based tools, not just the ssh command itself.

Organizing Config for Many Servers

Include ~/.ssh/config.d/*

For a large number of servers, splitting configuration into multiple files (one per project/client, for example) keeps the main config manageable.

Setting File Permissions Correctly

chmod 600 ~/.ssh/config

Common Errors

"Host key verification failed" after a server rebuild — the server's host key changed (expected after a rebuild); remove the old entry from ~/.ssh/known_hosts and reconnect to accept the new key.

Config settings seem ignored — verify Host pattern matching; a more specific match earlier in the file (or a typo in the Host name) is a common cause of settings not applying as expected.

Continue Reading

Browse more articles in Linux Server Administration.

  • ssh config file, multiple ssh servers, ssh host aliases, ssh proxyjump config
  • 0 Usuários acharam útil
Esta resposta lhe foi útil?

Artigos Relacionados

How to Update and Upgrade Your Ubuntu or Debian VPS

Keeping your VPS updated is one of the most important and simplest maintenance tasks — it...

How to Enable and Configure Swap on a Linux VPS (Ubuntu & Debian)

Swap is disk space Linux can use as overflow memory when physical RAM is exhausted. It won't make...

How to Manage Services with systemd and systemctl

systemd is the service manager used by Ubuntu, Debian, and most modern Linux distributions to...

How to Read and Analyze Linux Logs with journalctl

On modern Ubuntu and Debian systems, journalctl is the central tool for viewing system and...

How to Schedule Tasks with Cron on a Linux VPS

Cron is the standard Linux job scheduler, used to automate recurring tasks like backups, log...