SSH Cheatsheet

The Config File

Use this SSH reference while you build software engineering projects, review code for technical interview prep, or polish examples for a software engineer resume.

Overview

~/.ssh/config lets you alias hosts and set per-host options so you type ssh prod instead of ssh -p 2222 -i ~/.ssh/deploy_key pablo@203.0.113.5.

chmod 600 ~/.ssh/config    # required — SSH ignores world-readable configs

Structure

# Global defaults — apply to every host
Host *
    ServerAliveInterval 60
    ServerAliveCountMax 3
    AddKeysToAgent yes
    IdentityFile ~/.ssh/id_ed25519

# A named alias
Host prod
    HostName 203.0.113.5
    User deploy
    Port 2222
    IdentityFile ~/.ssh/deploy_ed25519

# Wildcard alias
Host *.internal
    User ubuntu
    ProxyJump bastion

Rules are read top-to-bottom; the first matching value wins. Put specific hosts before Host *.

Common Directives

DirectiveDefaultWhat it does
HostName(alias)Real hostname or IP
User$USERRemote login name
Port22Remote port
IdentityFile~/.ssh/id_*Private key to offer
IdentitiesOnlynoOnly use listed identity files (don't try agent keys)
ProxyJumpComma-separated jump hosts
ProxyCommandArbitrary command to open the transport
ServerAliveInterval0Keepalive interval in seconds
ServerAliveCountMax3Max unanswered keepalives before disconnect
ForwardAgentnoForward local agent to remote
ForwardX11noForward X11 display
StrictHostKeyCheckingaskyes/no/ask/accept-new
UserKnownHostsFile~/.ssh/known_hostsWhere to store/check host keys
LogLevelINFOQUIET / INFO / VERBOSE / DEBUG
CompressionnoEnable zlib compression
ControlMasternoauto to enable multiplexing
ControlPathSocket path for mux (~/.ssh/cm-%r@%h:%p)
ControlPersistnoKeep master open (10m, yes)
AddKeysToAgentnoAuto-add key to ssh-agent on use
BatchModenoFail instead of prompting (scripts)
ConnectTimeout(OS)Seconds before giving up

Jump Host Example

Host bastion
    HostName bastion.example.com
    User ubuntu
    IdentityFile ~/.ssh/bastion_key

Host db
    HostName 10.0.1.50
    User postgres
    ProxyJump bastion
    IdentityFile ~/.ssh/db_key

Host *.internal
    ProxyJump bastion
    User ubuntu

Now ssh db automatically tunnels through bastion.

Multiplexing (Reuse Connections)

Host *
    ControlMaster auto
    ControlPath ~/.ssh/cm-%r@%h:%p
    ControlPersist 10m

After the first connection, subsequent ssh/scp/rsync to the same host reuse the socket.

Separate Known Hosts Per Host

Host staging
    HostName staging.example.com
    UserKnownHostsFile ~/.ssh/known_hosts_staging
    StrictHostKeyChecking accept-new

CI / Scripting Boilerplate

Host deploy-*
    StrictHostKeyChecking accept-new
    BatchMode yes
    ConnectTimeout 10
    IdentitiesOnly yes
    IdentityFile ~/.ssh/ci_deploy

Token Substitutions in Config Values

TokenExpands to
%hRemote hostname
%pRemote port
%rRemote username
%nOriginal hostname (before alias)
%uLocal username
%dLocal home directory
%iLocal user ID
ControlPath ~/.ssh/cm-%r@%h:%p     # typical use of tokens