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
- SSH Hardening: Change the Port, Disable Root Login & Use SSH Keys
- How to Set Up a Bastion/Jump Host for Secure SSH Access
- How to Transfer Files To and From a VPS (SCP, SFTP & rsync)
Browse more articles in Linux Server Administration.