ubuntu, updated

ssh cheat sheet

Connect, copy files, use keys, the ~/.ssh/config file for short host names, jump hosts, port forwarding, and fixing common SSH problems.

Connect

WhatCommand
Connectssh anna@srv01.example.com
Other portssh -p 2222 anna@srv01
With a specific keyssh -i ~/.ssh/id_ed25519_work anna@srv01
Run one command and exitssh srv01 "df -h /"
Run a local script remotelyssh srv01 'bash -s' < check.sh
Verbose (debug the connection)ssh -v srv01
Hung session: disconnectpress Enter, then ~.

Keys

ssh-keygen -t ed25519 -C "anna@laptop"     # create a key pair (set a passphrase!)
ssh-copy-id anna@srv01                     # install the public key on the server
eval "$(ssh-agent -s)" && ssh-add          # type the passphrase once per session

The private key (id_ed25519) never leaves your machine. The public key (id_ed25519.pub) goes into ~/.ssh/authorized_keys on the server. Details: SSH Key Management.

~/.ssh/config: short names for everything

Instead of long command lines, describe each host once:

Host web01
    HostName 10.0.0.11
    User anna
    IdentityFile ~/.ssh/id_ed25519_work

Host db01
    HostName 10.0.0.21
    User anna
    ProxyJump bastion            # reach it through the jump host

Host bastion
    HostName bastion.example.com
    User anna
    Port 2222

Host *
    ServerAliveInterval 60       # keep idle connections alive
    AddKeysToAgent yes

Now ssh web01, scp file db01:/tmp/ and rsync ... web01: all use these settings. Set chmod 600 ~/.ssh/config.

Copy files

WhatCommand
Uploadscp file.tar.gz srv01:/tmp/
Downloadscp srv01:/var/log/syslog .
Whole folderscp -r ./site srv01:/srv/
Sync (only changes, resumable)rsync -avz --progress ./site/ srv01:/srv/site/
Interactivesftp srv01

Prefer rsync for anything big or repeated. Mind the trailing / on the source: site/ copies the contents, site the folder itself.

Jump hosts and tunnels

WhatCommand
Through a jump hostssh -J bastion db01
Local forward: reach a remote service on your machinessh -L 5432:localhost:5432 db01 then connect to localhost:5432
Local forward to a host behind the serverssh -L 8080:intranet.local:80 bastion
Remote forward: expose your local port on the serverssh -R 9000:localhost:3000 srv01
SOCKS proxy through the serverssh -D 1080 bastion
Tunnel only, no shelladd -N (and -f for background)

Server-side settings worth knowing

Put your own settings in /etc/ssh/sshd_config.d/ (for example 10-hardening.conf):

PasswordAuthentication no
PermitRootLogin no
AllowGroups ssh-users sudo

Check and apply, keeping your current session open:

sudo sshd -t && sudo systemctl reload ssh

Login problems: see Debugging login problems.

Common errors

MessageFix
REMOTE HOST IDENTIFICATION HAS CHANGEDServer reinstalled? Then ssh-keygen -R srv01. If not: stop and investigate.
Permission denied (publickey)Wrong user or key, or bad permissions on ~/.ssh (chmod 700, authorized_keys 600)
Connection refusedsshd not running or other port: systemctl status ssh, sudo ss -tlpn | grep ssh
Connection timed outFirewall or wrong IP
Too many authentication failuresAgent offers too many keys: use -i KEY -o IdentitiesOnly=yes
§ home
~/knubertus/home/
1,1 Top