SSH commands cheat sheet

This cheat sheet brings together useful SSH commands for working with a server: connecting, creating keys, the config file, file transfer with scp, sftp, and rsync, tunnels, server security settings, and fixing common errors. You can search for a command, filter by topic, and copy it with one click.

Connecting to a server

  • ssh user@example.com

    Connects to the server as the user named user, on the default port, 22. On the first connection to a server, the fingerprint of the server's key is shown, and once you confirm it, it's saved in the known_hosts file.

  • ssh -p 2222 user@example.com

    Connects to a server where the SSH service listens on a port other than 22.

  • ssh ssh://user@example.com:2222

    The same connection written as a URI, with the user name, the server, and the port together.

  • ssh -i ~/.ssh/id_ed25519 user@example.com

    Connects with a specific private key when your computer has several keys.

  • ssh -v user@example.com

    Shows detailed messages about each stage of the connection, such as the keys tried and the authentication methods the server allows. You can repeat the option up to three times (-vvv) for maximum detail. This is the first tool to use when a connection fails.

  • ssh user@example.com 'df -h'

    Runs a single command on the server and shows its output on your computer, without opening a shell on the server.

  • ssh -t user@example.com top

    Runs a program that needs an interactive terminal, such as top or a text editor, when you run it directly from the ssh command.

  • ~.

    Disconnects an SSH session that's stuck and not responding. Press Enter, then the ~ character and a period. The ~? sequence lists all available escape sequences.

SSH keys

  • ssh-keygen -t ed25519 -C "user@laptop"

    Creates an Ed25519 key pair on your computer: a private key in ~/.ssh/id_ed25519 and a public key in id_ed25519.pub. The comment after -C helps you identify the key on the server. We recommend setting a passphrase for the key when the command asks for one.

  • ssh-keygen -t ed25519 -f ~/.ssh/id_ed25519_work -C "work"

    Creates a key with a different file name, so you can keep separate keys for different servers or clients.

  • ssh-keygen -t rsa -b 4096 -C "user@laptop"

    Creates a 4096-bit RSA key for older systems that don't support Ed25519. The default RSA key length is 3072 bits.

  • ssh-keygen -p -f ~/.ssh/id_ed25519

    Adds, changes, or removes the passphrase of an existing private key, without creating a new key.

  • ssh-keygen -l -f ~/.ssh/id_ed25519.pub

    Shows the fingerprint of the public key (SHA256), so you can compare it with the keys set up on the server.

    Example: 256 SHA256:abc123... user@laptop (ED25519)

  • ssh-keygen -y -f ~/.ssh/id_ed25519

    Derives the public key from the private key and prints it, when the .pub file is lost.

  • ssh-copy-id -i ~/.ssh/id_ed25519.pub user@example.com

    Adds the public key to the ~/.ssh/authorized_keys file on the server, and creates the directory and the file when they're missing. This connection uses your password, and from the next connection on you can log in with the key.

  • ssh-copy-id -p 2222 -i ~/.ssh/id_ed25519.pub user@example.com

    Adds the key on a server where the SSH service listens on another port. The -n option shows which keys would be added, without changing anything on the server.

  • cat ~/.ssh/id_ed25519.pub | ssh user@example.com 'mkdir -p ~/.ssh && chmod 700 ~/.ssh && cat >> ~/.ssh/authorized_keys && chmod 600 ~/.ssh/authorized_keys'

    Adds the public key to the server when the ssh-copy-id command isn't available, for example on Windows. In PowerShell, use type instead of cat, with the path in your user folder, for example type $env:USERPROFILE\.ssh\id_ed25519.pub.

  • chmod 700 ~/.ssh && chmod 600 ~/.ssh/authorized_keys

    Restricts the ~/.ssh directory and the authorized_keys file on the server to the owner only. When other users have write permission on the file, the directory, or the home directory, the SSH service refuses to use the keys in the file.

  • chmod 600 ~/.ssh/id_ed25519

    Restricts the private key on your computer to the owner only. The SSH client ignores a private key that other users can read.

ssh-agent: keeping keys in memory

  • eval `ssh-agent -s`

    Starts ssh-agent in the current terminal window. The agent keeps keys whose passphrase you've already entered in memory, so you don't need to type it on every connection. In many desktop environments, the agent is already running.

  • ssh-add ~/.ssh/id_ed25519

    Adds a key to the agent and asks for its passphrase once. Without a path, the command adds the key files with the default names.

  • ssh-add -t 1h ~/.ssh/id_ed25519

    Adds a key to the agent for one hour only, after which it's removed from memory.

  • ssh-add -l

    Lists the fingerprints of all keys currently in the agent.

  • ssh-add -D

    Removes all keys from the agent. The key files themselves aren't deleted.

  • ssh -A user@example.com

    Caution: forwards access to your local agent to the server, so you can connect onward from it with the keys on your computer. A user on the server with sufficient permissions can use the agent while you're connected. To go through an intermediate server, ssh -J is the better choice.

