SSH & SCP

SSH & SCP commands cheatsheet.

Connect to servers, manage keys, copy files with scp and forward ports — the SSH commands worth knowing. Tap to copy.

Connecting
ssh user@hostConnect to a remote machine over SSH
ssh -p 2222 user@hostConnect on a non-default port
ssh -i ~/.ssh/key.pem user@hostConnect using a specific private key
ssh -v user@hostVerbose output for debugging connections
Keys & auth
ssh-keygen -t ed25519 -C "you@example.com"Generate a new SSH key pair
ssh-copy-id user@hostInstall your public key on a server
ssh-add ~/.ssh/id_ed25519Add a key to the ssh-agent
cat ~/.ssh/id_ed25519.pubPrint your public key to copy it
Copying files (scp)
scp file.txt user@host:/path/Copy a local file to a remote server
scp user@host:/path/file.txt .Copy a remote file to your machine
scp -r mydir user@host:/path/Copy a whole directory recursively
scp -P 2222 file user@host:~Copy over a non-default port (capital P)
Tunnels & config
ssh -L 8080:localhost:80 user@hostForward a local port to the remote side
ssh -D 1080 user@hostCreate a local SOCKS proxy
ssh -N -f user@hostOpen a tunnel in the background, no shell
nano ~/.ssh/configSave host aliases and options for reuse

Secure access in a few commands

SSH connects you to remote machines securely. ssh user@host opens a shell; key-based login (with ssh-keygen and ssh-copy-id) is more secure and convenient than passwords. To move files, scp works just like cp but across machines. And SSH's tunnelling — ssh -L to forward a port — is a quietly powerful way to reach services running on a remote network as if they were local.

FAQ

How do I set up passwordless SSH login?
Generate a key with ssh-keygen -t ed25519, then run ssh-copy-id user@host to install your public key on the server. After that, SSH uses the key instead of a password.
What's the difference between scp's -P and -p?
Capital -P sets the port; lowercase -p preserves file timestamps and permissions. (Note ssh uses lowercase -p for the port — scp is the odd one out.)

More cheatsheets