Basic Structure of ~/.ssh/config
CODE
Host <alias>
HostName <actual IP or domain>
User <username>
Port <port>
IdentityFile <path to key file>Create the file:
Bash
mkdir -p ~/.ssh
touch ~/.ssh/config
chmod 600 ~/.ssh/configRegistering Basic Servers
CODE
# ~/.ssh/config
# Production server
Host prod
HostName 203.0.113.10
User deploy
Port 2222
IdentityFile ~/.ssh/prod_rsa
# Development server
Host dev
HostName 203.0.113.20
User ubuntu
IdentityFile ~/.ssh/dev_rsa
# AWS instance
Host aws-web
HostName ec2-12-34-56-78.ap-northeast-2.compute.amazonaws.com
User ec2-user
IdentityFile ~/.ssh/my-key.pemBash
# Connect like this
ssh prod
ssh dev
ssh aws-webSetting Global Defaults
CODE
Host *
ServerAliveInterval 60 # keepalive every 60 seconds
ServerAliveCountMax 3 # disconnect after 3 unanswered keepalives
AddKeysToAgent yes # automatically add keys to ssh-agent
IdentityFile ~/.ssh/id_ed25519
StrictHostKeyChecking ask # prompt to confirm new hostsJump Host (Bastion/Jump Server)
Reach internal servers that are not directly accessible from outside by going through a jump host.
CODE
Host bastion
HostName 203.0.113.1
User admin
IdentityFile ~/.ssh/bastion_rsa
Host internal-db
HostName 10.0.1.50
User ubuntu
IdentityFile ~/.ssh/internal_rsa
ProxyJump bastion # via bastion
# Older OpenSSH:
# ProxyCommand ssh -W %h:%p bastionBash
ssh internal-db # automatically connects via bastionAutomatic Port Forwarding Setup
CODE
Host tunnel-db
HostName prod-server.example.com
User deploy
LocalForward 15432 localhost:5432 # local 15432 → server 5432
LocalForward 16379 localhost:6379 # local 16379 → server 6379Bash
ssh -N tunnel-db # port forwarding only (no shell)
# You can now connect to PostgreSQL at localhost:15432 from your local machinessh-agent — Automatic Key Management
Use ssh-agent to reuse keys without entering a passphrase each time.
Bash
# start the agent
eval "$(ssh-agent -s)"
# add keys
ssh-add ~/.ssh/id_ed25519
ssh-add ~/.ssh/prod_rsa
# list loaded keys
ssh-add -l
# remove all keys
ssh-add -D
# macOS: store in Keychain (persists across reboots)
ssh-add --apple-use-keychain ~/.ssh/id_ed25519Adding this to ~/.ssh/config loads keys automatically:
CODE
Host *
AddKeysToAgent yes
UseKeychain yes # macOS onlyFile Permissions (Important)
Bash
chmod 700 ~/.ssh
chmod 600 ~/.ssh/config
chmod 600 ~/.ssh/id_ed25519
chmod 644 ~/.ssh/id_ed25519.pub
chmod 600 ~/.ssh/authorized_keysIf permissions are too open, SSH will refuse to use the key files.
Editorial note
AI tools may assist drafting. Check the evidence in each guide for whether commands were run and which environment was used. Confirm the current docs for your version before applying instructions, and email us about errors. email us
Topic-related official docs (auto-linked · not per-claim sources)Linux man pages ↗
Questions & answers
Ask a question about this guide. We'll review and reply.