The ~/.ssh/config file

  • Host myserver

    Starts a settings block for a short name in the ~/.ssh/config file. The lines after it apply to that name, so you connect with a short command instead of typing the address, user, and port.

    Example: ssh myserver

  • HostName 203.0.113.10

    The real address or host name of the server that the short name connects to.

  • User deploy

    The user name to log in with, instead of typing user@ in every command.

  • Port 2222

    The port of the SSH service on the server. The default is 22.

  • IdentityFile ~/.ssh/id_ed25519_work

    The private key used to connect to this server.

  • IdentitiesOnly yes

    Limits the connection to the key set in IdentityFile, even when the agent holds other keys. Fixes the Too many authentication failures error.

  • ServerAliveInterval 60

    Sends the server a keepalive check after 60 seconds without traffic, which keeps an idle open connection from being dropped. After three checks without a reply (the ServerAliveCountMax default), the connection is closed.

  • ProxyJump bastion

    Connects to the server through an intermediate server, which can be another short name from the same file. Useful for servers on an internal network that you can't reach directly.

  • AddKeysToAgent yes

    Adds the key to ssh-agent the first time it's used, so you enter the key's passphrase only once.

  • Host *

    A block of general settings for all servers, such as ServerAliveInterval. For each setting, the first value found in the file wins, so Host * goes at the end of the file, after the blocks for specific servers.

  • ssh -G myserver

    Shows the settings that will actually apply when you connect to the short name, after all blocks are processed, without connecting to the server.

  • chmod 600 ~/.ssh/config

    Restricts the config file to the owner. The SSH client refuses to read a config file that other users can write to.

File transfer: scp, sftp, and rsync

  • scp backup.zip user@example.com:/home/user/

    Copies a file from your computer to a directory on the server.

  • scp user@example.com:/home/user/db.sql.gz .

    Downloads a file from the server to the current directory on your computer. The period at the end stands for the current directory.

  • scp -r ./site user@example.com:/home/user/

    Copies a whole directory with everything in it.

  • scp -P 2222 backup.zip user@example.com:/home/user/

    Copies to a server where the SSH service listens on another port. In scp and sftp, the P is uppercase, unlike in ssh.

  • scp -O backup.zip user@example.com:/home/user/

    In recent versions of OpenSSH, scp transfers files over the SFTP protocol. The -O option switches back to the legacy SCP protocol, for old servers that don't support SFTP.

  • sftp user@example.com

    Opens an interactive SFTP session for managing files on the server. The ls, cd, and pwd commands work on the server, and lls, lcd, and lpwd work on your computer. The exit command closes the session.

  • sftp -P 2222 user@example.com

    Opens an SFTP session to a server where the SSH service listens on another port.

  • put -r ./site

    Inside sftp: uploads a directory from your computer to the current directory on the server. Without -r, put uploads a single file, and the reput command resumes an interrupted upload.

  • get backup.tar.gz

    Inside sftp: downloads a file from the server to the current directory on your computer. get -r downloads a whole directory, and the reget command resumes an interrupted download.

  • rsync -avz ./site/ user@example.com:/var/www/site/

    Syncs a directory to the server over SSH and transfers only files that changed. -a preserves permissions, timestamps, and links, -v lists the files being transferred, and the -z option compresses data during the transfer. A trailing slash on the source directory copies its contents, and without the slash the directory itself is copied.

  • rsync -avzP -e 'ssh -p 2222' ./site/ user@example.com:/var/www/site/

    Syncs to a server where the SSH service listens on another port. The -P option shows the transfer progress and keeps partially transferred files, so an interrupted transfer can resume.

  • rsync -avz user@example.com:/var/www/site/ ./site-backup/

    Downloads a copy of a directory from the server to your computer, for example for a local backup. The next run transfers only the files that changed.

  • rsync -avz --delete --dry-run ./site/ user@example.com:/var/www/site/

    Caution: --delete removes every file at the destination that doesn't exist in the source. Run it first with --dry-run (or -n), which shows what would be deleted without changing anything, and only after reviewing the list run it again without --dry-run. Swapping the source and the destination by mistake deletes the right files.

