SSH Commands

SSH client commands, config, and key management.

Connection

ssh user@host

Connect to remote host

ssh -p 2222 user@host

Connect using specific port

ssh -i ~/.ssh/key.pem user@host

Connect using private key file

ssh -v user@host

Connect with verbose output

ssh -X user@host

Connect with X11 forwarding

ssh -A user@host

Connect with agent forwarding

ssh -t user@host command

Execute command on remote host

Key Management

ssh-keygen -t rsa -b 4096

Generate 4096-bit RSA key pair

ssh-keygen -t ed25519

Generate Ed25519 key pair (recommended)

ssh-copy-id user@host

Copy public key to remote host

ssh-copy-id -i ~/.ssh/key.pub user@host

Copy specific public key

ssh-keygen -p -f ~/.ssh/id_rsa

Change passphrase of private key

ssh-keygen -y -f ~/.ssh/id_rsa

Show public key from private key

ssh-keygen -lf ~/.ssh/id_rsa.pub

Show fingerprint of public key

Port Forwarding

ssh -L 8080:localhost:80 user@host

Local port forwarding

ssh -R 8080:localhost:80 user@host

Remote port forwarding

ssh -D 1080 user@host

Dynamic SOCKS proxy on port 1080

ssh -L 3306:db.host:3306 user@jump

Forward MySQL through jump host

ssh -fN -L 8080:localhost:80 user@host

Port forward in background

File Transfer

scp file.txt user@host:/path

Copy file to remote host

scp user@host:/path/file.txt .

Copy file from remote host

scp -r directory user@host:/path

Copy directory recursively

scp -P 2222 file.txt user@host:/path

Copy using specific port

scp -i ~/.ssh/key.pem file user@host:

Copy using private key

rsync -avz -e ssh src/ user@host:dst/

Sync files over SSH

SSH Config

ssh -F /path/to/config user@host

Use custom config file

ssh -o "StrictHostKeyChecking=no" user@host

Disable host key checking

ssh -o "UserKnownHostsFile=/dev/null"

Do not save host key

ssh -o "ServerAliveInterval=60" user@host

Keep connection alive

ssh -o "ConnectTimeout=10" user@host

Set connection timeout

SSH Agent

eval $(ssh-agent)

Start SSH agent

ssh-add ~/.ssh/id_rsa

Add private key to agent

ssh-add -l

List keys in agent

ssh-add -D

Remove all keys from agent

ssh-add -t 3600 ~/.ssh/id_rsa

Add key with 1 hour timeout

ssh-agent -k

Kill SSH agent

Tunneling & Proxying

ssh -J jump@host user@target

Jump through intermediate host

ssh -L 2222:target:22 user@jump

Create SSH tunnel through jump host

ssh -N -f -L 5900:localhost:5900 user@host

VNC tunnel in background

ssh -w 0:0 user@host

Create VPN tunnel (tun device)

Troubleshooting

ssh -v user@host

Verbose mode (debug level 1)

ssh -vv user@host

More verbose (debug level 2)

ssh -vvv user@host

Most verbose (debug level 3)

ssh-keyscan -H host >> ~/.ssh/known_hosts

Add host key without connecting

ssh -o "LogLevel=DEBUG3" user@host

Maximum debug logging

ssh -Q cipher

List supported ciphers

ssh -Q kex

List supported key exchange algorithms

Advanced Options

ssh -C user@host

Enable compression

ssh -4 user@host

Force IPv4

ssh -6 user@host

Force IPv6

ssh -q user@host

Quiet mode

ssh -tt user@host command

Force pseudo-terminal allocation

ssh -o "ControlMaster=auto" user@host

Enable connection multiplexing