SSH commands cheat sheet: connect, keys, files, tunnels and debug

This SSH commands cheat sheet covers the commands people use every week: ssh user@host to connect, ssh-keygen -t ed25519 and ssh-copy-id for keys, scp and rsync for files, ssh -L, -R and -D for tunnels, and ssh -v when something fails. Every command below works with current OpenSSH on macOS, Linux and Windows. Put the servers you open often in ~/.ssh/config and most of these commands get shorter.

The SSH commands cheat sheet at a glance

The ten commands below cover most days. The sections after them group the rest by task, each with one line on what it does and a link to a full guide when you need more.

TaskCommand
Connectssh deploy@203.0.113.10
Connect on another portssh -p 2222 deploy@203.0.113.10
Make a keyssh-keygen -t ed25519 -C "you@laptop"
Put the key on a serverssh-copy-id deploy@203.0.113.10
Run one commandssh deploy@web1 'uptime'
Upload a filescp app.tar.gz deploy@web1:/tmp/
Sync a folderrsync -avz site/ deploy@web1:/var/www/site/
Reach a remote portssh -N -L 5432:localhost:5432 deploy@db1
See what goes wrongssh -v deploy@web1
Close a frozen sessionEnter, then ~.

How do you connect to a server with SSH?

The address can be an IP or a host name. If you leave out the user, SSH uses your local user name, which is rarely what the server expects.

CommandWhat it does
ssh user@hostOpens a shell on the server.
ssh -p 2222 user@hostConnects to a port other than 22.
ssh -i ~/.ssh/id_ed25519_work user@hostSigns in with a specific key.
ssh user@host 'df -h'Runs one command and returns.
ssh -t user@host topRuns a full-screen program that needs a terminal.
ssh -J jump@bastion user@10.0.1.5Goes through a jump host to a private server.
ssh -o ServerAliveInterval=30 user@hostSends a keepalive every 30 seconds so idle sessions stay open.
exit or Ctrl+DLeaves the session.

Quote a remote command in single quotes so that your local shell does not expand $HOME or * before it reaches the server. More in how to run a command over SSH and ProxyJump and jump hosts.

SSH key commands: ssh-keygen and ssh-copy-id

CommandWhat it does
ssh-keygen -t ed25519 -C "you@laptop"Makes a new Ed25519 key pair in ~/.ssh.
ssh-copy-id -i ~/.ssh/id_ed25519.pub user@hostAdds the public key to the server's authorized_keys.
ssh-keygen -p -f ~/.ssh/id_ed25519Adds or changes the passphrase.
ssh-keygen -y -f ~/.ssh/id_ed25519 > id_ed25519.pubRebuilds a lost public key from the private key.
ssh-keygen -lf ~/.ssh/id_ed25519.pubShows the key's fingerprint.
ssh-keygen -c -C "new comment" -f ~/.ssh/id_ed25519Changes the comment at the end of the key.
ssh-keygen -t ed25519-skMakes a key on a FIDO2 security key, such as a YubiKey.

The private key never leaves your computer: only the .pub file goes to servers. On the server, keep ~/.ssh at 700 and authorized_keys at 600: if others can write to them, sshd ignores the key. Step by step: generate an Ed25519 key.

Which SSH config commands save the most typing?

Every flag in the sections above can live in ~/.ssh/config, under a short name:

Host web1
    HostName 203.0.113.10
    User deploy
    Port 2222
    IdentityFile ~/.ssh/id_ed25519_work

Now ssh web1, scp file web1: and rsync ... web1: all use those settings. Two commands help when a block does not behave:

  • ssh -G web1 prints every setting SSH will use for that name, without connecting.
  • ssh -F ./test-config -G web1 does the same for a file you are still editing.

Ready-made blocks for GitHub accounts, jump hosts and keepalive are in SSH config examples, and the rules behind them in the SSH config file guide.

How do you copy files over SSH?

CommandWhat it does
scp report.pdf user@host:/tmp/Uploads a file.
scp user@host:/var/log/syslog .Downloads a file to the current folder.
scp -r site/ user@host:~/Copies a folder.
rsync -avz --progress site/ user@host:/var/www/site/Sends only what changed, with progress.
rsync -avz --delete site/ user@host:/var/www/site/Also removes files that are gone locally. Try -n first for a dry run.
sftp user@hostOpens an interactive session with ls, get and put.

Since OpenSSH 9.0, scp uses the SFTP protocol underneath; scp -O brings back the old one for servers that need it. The trailing slash matters in rsync: site/ copies the contents, site copies the folder itself. See scp examples and rsync over SSH.

When you would rather drag files than type paths, the Files panel in Wardterm opens the same server over SFTP, with two panes on Mac and an editor that keeps earlier versions of each file.

SSH port forwarding commands: -L, -R and -D

CommandWhat it does
ssh -N -L 5432:localhost:5432 user@db1Local: port 5432 on your computer reaches port 5432 on the server.
ssh -N -L 8080:10.0.1.20:80 user@bastionLocal, to a third machine that only the server can reach.
ssh -N -R 8080:localhost:3000 user@hostRemote: port 8080 on the server reaches port 3000 on your computer.
ssh -N -D 1080 user@hostDynamic: a SOCKS proxy on port 1080 that sends traffic out from the server.
ssh -f -N -L 5432:localhost:5432 user@db1The same local tunnel, in the background.