Tunnels and port forwarding

  • ssh -L 3307:127.0.0.1:3306 user@example.com

    Local tunnel: port 3307 on your computer is forwarded through the encrypted connection to port 3306 on the server. This lets a program on your computer connect, at 127.0.0.1 on port 3307, to a database that listens only on the server's local address.

  • ssh -N -L 8080:127.0.0.1:80 user@example.com

    Opens a tunnel only, without a shell on the server. The -N option suits connections meant only for port forwarding. Close the tunnel with Ctrl+C.

  • ssh -f -N -L 3307:127.0.0.1:3306 user@example.com

    Opens the tunnel in the background and returns you to the command prompt. The tunnel stays open until the ssh process ends.

  • ssh -R 8080:127.0.0.1:3000 user@example.com

    Reverse tunnel: port 8080 on the server is forwarded to a service running on port 3000 on your local computer. By default, the server listens on the port only on its local address.

  • ssh -D 1080 -N user@example.com

    Starts a SOCKS server on port 1080 on your computer. A browser or a program set to use it as a proxy reaches the internet through the server, over an encrypted connection.

Jump hosts

  • ssh -J bastion.example.com user@10.0.0.5

    Connects to a server on an internal network through a jump host. The connection is encrypted all the way from your computer to the destination server, and the private key stays on your computer.

  • ssh -J admin@bastion.example.com:2222 user@10.0.0.5

    Sets the user and the port for the jump host. You can go through several jump hosts in a row, separated by commas.

  • scp -J bastion.example.com backup.zip user@10.0.0.5:/home/user/

    Copies a file to a server on an internal network through a jump host, the same way as ssh -J.

Host keys and known_hosts

  • ssh-keygen -R example.com

    Removes the server's saved keys from the known_hosts file. Use it after reinstalling the server, when the server's key has changed and you're sure the change is expected.

  • ssh-keygen -R '[example.com]:2222'

    Removes the saved key of a server that listens on a port other than 22. For other ports, the server is saved in known_hosts in the [host]:port format.

  • ssh-keygen -F example.com

    Searches the known_hosts file for the server and shows the matching lines, even when the file is hashed.

  • ssh-keygen -l -f /etc/ssh/ssh_host_ed25519_key.pub

    Run it on the server itself, for example through the management console, to see the fingerprint of the server's key and compare it with the one shown on the first connection.

  • ssh -o StrictHostKeyChecking=accept-new user@example.com

    Adds a new server's key to known_hosts without asking, but refuses to connect when the key of a known server has changed. Suitable for scripts. The value no turns off protection against server impersonation, so don't use it.

Server security settings (sshd_config)

  • sudo nano /etc/ssh/sshd_config

    Opens the SSH service's configuration file on the server for editing. Before you change it, keep an SSH connection open, and test the new settings in another window before you close that connection.

  • PasswordAuthentication no

    Caution: turns off password logins, so you can log in only with a key. Set it only after you've confirmed that key login works, or you'll lose access to the server.

  • KbdInteractiveAuthentication no

    Also turns off keyboard-interactive login, which on some servers lets you enter a password even when PasswordAuthentication is turned off.

  • PermitRootLogin prohibit-password

    Lets the root user log in with a key only, without a password. This is the OpenSSH default. The value no blocks any direct root login, and suits setups where you log in as a regular user and switch to administrator privileges with sudo.

  • AllowUsers deploy admin@203.0.113.10

    Caution: lets only the users on the list log in, and blocks every other user. The user@host form limits a user to a specific address. Make sure the user you log in with is on the list.

  • MaxAuthTries 3

    The maximum number of authentication attempts per connection. The default is 6. A value that's too low can block logins when the agent holds several keys.

  • Port 2222

    Caution: changes the port the SSH service listens on. Before reloading, open the new port in the firewall, and from then on connect with ssh -p 2222.

  • sudo sshd -t

    Checks the configuration file and the server keys for errors, without starting the service. No output means the settings are valid. Run it before every reload.

  • sudo sshd -T | grep -i passwordauthentication

    Shows the setting that actually applies. For each setting, the first value read wins, so a file included with Include at the top of sshd_config overrides a line that appears after it.

    Example: passwordauthentication no

  • sudo systemctl reload sshd

    Reloads the settings, and open connections stay connected. On Debian and Ubuntu, the service is named ssh. After reloading, test a login in a new window before you close the existing connection.

