SSH Commands Cheat Sheet

Common SSH commands for remote access, file transfer and tunnels


Connecting

ssh user@host                        # connect as a user
ssh -p 2222 user@host                # connect on a custom port
ssh -i /path/key.pem user@host       # connect with a key file
ssh -v user@host                     # verbose output (debugging)

Keys

ssh-keygen -t ed25519 -C "you@example.com"   # generate an ed25519 key
ssh-keygen -t rsa -b 4096                     # generate a 4096-bit RSA key
ssh-copy-id user@host                         # install your key on a host

File transfer

scp file.txt user@host:/path/        # copy a file up
scp user@host:/path/file.txt .       # copy a file down
scp -r folder user@host:/path/       # copy a directory
sftp user@host                       # interactive file transfer
rsync -avz folder/ user@host:/path/  # sync a directory

Port forwarding (tunnels)

ssh -L 8080:localhost:80 user@host    # local: 8080 on you -> 80 on host
ssh -R 9000:localhost:3000 user@host  # remote: expose your 3000 on host:9000
ssh -D 1080 user@host                 # SOCKS proxy through the host

Client config

# ~/.ssh/config
Host myserver
    HostName 203.0.113.10
    User deploy
    Port 2222
    IdentityFile ~/.ssh/myserver.pem