How-to

SSH Commands You Will Use Every Week

OpenSSH is more than one command. These are the tools and flags worth knowing by heart when you run your own machines, grouped by the job they do.

Which SSH commands matter most?

Eight cover nearly everything: ssh to connect and run commands, ssh-keygen to create and inspect keys, ssh-copy-id to install a key, ssh-agent and ssh-add to hold unlocked keys, scp and sftp to move files, ssh-keyscan to collect host keys, and sshd itself for checking the server side. All come with the OpenSSH packages on every Linux image we install and with macOS and Windows 10 or later on your desk.

How do I connect and run things remotely?

The basic form is ssh user@host, with -p for a non-default port. A command after the host runs remotely and returns its output, which makes quick checks scriptable:

ssh -p 2207 ops@203.0.113.25 'uptime; free -g | sed -n 2p'
ssh ops@203.0.113.25 'sudo journalctl -u nginx --since today | tail -40' > nginx-today.txt
ssh -t ops@203.0.113.25 htop

-t forces a terminal, needed for full-screen tools such as htop or anything that prompts for a sudo password. Output redirected on your side, as in the second line, lands in a local file.

How do I make and manage keys?

ssh-keygen -t ed25519 -a 100 -C "ops@thinkpad" -f ~/.ssh/ops_ed25519
ssh-keygen -lf ~/.ssh/ops_ed25519.pub          # show the fingerprint
ssh-keygen -p -f ~/.ssh/ops_ed25519            # change the passphrase
ssh-keygen -R 203.0.113.25                     # forget a host after a reinstall

-a 100 raises the number of key derivation rounds that protect the passphrase, making offline guessing slower. The -R form is the one you need after reinstalling a machine from the panel, since its host keys change. Installing your public key on a server is a one-liner with ssh-copy-id -i ~/.ssh/ops_ed25519.pub ops@203.0.113.25; the full walk-through is in Log in with an SSH key.

How do I avoid typing my passphrase all day?

Load the key into the agent once per session:

eval "$(ssh-agent -s)"
ssh-add -t 8h ~/.ssh/ops_ed25519
ssh-add -l

-t 8h drops the key from memory after a working day. On macOS the agent integrates with the keychain; most Linux desktops start one for you automatically.

How do I copy files?

TaskCommand
Upload one filescp -P 2207 dump.sql.zst ops@203.0.113.25:/var/tmp/
Download a folderscp -r ops@203.0.113.25:/etc/nginx ./nginx-backup
Interactive browsingsftp -P 2207 ops@203.0.113.25
Sync only changesrsync -az --info=progress2 site/ ops@203.0.113.25:/srv/site/

Note the capital -P for the port in scp and sftp, unlike lowercase -p in ssh. For anything larger than a few files, rsync saves time and traffic; see Rsync over SSH.

How do I reach a service that only listens locally?

With port forwarding. Three kinds exist:

  • Local -L: ssh -N -L 5433:127.0.0.1:5432 ops@203.0.113.25 makes the server's PostgreSQL reachable at localhost:5433 on your computer.
  • Remote -R: ssh -N -R 9000:127.0.0.1:3000 ops@203.0.113.25 exposes your local dev app on the server's port 9000 loopback.
  • Dynamic -D: ssh -N -D 1080 ops@203.0.113.25 runs a SOCKS proxy through the server, handy for checking how a site looks from Amsterdam or Singapore.

-N means no remote command, just the tunnel. Admin panels that should never face the internet are best reached this way.

How do I hop through a bastion host?

ssh -J ops@bastion.example.net app@10.20.0.15 connects to the bastion, then from it to the target, with encryption end to end. Since each of our machines has a public address, bastions are mostly used when you firewall SSH on all machines but one.

How do I check host keys and server settings?

ssh-keyscan -t ed25519 203.0.113.25 >> ~/.ssh/known_hosts   # pre-trust after checking the fingerprint
sudo sshd -T | grep -E 'port|permitrootlogin|passwordauthentication'
sudo sshd -t && echo syntax-ok

sshd -T prints the effective server configuration after all drop-ins are merged, which settles any argument about which setting is really active.

Keep Reading

The SSH config file

Turn long ssh command lines into short aliases with ~/.ssh/config: hosts, ports, keys, jump hosts, keepalives and connection sharing.

Open

Log in with an SSH key

Create an ed25519 key, install it on your machine through the panel or ssh-copy-id, and log in without typing the root password.

Open

SFTP vs SSH

SFTP is a file transfer subsystem that rides inside SSH; learn when to hand someone SFTP only and how to jail them.

Open

Rsync over SSH

Copy and mirror folders between machines with rsync over SSH: trailing slashes, dry runs, custom ports, sudo on the far side and bandwidth caps.

Open

FAQ

Quick Answers

Put the command after the host, for example ssh ops@host 'df -h'. It runs remotely and prints the output locally.

In scp and sftp, lowercase -p already means preserve file times, so the port uses capital -P.

Run ssh-keygen -R with the host name or address. This is needed after a reinstall changes the host keys.

It opens the connection without running a remote command, which is how you keep a port forward open.

Run sudo sshd -T on the server. It prints the merged configuration from all files.