Common errors

  • Permission denied (publickey).

    The server didn't accept any key. Check that the public key is in the ~/.ssh/authorized_keys file of the right user, that the permissions are 700 for the directory and 600 for the file, and that you connect with the right key (-i). The ssh -v output shows which keys were tried.

  • Host key verification failed.

    The server's key doesn't match the one saved in known_hosts, or it wasn't confirmed. After reinstalling the server, remove the old key with ssh-keygen -R and connect again. When there's no known reason for the change, check the server's fingerprint before you continue.

  • WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!

    The warning shown when the server's key has changed. The message gives the line number in known_hosts. The fix is the same: remove the old key only when the change is expected.

  • ssh: connect to host example.com port 22: Connection refused

    The server is reachable, but no service is listening on the port, or a firewall rejects the connection. Check that the port is correct (-p), that the service is running on the server, and that the firewall allows the port.

  • ssh: connect to host example.com port 22: Connection timed out

    The server doesn't respond. Check the address and the port, that the server is running, and that a firewall on the server or on the network isn't blocking the port or the IP address you connect from.

  • Too many authentication failures

    The client tried too many keys before the right one, usually when the agent holds many keys. Connect with -i and -o IdentitiesOnly=yes, or set IdentityFile and IdentitiesOnly yes in the config file.

  • WARNING: UNPROTECTED PRIVATE KEY FILE!

    The permissions of the private key are too open, so the SSH client ignores it. Fix them with chmod 600 on the key file.

    Example: Permissions 0644 for '/home/user/.ssh/id_ed25519' are too open.

Before you change SSH settings on the server

  • Keep a connection open: Before you change sshd_config or the firewall, keep an existing SSH connection open. After the change, test a login in a new window, so you can fix a mistake without losing access to the server.
  • Test before reloading: Run sudo sshd -t, and reload the settings only when the check passes without errors.
  • Move to keys gradually: First add a key and confirm that key login works, and only then turn off password logins.
  • Protect the private key: The private key stays on the computer you connect from, with a passphrase and 600 permissions. Copy only the public key, which ends in .pub, to the server.

On recent versions of Windows and Windows Server, the OpenSSH client is available as a system feature, and the ssh, scp, sftp, ssh-keygen, and ssh-add commands work from PowerShell or the Command Prompt. When the ssh command isn't recognized, install the OpenSSH Client feature from the Windows Optional Features. On macOS and Linux, the commands are available in the built-in terminal.

The server names, users, addresses, and paths on this page are examples, so replace them with your real values. You can also create a key pair with the SSH key generator, and the permissions of the ~/.ssh directory and the key files are fully explained on the Linux file permissions page.

Did you know?

On Linux-based VPS servers, you connect over SSH with full root access and manage the server with every command on this page: keys, the config file, file transfer, tunnels, and the security settings of the SSH service.

VPS plans

SSH commands FAQ

How do I connect to a server over SSH for the first time?

Open a terminal on your computer (PowerShell on Windows) and run ssh with the user name and the server address, for example ssh root@203.0.113.10. On the first connection, the fingerprint of the server's key is shown, and once you confirm it, it's saved on your computer, so on later connections the client recognizes the server. When the SSH service listens on a port other than 22, add the -p option with the port number.

How do I switch from password login to SSH key login?

Create a key pair on your computer with ssh-keygen -t ed25519, and add the public key to the server with ssh-copy-id. Once you've confirmed that key login works, set PasswordAuthentication no in the sshd_config file, check the file with sudo sshd -t, and reload the service. We recommend keeping a connection open until key login succeeds in a new window.

What is the difference between scp, sftp, and rsync?

Use scp to quickly copy a file or a directory with a single command. The sftp command opens an interactive session where you browse directories and upload and download files, and graphical file transfer programs use this protocol too. rsync transfers only what changed, so it suits syncing a whole website and repeat backups. All three work over an encrypted SSH connection with the same login details.

How do I save a short name for a server instead of typing the address and port every time?

Add a block to the ~/.ssh/config file that starts with Host and a short name, with the server's HostName, User, Port, and IdentityFile below it. From then on, you connect with a short command such as ssh myserver, and the short name works in scp, sftp, and rsync too. To see which settings will actually apply, run ssh -G myserver.

How do I connect to a database on the server over SSH?

Open a local tunnel, for example ssh -N -L 3307:127.0.0.1:3306 user@example.com, and point your database management program to address 127.0.0.1 and port 3307 on your computer. The connection runs encrypted inside SSH, and the database can keep listening only on the server's local address, without opening its port to the internet. Many database management programs can open an SSH tunnel on their own.

Shopping cart

Your cart is emptyYou can start with one of our popular plans.

Site search

Accessibility

Text size
100%
Accessibility statement