Sub topic 1
How SSH logins work and why passwords are a weak point
Prerequisites
This chapter assumes you can do the following:
- Use a Linux or macOS command line.
- Log in to a remote Linux server with
ssh user@hostand a password. - Read basic Unix file permissions and edit a file with nano or vim.
- Run commands with
sudoon a server you administer. Some examples read server logs. If you only have a normal account, you can still follow the client-side examples.
Later chapters build the key-based setup. This chapter explains what happens during a login today and why that process is worth replacing.
What happens when you run ssh
SSH (Secure Shell) is a protocol for running commands and moving data on a remote machine over an untrusted network. It uses a client-server model. The ssh program on your computer is the client. The sshd daemon on the remote machine is the server. By default, sshd listens on TCP port 22.
A login is not a single step. It has four phases:
- Transport setup. The client and server agree on algorithms for key exchange, encryption, and message integrity. They run a key exchange to produce session keys. From this point on, everything is encrypted.
- Server authentication. The server proves its identity with its host key. The client checks that key against what it already knows.
- User authentication. The client proves who the user is, using a password, a key, or another method.
- Session. The server starts a shell, runs a command, or opens a tunnel.
Phases 2 and 3 are easy to confuse. Server authentication answers "am I talking to the real server?" User authentication answers "is this really Alice?" Passwords only help with the second question.
Server authentication and known_hosts
When you connect for the first time, the client has no record of the server. It shows the server's host key fingerprint and asks whether to continue:
The authenticity of host 'server.example.com (203.0.113.10)' can't be established.
ED25519 key fingerprint is SHA256:Xx3...redacted...
Are you sure you want to continue connecting (yes/no/[fingerprint])?If you answer yes, the client stores the host key in ~/.ssh/known_hosts. On later connections it compares the key the server presents with the stored one. If they differ, the client refuses to continue and prints a loud warning about a possible man-in-the-middle attack.
This first-connection prompt is the weakest moment of any SSH login. If an attacker intercepts the very first connection and you accept their key, you have trusted the wrong party. The fix is to compare the fingerprint against a trusted source before typing yes. Examples are your cloud provider's console, or a fingerprint the administrator sent you through a separate channel. A later chapter covers this check in detail.
How password authentication works
During user authentication, the server lists the methods it accepts. With a default OpenSSH configuration, that list usually includes publickey and password. If the client chooses password, it sends the password to the server inside the encrypted channel. The server then checks it, usually through PAM (Pluggable Authentication Modules). PAM compares the password against a salted hash stored in /etc/shadow, or asks another system such as LDAP or Kerberos.
Two details matter here:
- The server receives your actual password. The channel is encrypted, so a network eavesdropper cannot read it. But
sshditself sees the plaintext. A compromised or malicious server can capture it. - The secret is shared. You and the server both know, or can verify, the same secret. Anyone who learns it can act as you from anywhere.
You can see which methods a server offers by asking for verbose output and filtering for one line:
ssh -v [email protected] 2>&1 | grep -i "authentications that can continue"The output looks like this:
debug1: Authentications that can continue: publickey,passwordPress Ctrl+C at the password prompt after you see the line. If the list contains only publickey, the server has already disabled password logins.
Authentication methods at a glance
| Method | What you prove | What the server stores | Main weakness |
|---|---|---|---|
| `password` | You know a shared secret | A salted hash of the password | Guessable, reusable, phishable |
| `publickey` | You hold a private key | Your public key | Stolen private key, if unprotected |
| `keyboard-interactive` | You answer prompts, such as a one-time code | Depends on PAM setup | Depends on what the prompts ask for |
| `gssapi-with-mic` | You hold a Kerberos ticket | Nothing per user in `sshd` | Needs a Kerberos environment |
This tutorial focuses on publickey, which removes the shared secret from the picture.
Why passwords are a weak point
Internet-facing servers are guessed at constantly
Any server with port 22 open to the internet receives automated login attempts within hours. Bots try common usernames such as root, admin, and ubuntu, paired with lists of common passwords. The attempts are cheap and run in parallel from many addresses. You can see them in your own logs. On Debian and Ubuntu, failed attempts go to /var/log/auth.log. On RHEL, Fedora, and similar systems, the file is /var/log/secure.
This command lists the source addresses with the most failed password attempts:
sudo grep "Failed password" /var/log/auth.log \
| awk '{print $(NF-3)}' | sort | uniq -c | sort -rn | headOn a server whose log files are not persisted, or on systems that use only the journal, read the same events with sudo journalctl -u ssh --no-pager | grep "Failed password". The unit is named sshd on RHEL-family systems.
A short or common password eventually loses to this kind of guessing. A long random password resists it, but humans rarely choose those for every server.
Passwords get reused and leaked
People reuse passwords across services. When a different website is breached, the leaked credentials are tried against SSH servers. This is called credential stuffing. Your server may be perfectly configured and still fall because of a breach somewhere else.
Passwords can be captured at the endpoint
Encryption protects the password in transit, not at either end. Several attacks bypass the encryption:
- A keylogger on your laptop records the password as you type it.
- A phishing page or a fake server collects it, which is easier if you accept an unknown host key.
- A compromised server running a modified
sshdlogs every password that users submit. Attackers use this to move to other machines where people reused the password. - Passwords typed into scripts or shell history can be left in plaintext. Never put a password on a command line.
With public key authentication, the private key never leaves your machine, and the server never receives a secret it could replay elsewhere.
Passwords do not scale
Every server needs an account password. Rotating them across a fleet is tedious, so people skip it. When an employee leaves, you must change every password they knew. Passwords also block automation. Scripts, backups, and deployment tools need to log in without a human, which pushes people toward storing passwords in files.
Rate limiting only reduces the problem
Defenses exist, and they help, but each has limits:
MaxAuthTriesinsshd_configlimits failed attempts per connection. It does not stop an attacker from opening a new connection.- Tools like fail2ban block addresses after repeated failures. Attackers who spread attempts across thousands of addresses stay under the threshold.
- Moving
sshdto another port reduces log noise, but a port scan finds it quickly. Treat it as noise control, not security. - Strong password policies help, but they depend on every user following them.
These measures make guessing slower. They do not remove the possibility of guessing, because a password is a secret that can be typed by anyone who knows it.
Checking what your server allows
Before changing anything, find out how your server is configured now. The sshd -T command prints the effective configuration after all includes and defaults are applied:
sudo sshd -T | grep -Ei '^(passwordauthentication|pubkeyauthentication|kbdinteractiveauthentication|permitrootlogin|maxauthtries)'Typical output on a fresh Debian or Ubuntu system:
permitrootlogin without-password
maxauthtries 6
pubkeyauthentication yes
passwordauthentication yes
kbdinteractiveauthentication noOutput varies by distribution and version. Some versions print prohibit-password instead of without-password, and older ones name the keyboard-interactive option challengeresponseauthentication. Note that pubkeyauthentication yes is already the default, so the server is ready for keys. Only passwordauthentication yes leaves the password route open.
Note also that sshd -T reports the server's global settings. A Match block in sshd_config can override them for particular users or addresses. Also, cloud images sometimes ship a file in /etc/ssh/sshd_config.d/ that sets PasswordAuthentication. For OpenSSH, the first value found for a setting wins, so an included file can override a line you add later in the main file. You will meet this again when you disable passwords in a later chapter.
What public key authentication changes
Instead of sending a secret, the client proves it holds a private key by signing data that is tied to the current session. The server checks the signature against a public key stored in the user's ~/.ssh/authorized_keys file. The server never sees the private key. Guessing becomes impractical, because an Ed25519 private key has about 256 bits of key material, and a password cannot match that. Capturing a transmitted value gains the attacker nothing, because a signature is valid only for one session.
Keys have their own risks. A private key stored without a passphrase works for anyone who copies the file. The next chapters show how to generate keys, protect them, install them, and manage them over time.
Exercise
A colleague says, "SSH passwords are safe because the whole connection is encrypted." Give two reasons this does not make password authentication safe, and name one command you can run to see which authentication methods a server offers.
Show answer
Encryption protects the password only while it travels over the network. First, the server (or anyone who has compromised it) receives the plaintext password, and a keylogger or phishing page on the client side also captures it. Second, encryption does not stop online guessing or credential stuffing: an attacker can open their own encrypted connection and try common or leaked passwords. To see the offered methods, run ssh -v user@host and look for the line "Authentications that can continue", for example by filtering with grep -i "authentications that can continue".
Exercise
You run sudo sshd -T | grep -i passwordauthentication and see passwordauthentication no, yet ssh -v to the same host still shows password in the list of methods that can continue. Give one likely explanation and one way to investigate it.
Show answer
A Match block in sshd_config can override the global setting for particular users or source addresses, and sshd -T without connection parameters reports only the global result. Another cause is that you are connecting to a different host or port than the one you checked. To investigate, run sudo sshd -T -C user=alice,host=client.example.com,addr=198.51.100.7 with values matching your connection, and read /etc/ssh/sshd_config and any files in /etc/ssh/sshd_config.d/ for Match blocks.