ssh is the OpenSSH client that ships with Linux, macOS, and Windows 10 and later. This sheet covers connecting, running remote commands, copying files, keys, the config file, tunnels, and the flags you reach for when a connection misbehaves.
Load a key into the agent the first time it is used
Trust new hosts on first use, still refuse changed keys
Split the file (goes at the top)
Print the settings that apply to a host after all matching blocks merge
Use a different config file
Port Forwarding
Syntax is , where the destination is named from the SSH server's point of view, so means the server itself.
Command
What it does
Local: your reaches port 80 on host
Local: reach a database that only host can see
Remote: port 9000 on host reaches port 3000 on your machine
SOCKS proxy on local port 1080; point a browser or at it
Tunnel only, no shell
Tunnel in the background
Let other machines on your network use the local forward
Several tunnels, repeat the flag
Stop background tunnels
Close a shared master connection when is on
For to accept connections from outside the server, must be set in the server's . Confirm the far end is actually listening with the SSH port checker before you blame the tunnel.
Agent
The agent holds decrypted keys so you type the passphrase once per login.
Command
What it does
Start an agent in this shell
Load the default keys, prompting for passphrases
Load a specific key
List loaded keys ( prints the public keys)
Unload one key ( unloads all)
Load a key that expires after an hour
macOS: remember the passphrase in Keychain
Forward the agent so the remote host can use your keys, for on a server
Windows PowerShell (as admin): turn on the built-in agent
ssh takes for the port, scp and sftp take . Lowercase on scp means preserve timestamps, and the copy quietly goes to port 22.
There is no password flag. If a script needs one, that is , and the better answer is a key plus .
A private key readable by anyone else is silently skipped. wants 700, keys and want 600, and the home directory must not be group-writable.
Single-quote remote commands. In double quotes your local shell expands and before ssh sends anything.
Closing the terminal kills whatever the session was running. Start long jobs inside tmux or under .
A background tunnel started with outlives the terminal. finds it when a port is unexpectedly busy.
On Windows, in PowerShell is OpenSSH and reads . PuTTY keys do not work with it; convert with the PPK to PEM converter, or stick with the PuTTY commands instead.
SSH Cheat Sheet FAQ
How do I run a command on a remote host over SSH without opening a shell?
Put the command after the host: ssh user@host 'uptime' runs it, prints the output, and disconnects. Quote the whole thing in single quotes so your local shell does not expand $ or * before it leaves your machine. Anything that needs a terminal, such as sudo asking for a password or an editor, needs -t: ssh -t user@host 'sudo systemctl restart nginx'. To run a local script remotely without copying it, use ssh user@host 'bash -s' < script.sh. The exit code you get back is the remote command's, so ssh user@host 'test -f /etc/nginx/nginx.conf' works as a check in scripts.
Can I pass a username and password in one ssh command?
Not with ssh itself. There is no password flag, on purpose: ssh reads the password from the terminal so it never lands in shell history or the process list. The username goes in the command as ssh user@host or ssh -l user host. If a script absolutely must supply a password, install sshpass and run sshpass -p 'secret' ssh user@host, or better, sshpass -f pwfile ssh user@host. The real fix is a key: ssh-keygen -t ed25519, then ssh-copy-id user@host, and the password prompt goes away for good.
Why does my ssh command fail with exit code 255?
255 means ssh itself failed before your command ran: it could not resolve the host, the connection was refused or timed out, or authentication failed. Any other code is passed through from the remote command, so exit 1 or 127 is your command's problem, not ssh's. Rerun with ssh -v to see which step broke; the message right before it gives up is the one to search. Connection refused, Permission denied (publickey), Host key verification failed, and Could not resolve hostname each have their own fix page linked in the Debugging section above.
How do I stop an SSH session from disconnecting when idle?
Add ServerAliveInterval 60 and ServerAliveCountMax 3 to ~/.ssh/config under Host *. The client then sends a probe every 60 seconds, which keeps NAT routers and firewalls from dropping the idle connection, and gives up only after three missed replies. For a single connection, pass -o ServerAliveInterval=60 on the command line. If the session still drops, for example on Wi-Fi or a laptop that sleeps, run long jobs inside tmux so they survive the disconnect and you can reattach with tmux a. The Broken pipe fix page covers the server-side settings too.
How do I remove a host from known_hosts?
ssh-keygen -R hostname deletes every entry for that name or IP from ~/.ssh/known_hosts and keeps a backup as known_hosts.old. Use the same spelling you connect with; a host known as both a name and an IP needs two runs, and a non-standard port is written as ssh-keygen -R '[hostname]:2222'. You need this after a server is reinstalled or replaced, because its host key changes and ssh refuses to connect with Host key verification failed. If nothing was reinstalled and the key still changed, ask whoever runs the server before you delete anything.