What is SSH? How Secure Shell works and how to use it

What SSH is, how it keeps a remote login private, the commands you will use with it, and how to keep SSH access to your server safe.

7–11 minutes
Laptop screen showing code beside a keyboard

SSHSSH A secure way to type commands on a server from your own computer, as if you were sitting at it. More about SSH → (Secure Shell) is the encrypted connection you use to log in to a remote server, run commands on it and copy files to it. This guide is for anyone who has just been given a server, a VPS or SSH access in a hosting plan and wants to understand what SSH is before relying on it.

SSH is a network protocol that gives you an encrypted command line on another computer. You run ssh username@server-address on your own machine, the server proves who it is with its host key, you prove who you are with a password or, better, an SSH key, and everything you type and see travels encrypted. It normally uses portPort A numbered door on a server. Each service listens behind its own door: web pages on 443, email on others, remote login on 22. More about Port → 22, and the same connection also carries file transfers (SFTPSFTP A secure way to copy files between your computer and a server. Like dragging files into a folder, but the folder is on the server. More about SFTP →, scp, rsync) and tunnels.

What SSH does

A server in a data centre has no screen or keyboard you can reach. SSH is how you work on it from wherever you are. Once connected, the terminal window on your laptop behaves as if it were plugged into the server: commands you type run there, and their output comes back to you.

SSH solves three problems at once:

  • Privacy. Everything between your computer and the server is encrypted, so someone watching the network (on café Wi-Fi, for example) sees only scrambled data, not your password or your commands.
  • Proof of the server’s identity. The server has a host key. Your computer remembers it, so if a different machine ever pretends to be your server, SSH warns you loudly.
  • Proof of your identity. You log in with a password or with a key pair. Keys are far stronger, because there is nothing to guess.

Before SSH, people used Telnet and FTP, which sent passwords across the network as plain text. SSH replaced them, and today almost every Linux server is managed this way. If you are new to servers generally, What is a server? explains where SSH fits in the bigger picture.

The pieces involved

Client and server

SSH has two sides. The client is the program on your computer that starts the connection: the ssh command. The server is a background program on the remote machine, called sshd (the SSH daemon), that listens for connections and checks who is allowed in.

The software almost everyone uses on both sides is OpenSSH. It is built into macOS, Linux and Windows 10 and 11, so you rarely need to install anything on your own computer. On Windows you can use the ssh command in PowerShell or Windows Terminal, or the copy that comes with Git Bash.

The port

The server listens on a port, which is a number that tells incoming traffic which program it is meant for. SSH uses port 22 by default. Some hosts move it to another number, such as 2222, to cut down on automated login attempts in the logs. If yours has, they will tell you, and you add -p 2222 to the command. What is a port number? explains ports in more depth.

Usernames and accounts

You log in as an account that already exists on the server: often root, ubuntu, almalinux or a name your host created for you, such as exampleco. SSH does not create accounts; it lets existing ones log in remotely.

What happens when you connect

When you run:

ssh exampleco@203.0.113.10

these steps happen in about a second:

  1. Your computer opens a network connection to 203.0.113.10 on port 22.
  2. Client and server agree which encryption methods to use and set up a shared secret that only the two of them know. From this point everything is encrypted.
  3. The server shows its host key. The first time, SSH asks if you trust it and, if you type yes, saves its fingerprint in ~/.ssh/known_hosts on your computer. On every later connection it checks the key matches.
  4. You prove who you are, either with the account’s password or with your SSH key.
  5. The server starts a shell (a command line) for your account, and you see a prompt such as exampleco@web1:~$.

The first-time prompt and what to do with it are covered step by step in How to connect to a server with SSH.

If SSH ever says REMOTE HOST IDENTIFICATION HAS CHANGED, stop and find out why before you continue. It is usually harmless (the server was rebuilt or you were given a new one at the same address), but it is also exactly what you would see if someone were intercepting the connection. Ask your host or check the fingerprint in their control panelControl panel A website you log into to manage your hosting with buttons and forms, instead of typing commands. cPanel and Plesk are the common ones. More about Control panel →, then remove the old entry with ssh-keygen -R 203.0.113.10.

Passwords versus SSH keys

PasswordSSH key
What you holdSomething you rememberA private key file on your computer
Can it be guessed?Yes, bots try thousands every dayNo, not in practice
Typing neededEvery loginNone, or a passphrase your keychain remembers
Good forFirst login on a new serverEveryday use

