Cheatsheet: ssh

Last updated 2026-06-21

Connecting

Connect to a host as the current user

ssh example.com

Connect as a specific user

ssh deploy@example.com

Connect on a custom port

ssh -p 2222 deploy@example.com

Use a specific private key file

ssh -i ~/.ssh/id_ed25519 deploy@example.com

Run a remote command without opening an interactive shell

ssh deploy@example.com 'systemctl status nginx'

Keys and agent

Generate a modern Ed25519 key pair

ssh-keygen -t ed25519 -C "you@example.com"

Copy your public key to a server

ssh-copy-id deploy@example.com

Start the ssh-agent and add a key

eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_ed25519

List keys loaded in the agent

ssh-add -l

Configuration

Define a reusable host in ~/.ssh/config

Host prod
  HostName example.com
  User deploy
  Port 2222
  IdentityFile ~/.ssh/id_ed25519

Connect using a configured host alias

ssh prod

Use a jump host

ssh -J bastion.example.com deploy@internal.example.com

Enable agent forwarding for one connection

ssh -A deploy@example.com

Forwarding and copy

Forward a local port to a remote service

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

Forward a remote port back to your local machine

ssh -R 9000:localhost:3000 deploy@example.com

Create a SOCKS proxy

ssh -D 1080 deploy@example.com

Copy a local file to a remote host

scp ./app.tar.gz deploy@example.com:/srv/app/

Copy a remote file to the current directory

scp deploy@example.com:/var/log/app.log .

See also: