Chapter 1 · Lesson 11 of 19
Server basics

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.

By the end you will

Explain how an SSH connection starts and how to use it safely.

Time
14 min
Level
Beginner
Check
4 questions

How SSH key login works

SSH login with a key pair: the private key stays on your laptop, the server holds the matching public key, and logging in proves you hold the private key without sending it.
  1. Your computer connects to the server on port 22 and asks to log in as a user.
  2. The server shows its host key so you can check you reached the right machine.
  3. Your computer offers its public key and the server looks for it in authorized_keys.
  4. The server sends a challenge that only the matching private key can answer.
  5. Your computer signs the challenge with the private key, which never leaves it.
  6. The signature checks out, so the server opens an encrypted shell for you.
Static diagram
  1. Your computerconnectYour server
  2. Your serverhost keyYour computer
  3. Your computeroffer public keyauthorized_keys
  4. authorized_keyschallengeYour computer
  5. Your computersigned answerYour server
  6. 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:

  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.

PartWhere it livesWhat it does
ssh clientYour computerOpens the connection
sshd daemonThe serverListens on port 22, checks identity
~/.ssh/known_hostsYour computerRemembers server fingerprints
~/.ssh/authorized_keysThe server, per userLists public keys allowed to log in
/etc/ssh/sshd_configThe serverServer settings

SSH compared with the alternatives#

MethodEncryptedTypical use
SSHYesShell, file copy and port forwarding on servers
TelnetNoOld devices, avoid on the internet
Provider web consoleDepends on the providerRescue access when SSH is broken
RDP or VNCVariesGraphical 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) 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 for how config files interact.

Troubleshooting#

SymptomLikely causeFix
Connection refusedsshd is not running, or nothing listens on that portUse the provider console, run sudo systemctl status ssh
Connection timed outWrong address, or a firewall drops port 22Check the IP, firewall rules and the provider’s network firewall
Permission denied (publickey)Server does not accept your key or user nameCheck the user, and that the public key is in authorized_keys
Host key changed warningServer rebuilt, IP reused, or a different machineVerify the cause, then ssh-keygen -R <address>
Could not resolve hostnameTypo or missing DNS recordCheck the name, or use the IP address directly
Prompt appears but typing hangs after idleIdle session dropped by NAT or firewallSet ServerAliveInterval 60 in ~/.ssh/config
Login works with a password but you expected keysPassword login is still enabled on the serverSee Disable SSH password login
Host key verification failed.The saved fingerprint no longer matches the serverConfirm 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#

CommandWhat it does
ssh user@hostOpen a shell on the server
ssh -v user@hostShow each connection stage
ssh -p 2222 user@hostUse a non-default port
ssh -VPrint the client version
ssh-keygen -R hostForget a saved host key
ssh-keygen -l -f ~/.ssh/known_hostsList 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.

Next#

Put these ideas into practice with the Initial server setup checklist.

Check what you learned4 questions
1What does the saved fingerprint in known_hosts protect against?
2Which side runs sshd and listens on port 22 by default?
3You see Connection refused. What is a likely cause?
4ssh warns that the remote host identification has changed. What should you do first?

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

View MD Edit
Share
Helpful?
Progress is saved in this browser only.

Keep going: guides and tutorials

View MD Edit
Share
Helpful?
Run this on your own server

Levelrail deploys apps from git with health-checked releases and one-click rollback.

docker pull ghcr.io/glincker/levelrail:latest Install Levelrail From GLINR Studios