An SSH key pair is two matching files. The private key (for example ~/.ssh/id_ed25519) stays on your computer and is never shared. The public key (id_ed25519.pub) goes on the server, in the account’s ~/.ssh/authorized_keys file. When you connect, the server sets a challenge that only the matching private key can answer, so no secret crosses the network.

You create a key with ssh-keygen -t ed25519. The full process, including copying the key across and switching password logins off, is in How to set up SSH keys.

What you can do over SSH

Run commands

This is the everyday use: install updates, restart a web serverWeb server The program that answers visitors’ browsers and sends them your website. Also used for the computer it runs on. More about Web server →, read a log file, check disk space. Once you are in, you type ordinary Linux commands. For example, to find out what the server is running, see How to check your Ubuntu or Linux version.

You can also run one command without opening an interactive session:

ssh exampleco@203.0.113.10 df -h

Copy files

Because SSH already provides a secure channel, file transfer tools ride on top of it:

  • SFTP (SSH File Transfer Protocol) is what graphical programs such as FileZilla and WinSCP use when you choose SFTP. It uses the same address, port, username and key as SSH.
  • scp copies a single file or folder from the command line:
scp backup.sql exampleco@203.0.113.10:/home/exampleco/
  • rsync copies only what has changed, which makes it the tool for large sites and backups. See How to use rsync.

Tunnel a connection

SSH can carry other traffic through its encrypted connection. A common case is reaching a database that only listens on the server itself. This command forwards port 3306 on your computer to port 3306 on the server:

ssh -L 3306:127.0.0.1:3306 exampleco@203.0.113.10

While that session is open, a database tool on your laptop can connect to 127.0.0.1 port 3306 and reach the server’s MySQL or MariaDB, without the database ever being open to the internet.

Edit files in a code editor

Editors such as VS Code and Cursor can open a folder on the server over SSH, so you edit live files with a full editor instead of a terminal. How to connect Cursor to a server over SSH walks through it.

Save time with an SSH config file

Typing the user, address, port and key every time gets tedious. On your own computer, create or edit ~/.ssh/config (on Windows, C:\Users\you\.ssh\config) and add a short name:

Host web1
    HostName 203.0.113.10
    User exampleco
    Port 22
    IdentityFile ~/.ssh/id_ed25519

Now ssh web1 does the same as the long command, and scp, rsync and code editors understand the name too.

Keeping SSH secure

SSH is the front door to your server, and automated bots knock on port 22 all day. A few settings close the doors they try first. On the server they live in /etc/ssh/sshd_config (Ubuntu and Debian also read extra files in /etc/ssh/sshd_config.d/).

  • Log in with SSH keys and set PasswordAuthentication no once key login works.
  • Set PermitRootLogin no (or prohibit-password) and use a normal account with sudo.
  • Allow SSH through the firewallFirewall A gatekeeper that decides which connections are allowed into a server and blocks the rest. More about Firewall → and close everything you do not need, as in How to set up a firewall on Linux.
  • Keep the server updated so OpenSSH gets security fixes.
  • Protect your private key with a passphrase, and never copy it onto a server or share it.

After changing sshd_config, check it for mistakes and then reload the service:

sudo sshd -t
sudo systemctl reload ssh

On AlmaLinux and Rocky Linux the service is called sshd, so use sudo systemctl reload sshd. If sshd -t prints nothing, the file is valid.

Keep your current SSH session open while you change SSH settings, and test the new settings by logging in from a second terminal window. If the new login fails, you can still fix the problem from the first window instead of being locked out.

Common problems

SymptomCauseFix
Connection timed outWrong address, server down, or a firewall blocking the portCheck the IP address and port with your host, and that the firewall allows SSH
Connection refusedNothing is listening on that port, or SSH is on a different portUse the port your host gave you with -p, or ask them to check sshd is running
Permission denied (publickey)The server only accepts keys and yours is not in authorized_keysAdd your public key, or connect with -i pointing to the right private key
Permission denied, please try againWrong password or usernameCheck the username exactly, and remember the password does not show as you type
REMOTE HOST IDENTIFICATION HAS CHANGEDThe server’s host key is different from last timeConfirm with your host why, then run ssh-keygen -R with the address
Session freezes after a while idleA router drops quiet connectionsAdd ServerAliveInterval 60 under the host in ~/.ssh/config

For more detail on any connection error, add -v to the command (ssh -v exampleco@203.0.113.10). SSH then prints each step, and the last few lines usually show where it stopped.

Official documentation

Get the next guide

One email when there is something worth reading: new guides, errors going around, and hosting changes that affect your site.

No tracking pixels, and one click to leave.

Something out of date? Software changes. If a step no longer works, tell us and we will check it and update the page.