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
| What | Command |
|---|---|
| Connect | ssh anna@srv01.example.com |
| Other port | ssh -p 2222 anna@srv01 |
| With a specific key | ssh -i ~/.ssh/id_ed25519_work anna@srv01 |
| Run one command and exit | ssh srv01 "df -h /" |
| Run a local script remotely | ssh srv01 'bash -s' < check.sh |
| Verbose (debug the connection) | ssh -v srv01 |
| Hung session: disconnect | press 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
| What | Command |
|---|---|
| Upload | scp file.tar.gz srv01:/tmp/ |
| Download | scp srv01:/var/log/syslog . |
| Whole folder | scp -r ./site srv01:/srv/ |
| Sync (only changes, resumable) | rsync -avz --progress ./site/ srv01:/srv/site/ |
| Interactive | sftp 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
| What | Command |
|---|---|
| Through a jump host | ssh -J bastion db01 |
| Local forward: reach a remote service on your machine | ssh -L 5432:localhost:5432 db01 then connect to localhost:5432 |
| Local forward to a host behind the server | ssh -L 8080:intranet.local:80 bastion |
| Remote forward: expose your local port on the server | ssh -R 9000:localhost:3000 srv01 |
| SOCKS proxy through the server | ssh -D 1080 bastion |
| Tunnel only, no shell | add -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
| Message | Fix |
|---|---|
REMOTE HOST IDENTIFICATION HAS CHANGED | Server 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 refused | sshd not running or other port: systemctl status ssh, sudo ss -tlpn | grep ssh |
Connection timed out | Firewall or wrong IP |
Too many authentication failures | Agent offers too many keys: use -i KEY -o IdentitiesOnly=yes |