Skip to main content

ssh

Secure shell for remote login and tunneling.

Basics

  • Connect with key
ssh -i ~/.ssh/id_ed25519 user@host
  • Specify port and command
ssh -p 2222 user@host 'uname -a'

Key management

  • Generate key
ssh-keygen -t ed25519 -C "you@example.com"
  • Copy public key to server
ssh-copy-id -i ~/.ssh/id_ed25519.pub user@host

Config file (~/.ssh/config)

Host mybox
HostName example.com
User ubuntu
Port 22
IdentityFile ~/.ssh/id_ed25519
ForwardAgent yes
  • Use it
ssh mybox

Tunnels

  • Local port forward (access remote service locally)
ssh -L 5433:localhost:5432 user@db-host
  • Remote port forward (expose local to remote)
ssh -R 8080:localhost:3000 user@host
  • Dynamic SOCKS proxy
ssh -D 1080 user@host

Multiplexing (faster repeated SSH)

Host *
ControlMaster auto
ControlPath ~/.ssh/cm-%r@%h:%p
ControlPersist 10m

Common options

  • -o StrictHostKeyChecking=no # auto-accept host key (use with caution)
  • -A ForwardAgent # agent forwarding
  • -J jump-host # ProxyJump

SCP and SFTP

  • Copy files
scp file user@host:/path/
  • SFTP session
sftp user@host