SSH Cheatsheet
Basic Connection
ssh user@hostname
- Connect with specific port
ssh -p <port> user@hostname
- Connect with specific identity file
ssh -i <private-key> user@hostname
- Connect with verbose output
ssh -v user@hostname
- Connect and execute command
ssh user@hostname 'command'
SSH Key Management
ssh-keygen -t rsa -b 4096 -C "your-email@example.com"
- Generate Ed25519 key (recommended)
ssh-keygen -t ed25519 -C "your-email@example.com"
- Copy public key to remote server
ssh-copy-id user@hostname
- Copy public key with specific key file
ssh-copy-id -i ~/.ssh/id_rsa.pub user@hostname
ssh-add ~/.ssh/id_rsa
ssh-add -l
- Remove all keys from ssh-agent
ssh-add -D
File Transfer
- Copy file to remote server
scp local-file user@hostname:/remote/path/
- Copy file from remote server
scp user@hostname:/remote/file /local/path/
- Copy directory recursively
scp -r local-directory user@hostname:/remote/path/
scp -P <port> local-file user@hostname:/remote/path/
- Sync directories with rsync
rsync -avz local-directory/ user@hostname:/remote/path/
- Sync with delete (mirror)
rsync -avz --delete local-directory/ user@hostname:/remote/path/
Port Forwarding and Tunneling
ssh -L <local-port>:<remote-host>:<remote-port> user@hostname
ssh -R <remote-port>:<local-host>:<local-port> user@hostname
- Dynamic port forwarding (SOCKS proxy)
ssh -D <local-port> user@hostname
ssh -f -N -L <local-port>:<remote-host>:<remote-port> user@hostname
ssh -o ServerAliveInterval=60 -o ServerAliveCountMax=3 user@hostname
SSH Configuration
vim ~/.ssh/config
Host myserver
HostName example.com
User username
Port 2222
IdentityFile ~/.ssh/id_rsa
ServerAliveInterval 60
- Connect using config alias
ssh myserver
ssh -T git@github.com
Security and Authentication
- Disable password authentication (server-side)
# In /etc/ssh/sshd_config
PasswordAuthentication no
PubkeyAuthentication yes
- Change SSH key passphrase
ssh-keygen -p -f ~/.ssh/id_rsa
ssh-keygen -lf ~/.ssh/id_rsa.pub
- Connect with specific cipher
ssh -c aes256-ctr user@hostname
- Disable host key checking (not recommended for production)
ssh -o StrictHostKeyChecking=no user@hostname
Session Management
- Run command in background
ssh user@hostname 'nohup command &'
- Keep session alive after disconnect
ssh user@hostname
screen -S session-name
# or
tmux new-session -s session-name
- Reconnect to screen session
ssh user@hostname
screen -r session-name
- Reconnect to tmux session
ssh user@hostname
tmux attach-session -t session-name
Troubleshooting
ssh -vvv user@hostname
systemctl status ssh
# or
service ssh status
tail -f /var/log/auth.log
# or
journalctl -u ssh -f
- Test specific SSH version
ssh -o Protocol=2 user@hostname
- Escape sequences (when connected)
~. # Disconnect
~^Z # Background SSH
~# # List forwarded connections
~? # Help
Common Use Cases
- Jump host / bastion server
ssh -J jump-host target-host
ssh -J host1,host2 target-host
- X11 forwarding for GUI applications
ssh -X user@hostname
ssh -C user@hostname
- Run local script on remote server
ssh user@hostname 'bash -s' < local-script.sh
- Mount remote filesystem with SSHFS
sshfs user@hostname:/remote/path /local/mount/point
Quick File Operations
- Edit remote file directly
ssh user@hostname 'vim /path/to/file'
ssh user@hostname 'mkdir -p /path/to/directory'
- Check disk space on remote server
ssh user@hostname 'df -h'
ssh user@hostname 'cat /path/to/file'
ssh user@hostname 'tail -f /var/log/application.log'