$ command-helper

SSH, scp and rsync

Connecting, keys, ~/.ssh/config, port forwarding, jump hosts and copying files.

Requires Google sign-in

Connecting

Connect to a server
ssh <user>@<host>
Connect on a non-standard port
ssh -p <port> <user>@<host>
Connect with a specific key
ssh -i ~/.ssh/<ssh_key> <user>@<host>
Run a single command remotely
ssh <user>@<host> 'uptime && df -h'
Debug connection problems
ssh -vvv <user>@<host>

Keys

Generate a key
ssh-keygen -t ed25519 -C "<email>"
Copy your public key to a server
ssh-copy-id <user>@<host>
Add a key to the agent
ssh-add ~/.ssh/<ssh_key>
Remove a host from known_hosts
ssh-keygen -R <host>

Needed after a server was reinstalled and the “host key changed” warning appears.

Key fingerprint
ssh-keygen -lf ~/.ssh/<ssh_key>.pub

Port forwarding and tunnels

Local forward: remote DB on localhost:5433
ssh -N -L 5433:localhost:5432 <user>@<host>

-N means no remote command, just the tunnel.

Forward to a host reachable only from the serveradvanced
ssh -N -L 5433:<internal_host>:5432 <user>@<host>
Remote forward: expose a local port on the serveradvanced
ssh -N -R 8080:localhost:3000 <user>@<host>
SOCKS proxy through the serveradvanced
ssh -N -D 1080 <user>@<host>
Connect through a jump host (bastion)advanced
ssh -J <user>@<bastion> <user>@<host>

~/.ssh/config

Host alias with key and jump hostadvanced
Host prod
  HostName <host>
  User <user>
  IdentityFile ~/.ssh/<ssh_key>
  ProxyJump <bastion>

# then just: ssh prod
Keep the connection aliveadvanced
Host *
  ServerAliveInterval 60
  ServerAliveCountMax 3

Copying files

Copy a file to a server
scp <file> <user>@<host>:/tmp/
Copy a file from a server
scp <user>@<host>:<remote_path> .
Sync a directory to a server
rsync -avz --progress <path>/ <user>@<host>:<remote_path>/

The trailing slash on the source means “contents of the directory”, not the directory itself.

Mirror a directory, deleting extra files (dry run)advanced
rsync -avz --delete --dry-run <path>/ <user>@<host>:<remote_path>/
rsync over a non-standard SSH portadvanced
rsync -avz -e 'ssh -p <port>' <path>/ <user>@<host>:<remote_path>/