___________ __________ _____ ________ ________ .____ ___________ \_ _____/ \______ \ / _ \ / _____/ / _____/ | | \_ _____/ | __) | _/ / /_\ \ / \ ___ / \ ___ | | | __)_ | \ | | \ / | \ \ \_\ \ \ \_\ \ | |___ | \ \___ / |____|_ / \____|__ / \______ / \______ / |_______ \ /_______ / \/ \/ \/ \/ \/ \/ \/

ssh Cheatsheet

← Back to cheatsheets

← Home


SSH (Secure Shell) is a protocol for secure remote login and command execution. Essential for managing remote servers, secure file transfer, and tunneling.


Basic Connection

  • ssh user@hostname - Connect to remote host
  • ssh user@hostname -p 2222 - Connect on custom port
  • ssh -i ~/.ssh/key.pem user@hostname - Use specific key file
  • ssh -v user@hostname - Verbose output (debugging)

Port Forwarding

  • ssh -L 8080:localhost:80 user@hostname - Local port forward
  • ssh -R 8080:localhost:80 user@hostname - Remote port forward
  • ssh -D 1080 user@hostname - Dynamic SOCKS proxy
  • ssh -L 8080:remote:80 -N user@hostname - Forward without shell

File Transfer

  • scp file.txt user@hostname:/path/ - Copy file to remote
  • scp user@hostname:/path/file.txt . - Copy file from remote
  • scp -r dir/ user@hostname:/path/ - Copy directory recursively
  • rsync -avz dir/ user@hostname:/path/ - Sync with rsync

Remote Execution

  • ssh user@hostname "command" - Execute command remotely
  • ssh user@hostname "cat file.txt" - Read remote file
  • cat file.txt | ssh user@hostname "cat > remote.txt" - Pipe to remote

Configuration

  • ~/.ssh/config - SSH client configuration file
  • Host alias - Define host alias
  • HostName real.hostname.com - Set hostname
  • User username - Set default user
  • IdentityFile ~/.ssh/key - Specify key file
  • Port 2222 - Set port

Common Examples

Basic Connection

ssh user@example.com

Connect to remote server.

Port Forwarding

ssh -L 8080:localhost:80 user@example.com

Forward local port 8080 to remote port 80.

Copy File

scp file.txt user@example.com:/home/user/

Copy file to remote server.

Execute Command

ssh user@example.com "ls -la"

Run command on remote server.

SSH Config

Host myserver
    HostName example.com
    User myuser
    IdentityFile ~/.ssh/id_ed25519
    Port 2222

Configure SSH alias in ~/.ssh/config.


Tips

  • Use key-based authentication instead of passwords
  • Configure ~/.ssh/config for easier connections
  • Use -N flag for port forwarding without shell
  • rsync is often faster than scp for large transfers
  • Use ControlMaster for connection multiplexing
  • Keep SSH keys secure and use passphrases
  • Disable password authentication on servers when possible