SSH Config Aliases (Pro Usage)#
This shows how to go from:
ssh -i ~/.ssh/prod_server_deploy deploy@server_ip
➡️ to:
ssh prod-server
Problem (without SSH config)#
ssh -i ~/.ssh/prod_server_deploy deploy@185.xxx.xxx.xxx
Solution: ~/.ssh/config#
SSH config acts as a shortcut + router for keys, users, and hosts.
A. Basic Alias Setup#
Edit config:
nano ~/.ssh/config
Add:
Host prod-server
HostName server_ip
User deploy
IdentityFile ~/.ssh/prod_server_deploy
Permissions:
chmod 600 ~/.ssh/config
Now connect using:
ssh prod-server
B. Multiple Servers (Real-world)#
# Production
Host prod-server
HostName prod_ip
User deploy
IdentityFile ~/.ssh/prod_server_deploy
# Staging
Host staging-server
HostName staging_ip
User deploy
IdentityFile ~/.ssh/staging_server_deploy
# Dev
Host dev-server
HostName dev_ip
User dev
IdentityFile ~/.ssh/dev_server_access
Usage:
ssh staging-server
C. Combine with GitHub / CI / Servers#
# GitHub Work
Host github.com-work
HostName github.com
User git
IdentityFile ~/.ssh/github_work
IdentitiesOnly yes
# Production Server
Host prod-server
HostName prod_ip
User deploy
IdentityFile ~/.ssh/prod_server_deploy
D. Advanced (Recommended Options)#
Host prod-server
HostName prod_ip
User deploy
IdentityFile ~/.ssh/prod_server_deploy
Port 22
IdentitiesOnly yes
ServerAliveInterval 60
ServerAliveCountMax 3
Naming Convention (Best Practice)#
Host <env>-<service>
Examples:
prod-serverstaging-serverclientA-prodclientB-staging