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.
- Updated Nov 27, 2026
- Markdown
Explain how an SSH connection starts and how to use it safely.
How SSH key login works
- Your computer connects to the server on port 22 and asks to log in as a user.
- The server shows its host key so you can check you reached the right machine.
- Your computer offers its public key and the server looks for it in authorized_keys.
- The server sends a challenge that only the matching private key can answer.
- Your computer signs the challenge with the private key, which never leaves it.
- The signature checks out, so the server opens an encrypted shell for you.
Static diagram
- Your computerconnectYour server
- Your serverhost keyYour computer
- Your computeroffer public keyauthorized_keys
- authorized_keyschallengeYour computer
- Your computersigned answerYour server
- Your serveropenShell
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 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:
- 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.
- 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_hostson your computer. Later connections compare against that saved key. - 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_keysand 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:
ssh -v deploy@203.0.113.10What 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:
ssh-keygen -l -f ~/.ssh/known_hostsWhat 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::10but be blocked on IPv4, or the reverse. Test withssh -4andssh -6to tell them apart. - Idle connections get dropped. NAT devices and firewalls forget quiet sessions. Add
ServerAliveInterval 60to your client config (see SSH config file shortcuts) to keep them alive. - Your local user name is sent by default.
ssh 203.0.113.10tries to log in as your computer’s user name. Writeuser@hostor setUserin 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.socketunit may own the listening port. See 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 |
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: create the key pair this lesson talks about
- Set up a firewall with ufw: keep port 22 reachable while closing the rest
- Disable SSH password login: make keys the only way in
- 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.
Check what you learned
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
Keep going: guides and tutorials
- Install a Self-Hosted PaaS on Your Own Server in Five Minutes One installer download, one setup token, one sample app. The full Levelrail install on a fresh Linux server, with the checks and the fixes.
- Manage Secrets Self-Hosted: Env Vars and Master Key Rotation Set env vars and encrypted secrets on a self-hosted server, apply changes safely, and rotate the master key with no downtime without locking yourself out.