# What is SSH and how does it work on a server

> SSH (Secure Shell) is a protocol that gives you an encrypted command line on a remote machine, usually over port 22.

URL: https://levelrail.com/learn/server-basics/what-is-ssh-and-how-does-it-work/
Updated: 2026-11-27

SSH (Secure Shell) is the encrypted protocol you use to open a command line on a remote server, normally on port 22. Your server has no keyboard or screen. You reach it through a terminal on your own laptop, and everything you type crosses networks you do not control: coffee-shop Wi-Fi, your ISP, the hosting provider. SSH is what makes that safe, and it works with [a VPS](https://levelrail.com/learn/server-basics/what-is-a-vps/)) from any provider. Without it, your password and every command would travel in readable form.

## A short history

SSH was written in 1995 after a password-sniffing attack on a university network, as a replacement for telnet and rlogin, which sent everything in plain text. OpenSSH, the version on nearly every Linux server, has been developed by the OpenBSD project since 1999.

## How it works

SSH is a client and a server talking over TCP. Your computer runs the client (`ssh`). The server runs the daemon (`sshd`), which listens on port 22 by default and waits for connections.

When you connect, three things happen in order:

1. **Encryption is agreed.** Both sides pick algorithms and derive a shared secret key that never crosses the network. From here on, all traffic is encrypted.
2. **The server proves who it is.** It presents its host key. The first time, your client shows a fingerprint and asks if you trust it. If you say yes, it saves the key in `~/.ssh/known_hosts` on your computer. Later connections compare against that saved key.
3. **You prove who you are.** With a password, or with a key pair. In key login, the server checks your public key against `~/.ssh/authorized_keys` and you prove you hold the private key without ever sending it.

Step 2 is the part people skip. It is what stops someone in the middle from pretending to be your server. Step 3 is why keys beat passwords: there is nothing to guess and nothing to type.

Once logged in, SSH carries more than a shell. `scp`, `sftp` and `rsync` all ride on it, and it can forward ports to reach services that are not exposed to the internet.

| Part | Where it lives | What it does |
| --- | --- | --- |
| `ssh` client | Your computer | Opens the connection |
| `sshd` daemon | The server | Listens on port 22, checks identity |
| `~/.ssh/known_hosts` | Your computer | Remembers server fingerprints |
| `~/.ssh/authorized_keys` | The server, per user | Lists public keys allowed to log in |
| `/etc/ssh/sshd_config` | The server | Server settings |

## SSH compared with the alternatives

| Method | Encrypted | Typical use |
| --- | --- | --- |
| SSH | Yes | Shell, file copy and port forwarding on servers |
| Telnet | No | Old devices, avoid on the internet |
| Provider web console | Depends on the provider | Rescue access when SSH is broken |
| RDP or VNC | Varies | Graphical desktops |

## Common misconceptions

- A changed port is not a lock. Scanners find any open port quickly. Keys and a firewall do the real work.
- A passphrase on your key is not a server password. It protects the key file on your own computer.
- Encryption does not prove identity. The host fingerprint check is what tells you the server is the right one.

## When not to use it

Do not expose SSH to the whole internet if only your office needs it. Limit port 22 to known addresses in the firewall, or reach the server through a VPN. For a quick rescue after you lock yourself out, use the provider console instead.

## Check it worked

Connect with verbose output to watch each stage:

```bash
ssh -v deploy@203.0.113.10
```

What you should see: many lines starting with `debug1:`. Look for the server version line, the host key check against `known_hosts`, and the authentication methods tried, ending with `Authenticated to` followed by the address. Press Ctrl+C, or type `exit` once logged in.

To see which fingerprints your computer already trusts:

```bash
ssh-keygen -l -f ~/.ssh/known_hosts
```

What you should see: one line per saved host with its key size, fingerprint and address (names may be hashed). Check the installed version with `ssh -V`; it prints a line starting with `OpenSSH_`.

## Edge cases and gotchas

- **The fingerprint warning after a rebuild is normal, but not always harmless.** If you reinstall the server or move the IP to a new machine, the host key changes and ssh refuses to connect with a warning that the remote host identification has changed. If you caused the change, remove the old entry with `ssh-keygen -R 203.0.113.10`. If you did not, stop and ask your provider.
- **Reused IP addresses trigger the same warning.** Cloud providers recycle addresses. A new server on an old IP looks like a changed host to your client.
- **IPv6 and IPv4 are separate addresses with separate firewall rules.** A server may answer on `2001:db8::10` but be blocked on IPv4, or the reverse. Test with `ssh -4` and `ssh -6` to tell them apart.
- **Idle connections get dropped.** NAT devices and firewalls forget quiet sessions. Add `ServerAliveInterval 60` to your client config (see [SSH config file shortcuts](https://levelrail.com/learn/server-basics/ssh-config-file-shortcuts/))) to keep them alive.
- **Your local user name is sent by default.** `ssh 203.0.113.10` tries to log in as your computer's user name. Write `user@host` or set `User` in your config.
- **Clock drift does not break SSH, but it confuses logs.** Keep the server time synced so failed-login times line up when you investigate.
- **Changing the port needs more than one edit.** The firewall must allow the new port, and on newer Ubuntu releases the `ssh.socket` unit may own the listening port. See [Disable SSH password login](https://levelrail.com/learn/server-basics/disable-ssh-password-login/)) for how config files interact.

## Troubleshooting

| Symptom | Likely cause | Fix |
| --- | --- | --- |
| `Connection refused` | sshd is not running, or nothing listens on that port | Use the provider console, run `sudo systemctl status ssh` |
| `Connection timed out` | Wrong address, or a firewall drops port 22 | Check the IP, firewall rules and the provider's network firewall |
| `Permission denied (publickey)` | Server does not accept your key or user name | Check the user, and that the public key is in `authorized_keys` |
| Host key changed warning | Server rebuilt, IP reused, or a different machine | Verify the cause, then `ssh-keygen -R <address>` |
| `Could not resolve hostname` | Typo or missing DNS record | Check the name, or use the IP address directly |
| Prompt appears but typing hangs after idle | Idle session dropped by NAT or firewall | Set `ServerAliveInterval 60` in `~/.ssh/config` |
| Login works with a password but you expected keys | Password login is still enabled on the server | See [Disable SSH password login](https://levelrail.com/learn/server-basics/disable-ssh-password-login/)) |
| `Host key verification failed.` | The saved fingerprint no longer matches the server | Confirm the server was rebuilt, then `ssh-keygen -R <address>` and reconnect |

## Real-world scenario

You rent a server and the provider emails you an address, 203.0.113.10, and a root password. You run `ssh root@203.0.113.10`. Your client prints a fingerprint and asks whether to continue. You compare it with the fingerprint shown in the provider's console, type `yes`, and the client writes it to `~/.ssh/known_hosts`.

A week later you rebuild the server from scratch to start clean. The next `ssh` command stops with a warning that the remote host identification has changed. Nothing is wrong: the new install generated a new host key. You run `ssh-keygen -R 203.0.113.10`, connect again, and accept the new fingerprint.

The lesson is that the warning is useful because it fires on both harmless and harmful changes. Only you know which one it is.

## Quick reference

| Command | What it does |
| --- | --- |
| `ssh user@host` | Open a shell on the server |
| `ssh -v user@host` | Show each connection stage |
| `ssh -p 2222 user@host` | Use a non-default port |
| `ssh -V` | Print the client version |
| `ssh-keygen -R host` | Forget a saved host key |
| `ssh-keygen -l -f ~/.ssh/known_hosts` | List saved fingerprints |

## Security and safety notes

- Keep your private key private. Anyone holding it can log in as you.
- Keep your current session open when you edit `/etc/ssh/sshd_config`, and test a new login in a second window before you close the first.
- Never accept a changed host key without knowing why it changed.

## Related lessons

- [Log in with SSH keys](https://levelrail.com/learn/server-basics/log-in-with-ssh-keys/)): create the key pair this lesson talks about
- [Set up a firewall with ufw](https://levelrail.com/learn/server-basics/set-up-a-firewall-with-ufw/)): keep port 22 reachable while closing the rest
- [Disable SSH password login](https://levelrail.com/learn/server-basics/disable-ssh-password-login/)): make keys the only way in
- [SSH config file shortcuts](https://levelrail.com/learn/server-basics/ssh-config-file-shortcuts/)): save the user, port and key under one name

## Next

Put these ideas into practice with the [Initial server setup checklist](https://levelrail.com/learn/server-basics/initial-server-setup-checklist/)).

## Frequently asked questions

### Is SSH safer than a password login on a website?

SSH encrypts everything between you and the server, so passwords and commands cannot be read on the way. Keys are stronger than passwords because there is nothing to guess.

### Do I need to change the SSH port?

No. It can reduce noise in the logs, but it is not real protection. Keys, a firewall and disabled password login matter more.

### Why does SSH ask me to trust a fingerprint the first time?

Your computer has never seen this server, so it cannot verify it. It shows the host key fingerprint and saves it in known_hosts if you accept, then checks it on every later connection.

### Why does my connection drop after a few minutes of silence?

A router or firewall in the path may forget idle connections. Setting ServerAliveInterval in your SSH config sends small keepalive messages and usually fixes it.


## Sources

- [OpenSSH manual pages](https://www.openssh.com/manual.html)
