SSH Cheatsheet
Basic Usage
Connect to a Remote Host
ssh username@hostname
Example:
ssh user@example.com
Connect with a Specific Port
ssh -p port_number username@hostname
Example:
ssh -p 2222 user@example.com
Run a Command on a Remote Host
ssh username@hostname command
Example:
ssh user@example.com 'ls -l /var/www'
Key-Based Authentication
Generate SSH Key Pair
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
- Add SSH Key to SSH-Agent
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_rsa
Copy Public Key to Remote Host
ssh-copy-id username@hostname
Example:
ssh-copy-id user@example.com
Configuration
Edit SSH Config File
- Path:
~/.ssh/config
- Path:
nano ~/.ssh/config
Example Config File:
Host example
HostName example.com
User user
Port 22
IdentityFile ~/.ssh/id_rsa
Connect Using Config Alias
ssh example
Port Forwarding
Local Port Forwarding
ssh -L local_port:destination_host:destination_port username@hostname
Example:
ssh -L 8080:localhost:80 user@example.com
Remote Port Forwarding
ssh -R remote_port:destination_host:destination_port username@hostname
Example:
ssh -R 8080:localhost:80 user@example.com
Dynamic Port Forwarding (SOCKS Proxy)
ssh -D local_port username@hostname
Example:
ssh -D 1080 user@example.com
File Transfer
SCP (Secure Copy)
- Copy Local to Remote
scp local_file username@hostname:/remote/directory
Example:
scp file.txt user@example.com:/home/user/
- Copy Remote to Local
scp username@hostname:/remote/file /local/directory
Example:
scp user@example.com:/home/user/file.txt /local/directory
SFTP (SSH File Transfer Protocol)
sftp username@hostname
- Common SFTP Commands
sftp> get remote_file
sftp> put local_file
sftp> ls
sftp> cd directory
sftp> pwd
sftp> exit
Tunneling and Proxying
SSH Tunnel for All Traffic
ssh -D 8080 -C -q -N username@hostname
- Use the tunnel as a SOCKS proxy in your browser or application.
ProxyCommand
- Use another SSH server as a proxy.
Host internal
HostName internal.example.com
User user
ProxyCommand ssh -q -W %h:%p gateway.example.com
Advanced Options
Enable X11 Forwarding
ssh -X username@hostname
Enable Compression
ssh -C username@hostname
Increase Verbosity
ssh -v username@hostname
Multiple Levels of Verbosity
ssh -vvv username@hostname
SSH Agent Forwarding
Enable Agent Forwarding
ssh -A username@hostname
Managing SSH Keys
List Loaded Keys
ssh-add -l
Remove All Loaded Keys
ssh-add -D
SSH Security
Disable Root Login
- Edit
/etc/ssh/sshd_config
on the remote server:
- Edit
PermitRootLogin no
Change Default SSH Port
- Edit
/etc/ssh/sshd_config
on the remote server:
- Edit
Port 2222
Restrict User Logins
- Edit
/etc/ssh/sshd_config
on the remote server:
- Edit
AllowUsers user1 user2
Additional Tips
Use SSH Escape Sequences
~.
: Disconnect~C
: Open command lineType escape sequences after pressing Enter.
SSHFS (SSH Filesystem)
- Mount a remote filesystem via SSH:
sshfs username@hostname:/remote/directory /local/mount/point
This cheatsheet covers essential commands and configurations for using SSH effectively, from basic connections to advanced tunneling and security practices. Adjust commands and paths according to your specific setup and needs.