Connect
ssh user@serverssh -p 2222 user@server # another portssh -i ~/.ssh/deploy_ed25519 user@serverssh -J bastion user@10.0.1.20 # through a jump hostssh user@server 'df -h /' # run one commandssh -t user@server 'sudo journalctl -f' # command that needs a terminalssh -v user@server # debug: -vv, -vvv for moreKeys
ssh-keygen -t ed25519 -C "anna@laptop" # new key pairssh-copy-id -i ~/.ssh/id_ed25519.pub user@serverssh-add ~/.ssh/id_ed25519 # load into the agentssh-add -l # keys in the agentssh-keygen -lf ~/.ssh/id_ed25519.pub # fingerprintssh-keygen -p -f ~/.ssh/id_ed25519 # change the passphrasessh-keygen -R server # forget a host key~/.ssh/config
Host web1 HostName 203.0.113.10 User deploy Port 2222 IdentityFile ~/.ssh/deploy_ed25519 IdentitiesOnly yes
Host 10.0.1.* ProxyJump bastion
Host * ServerAliveInterval 30Then just ssh web1. ssh -G web1 prints the settings that apply. The SSH Config Generator writes this file for you.
Tunnels
ssh -L 5432:localhost:5432 user@db # local 5432 → db's own 5432ssh -L 8080:10.0.1.5:80 user@bastion # local 8080 → a host behind bastionssh -R 9000:localhost:3000 user@server # server's 9000 → your 3000ssh -D 1080 user@server # SOCKS proxy on local 1080ssh -fN -L 5432:localhost:5432 user@db # in the background, no shellCopy files
scp file.txt user@server:/tmp/scp -r user@server:/var/log/app ./logsrsync -avz --progress ./site/ user@server:/var/www/site/rsync -avz -e 'ssh -p 2222' ./data/ user@server:/data/sftp user@serverWith zsh, quote remote patterns: scp 'server:/var/log/*.log' .
Permissions
chmod 700 ~/.sshchmod 600 ~/.ssh/id_ed25519 ~/.ssh/authorized_keys ~/.ssh/configchmod 644 ~/.ssh/id_ed25519.pubOn the server, the home folder must not be writable by others either.
The server
sudo sshd -t # test sshd_configsudo sshd -T | grep -i passwordauth # settings in effectsudo systemctl reload ssh # "sshd" on RHEL-based systemssudo journalctl -u ssh -f # login attempts and errorssudo ssh-keygen -A # recreate missing host keysKeep a second session open while you change sshd_config.
Escape sequences
Type them at the start of a line, after Enter:
| Keys | Does |
|---|---|
~. |
Close a frozen connection |
~C |
Command line: add a -L tunnel to the open session |
~? |
List them |
Common errors
Permission denied (publickey)REMOTE HOST IDENTIFICATION HAS CHANGEDToo many authentication failureskex_exchange_identification: Connection closedConnection timed outvsConnection refusedsshd: no hostkeys available