What I Wanted to Do

Managing multiple VPS and bastion servers meant typing long commands every time — ssh -i ~/.ssh/id_rsa ubuntu@203.0.113.10. Setting up ~/.ssh/config lets you connect with just ssh myserver.

Basic ~/.ssh/config Setup

Create the File

mkdir -p ~/.ssh
chmod 700 ~/.ssh
touch ~/.ssh/config
chmod 600 ~/.ssh/config

The config file must have 600 permissions — SSH ignores it otherwise.

Define a Host Alias

Add the following to ~/.ssh/config:

Host myserver
    HostName 203.0.113.10
    User ubuntu
    Port 22
    IdentityFile ~/.ssh/id_rsa

Now connect with a short command:

ssh myserver

Managing Multiple Servers

Host web
    HostName 203.0.113.10
    User ubuntu
    IdentityFile ~/.ssh/id_rsa

Host db
    HostName 203.0.113.20
    User ubuntu
    Port 2222
    IdentityFile ~/.ssh/id_db_rsa

Host github
    HostName github.com
    User git
    IdentityFile ~/.ssh/id_github_rsa

Now ssh web or ssh db is all you need.

Global Settings with Host *

Host *
    ServerAliveInterval 60
    ServerAliveCountMax 3
    AddKeysToAgent yes
  • ServerAliveInterval — sends keepalive packets to prevent dropped connections
  • AddKeysToAgent — automatically adds keys to the SSH agent

Bastion/Jump Host via ProxyJump

Host bastion
    HostName 203.0.113.1
    User ubuntu
    IdentityFile ~/.ssh/id_rsa

Host internal
    HostName 10.0.0.10
    User ubuntu
    IdentityFile ~/.ssh/id_rsa
    ProxyJump bastion
ssh internal
# Connects through bastion automatically

Common Pitfalls

  • Config file permissions must be 600644 causes SSH to silently ignore it
  • IdentityFile paths should start with ~ or be absolute paths
  • Host alias names are case-insensitive
  • Omitting Port defaults to port 22
  • Aliases work with scp and rsync too: scp myserver:/path/file .

Looking for reliable cloud infrastructure? Check out these developer-friendly services.