-N means no shell, only the tunnel. If a port is already taken, add -o ExitOnForwardFailure=yes so SSH stops instead of opening a session without the tunnel. All three kinds are explained in SSH tunnels: local, remote and dynamic.

How do you use ssh-agent?

The agent holds your unlocked keys, so you type a passphrase once instead of at every connection.

CommandWhat it does
eval "$(ssh-agent -s)"Starts an agent in this shell (Linux; macOS starts one for you).
ssh-add ~/.ssh/id_ed25519Adds a key to the agent.
ssh-add --apple-use-keychain ~/.ssh/id_ed25519On macOS, also stores the passphrase in the Keychain.
ssh-add -lLists the keys the agent holds.
ssh-add -t 1h ~/.ssh/id_ed25519Adds a key for one hour only.
ssh-add -DRemoves every key from the agent.
ssh -A user@hostForwards the agent to the server, so it can use your keys for the next hop.

Forward the agent only to servers you trust: anyone with root there can use your keys while you are connected. To reach a server behind another, -J is the safer choice. The macOS details are in ssh-agent and the macOS Keychain.

How do you debug an SSH connection?

CommandWhat it does
ssh -v user@hostVerbose output: which keys are offered and where it stops. -vvv for more.
ssh -G hostShows the settings SSH will use, after your config.
nc -vz host 22Checks that the port answers at all.
ssh-keygen -R hostRemoves an old host key from known_hosts after a reinstall.
ssh-keygen -F hostFinds the saved host key for a name.
ssh-keyscan -t ed25519 hostFetches the host key the server presents now.
Enter, then ~.Closes a frozen session.

Only remove a host key when you know why it changed, such as a rebuilt server. Otherwise compare it with the fingerprint from the server's console first. The full method, from DNS to the server log, is in how to debug an SSH connection.

Server side: the sshd commands to know

These run on the server, usually with sudo. On Debian and Ubuntu the service is ssh; on Fedora, RHEL and most others it is sshd.

CommandWhat it does
sudo sshd -tChecks sshd_config for errors. Run it before every restart.
sudo sshd -T | grep -i passwordauthenticationShows the setting sshd really uses.
sudo systemctl restart sshRestarts the service after a change.
sudo journalctl -u ssh -fFollows the SSH log, including failed logins.
ssh-keygen -lf /etc/ssh/ssh_host_ed25519_key.pubPrints the host key fingerprint, to compare on first connect.
chmod 700 ~/.ssh && chmod 600 ~/.ssh/authorized_keysFixes the permissions that make keys fail.
last -n 10Lists the latest logins.

Keep a second session open while you change sshd_config: if the new settings lock you out, the open one still works. For the settings worth changing, see how to harden sshd_config.

Turn your SSH commands cheat sheet into saved commands

A cheat sheet helps until you notice you type the same five lines every week. In Wardterm, saved commands keep them by name, ready on every server. A tap writes a command on the line or runs it, and Run on several servers shows the exit code and output of each server.

The rest of this page has a home in the app too. Import turns your ~/.ssh/config into servers, with keys, jump hosts and tunnels.

Questions

What is the basic SSH command to connect to a server?

ssh user@host, for example ssh deploy@203.0.113.10. Add -p 2222 for another port and -i ~/.ssh/key for a specific key. The first time, SSH shows the server's fingerprint and asks you to confirm it.

How do I run one command over SSH without opening a shell?

Put it after the host: ssh deploy@web1 'df -h'. The output comes back to your terminal and the connection closes. Add -t for programs that need a real terminal, such as top or sudo with a password.

What is the ssh-keygen command for a new key?

ssh-keygen -t ed25519 -C "you@laptop". It writes the private key to ~/.ssh/id_ed25519 and the public key to ~/.ssh/id_ed25519.pub. Then ssh-copy-id puts the public key on the server.

How do I see SSH verbose debug output?

Add -v: ssh -v user@host. Use -vv or -vvv for more detail. The lines about which keys were offered and why the server refused them usually show the problem.

What is the SSH port forwarding command to reach a remote database?

ssh -N -L 5432:localhost:5432 user@db-server. Port 5432 on your computer then leads to port 5432 on the server, and -N keeps the connection open without a shell.

How do I close a frozen SSH session?

Press Enter, then type ~. (tilde, then a dot). SSH closes the connection at once, even when the server no longer answers. ~? lists the other escape sequences.

What are good scp command examples?

scp file.txt user@host:/tmp/ uploads a file, scp user@host:/var/log/syslog . downloads one, and scp -r folder user@host:~/ copies a folder. For large or repeated copies, rsync -avz sends only what changed.

Do these SSH commands work on Windows?

Yes. Windows 10 and 11 include OpenSSH, so ssh, scp, sftp and ssh-keygen work in PowerShell. ssh-copy-id and rsync are not part of it: copy the public key by hand, or use WSL.

Sources

Each page was read on the date shown. Prices and features change, so check the maker's page before you decide.

  1. ssh(1), OpenBSD manual pages, read on
  2. ssh-keygen(1), OpenBSD manual pages, read on
  3. ssh_config(5), OpenBSD manual pages, read on
  4. sshd(8), OpenBSD manual pages, read on
  5. OpenSSH 9.0 release notes (scp uses SFTP), read on
  6. Wardterm docs, saved commands, read on
  7. Wardterm docs, port forwarding, read on

All posts