SSH Cheat Sheet

A quick reference for SSH, connecting to remote hosts, generating and copying keys, transferring files with scp and rsync, local and remote port forwarding, and the SSH config file.

A quick reference to the SSH client and friends. Replace user, host, PORT, and KEY with your own values. Square brackets mark optional parts.

Connecting

CommandWhat it does
ssh user@hostOpen an interactive shell on the remote host
ssh -p PORT user@hostConnect on a non-default port
ssh -i KEY user@hostUse a specific private key file
ssh user@host "cmd"Run one command remotely and return
ssh -J jump user@hostConnect through a jump (bastion) host

Keys

CommandWhat it does
ssh-keygen -t ed25519Generate a modern Ed25519 key pair
ssh-keygen -t rsa -b 4096Generate a 4096-bit RSA key pair
ssh-copy-id user@hostInstall your public key on the remote host
ssh-keygen -lf KEYShow a key's fingerprint
ssh-keygen -R hostRemove a host from known_hosts (after a change)

Copying files

CommandWhat it does
scp FILE user@host:/pathCopy a local file to the remote host
scp user@host:/path FILECopy a remote file back here
scp -r DIR user@host:/pathCopy a whole directory recursively
rsync -avz DIR/ user@host:/path/Sync a folder, only sending changes
sftp user@hostOpen an interactive file transfer session

Port forwarding (tunnels)

CommandWhat it does
ssh -L 8080:localhost:80 user@hostLocal tunnel: reach a remote service from your machine
ssh -R 8080:localhost:80 user@hostRemote tunnel: expose a local service to the remote host
ssh -D 1080 user@hostDynamic SOCKS proxy through the remote host
ssh -L 8080:localhost:80 -N user@hostForward only, no shell (-N)

Config file (~/.ssh/config)

SnippetWhat it does
Host devDefine a short alias for a host block
HostName example.comThe real address the alias connects to
User deployDefault username for the alias
IdentityFile ~/.ssh/devPrivate key to use for the alias

Agent

CommandWhat it does
eval $(ssh-agent)Start the agent in the current shell
ssh-add KEYLoad a private key into the agent
ssh-add -lList keys currently held by the agent

Tip: define Host blocks in ~/.ssh/config so you can type ssh dev instead of ssh -i ~/.ssh/dev -p 2222 deploy@example.com. Lock the server down by disabling password login once keys work.

What this does

An SSH cheat sheet covers connecting to remote hosts, generating and copying keys, transferring files with scp and rsync, port forwarding, and the SSH config file.

How to use it

  1. Browse by section.
  2. Find the command you need.
  3. Copy it with one tap.
  4. Replace the host, user, and port.

Example

ssh user@example.com opens an interactive shell on the remote host.

Sources & methodology

Last updated .

Frequently asked questions

Key-based or password login — which is better?

Keys are stronger and more convenient. Generate a key pair with ssh-keygen, copy the public key with ssh-copy-id, then disable password login in the server config for better security.

Why do I get permission denied (publickey)?

Your private key is not being offered, usually because of wrong permissions. Run chmod 700 ~/.ssh and chmod 600 on the key file, and check that the public key is in the server’s authorized_keys file.

How do I keep a connection from dropping?

Add ServerAliveInterval 60 and ServerAliveCountMax 3 to your SSH config so the client sends a keepalive packet every minute and tolerates a few missed replies.