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.
| Task | Command |
|---|---|
| Connect | ssh deploy@203.0.113.10 |
| Connect on another port | ssh -p 2222 deploy@203.0.113.10 |
| Make a key | ssh-keygen -t ed25519 -C "you@laptop" |
| Put the key on a server | ssh-copy-id deploy@203.0.113.10 |
| Run one command | ssh deploy@web1 'uptime' |
| Upload a file | scp app.tar.gz deploy@web1:/tmp/ |
| Sync a folder | rsync -avz site/ deploy@web1:/var/www/site/ |
| Reach a remote port | ssh -N -L 5432:localhost:5432 deploy@db1 |
| See what goes wrong | ssh -v deploy@web1 |
| Close a frozen session | Enter, then ~. |
Save the commands from this page in Wardterm and run them in any session, or on several servers at once. Free to start.
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.
| Command | What it does |
|---|---|
ssh user@host | Opens a shell on the server. |
ssh -p 2222 user@host | Connects to a port other than 22. |
ssh -i ~/.ssh/id_ed25519_work user@host | Signs in with a specific key. |
ssh user@host 'df -h' | Runs one command and returns. |
ssh -t user@host top | Runs a full-screen program that needs a terminal. |
ssh -J jump@bastion user@10.0.1.5 | Goes through a jump host to a private server. |
ssh -o ServerAliveInterval=30 user@host | Sends a keepalive every 30 seconds so idle sessions stay open. |
exit or Ctrl+D | Leaves 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
| Command | What 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@host | Adds the public key to the server's authorized_keys. |
ssh-keygen -p -f ~/.ssh/id_ed25519 | Adds or changes the passphrase. |
ssh-keygen -y -f ~/.ssh/id_ed25519 > id_ed25519.pub | Rebuilds a lost public key from the private key. |
ssh-keygen -lf ~/.ssh/id_ed25519.pub | Shows the key's fingerprint. |
ssh-keygen -c -C "new comment" -f ~/.ssh/id_ed25519 | Changes the comment at the end of the key. |
ssh-keygen -t ed25519-sk | Makes 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 web1prints every setting SSH will use for that name, without connecting. -
ssh -F ./test-config -G web1does 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?
| Command | What 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@host | Opens 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
| Command | What it does |
|---|---|
ssh -N -L 5432:localhost:5432 user@db1 | Local: port 5432 on your computer reaches port 5432 on the server. |
ssh -N -L 8080:10.0.1.20:80 user@bastion | Local, to a third machine that only the server can reach. |
ssh -N -R 8080:localhost:3000 user@host | Remote: port 8080 on the server reaches port 3000 on your computer. |
ssh -N -D 1080 user@host | Dynamic: a SOCKS proxy on port 1080 that sends traffic out from the server. |
ssh -f -N -L 5432:localhost:5432 user@db1 | The 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.
In Wardterm, a saved tunnel shows whether it is Open or Failed, and starts or stops with one tap. Your LocalForward lines come in with the import.
How do you use ssh-agent?
The agent holds your unlocked keys, so you type a passphrase once instead of at every connection.
| Command | What it does |
|---|---|
eval "$(ssh-agent -s)" | Starts an agent in this shell (Linux; macOS starts one for you). |
ssh-add ~/.ssh/id_ed25519 | Adds a key to the agent. |
ssh-add --apple-use-keychain ~/.ssh/id_ed25519 | On macOS, also stores the passphrase in the Keychain. |
ssh-add -l | Lists the keys the agent holds. |
ssh-add -t 1h ~/.ssh/id_ed25519 | Adds a key for one hour only. |
ssh-add -D | Removes every key from the agent. |
ssh -A user@host | Forwards 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?
| Command | What it does |
|---|---|
ssh -v user@host | Verbose output: which keys are offered and where it stops. -vvv for more. |
ssh -G host | Shows the settings SSH will use, after your config. |
nc -vz host 22 | Checks that the port answers at all. |
ssh-keygen -R host | Removes an old host key from known_hosts after a reinstall. |
ssh-keygen -F host | Finds the saved host key for a name. |
ssh-keyscan -t ed25519 host | Fetches 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.
| Command | What it does |
|---|---|
sudo sshd -t | Checks sshd_config for errors. Run it before every restart. |
sudo sshd -T | grep -i passwordauthentication | Shows the setting sshd really uses. |
sudo systemctl restart ssh | Restarts the service after a change. |
sudo journalctl -u ssh -f | Follows the SSH log, including failed logins. |
ssh-keygen -lf /etc/ssh/ssh_host_ed25519_key.pub | Prints the host key fingerprint, to compare on first connect. |
chmod 700 ~/.ssh && chmod 600 ~/.ssh/authorized_keys | Fixes the permissions that make keys fail. |
last -n 10 | Lists 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.
Import your ssh config, save your commands and connect from Mac, Windows, Linux, iPhone, iPad or Android. Start free.
Originally published on the Wardterm blog.