Sub topic 1
How SSH Logins Work and Why Passwords Are a Weak Point
Prerequisites
This chapter assumes you can:
- Run commands in a Linux or macOS terminal.
- Log in to a remote server with
ssh user@hostusing a password. - Read basic Unix file permissions, such as
rwxandchmod.
How SSH Logins Work
Secure Shell (SSH) is a protocol that lets you run commands on a remote machine over an encrypted connection. The machine you type on is the client. The machine you connect to is the server.
When you log in with a password, the server checks whether you know a secret it stores for your account. The password travels encrypted, but it still crosses the network every time you log in. Passwords also get reused, guessed, phished, or leaked in breaches. Automated bots scan public servers and try common passwords all day.
Why Passwords Are a Weak Point
A password is a shared secret. Both sides must know it, and anyone who learns it can log in as you. Strong passwords are hard to type and hard to remember, so people often choose weak ones. Key-based login removes the shared secret from the login step entirely.
How Public Key Authentication Works
Public key authentication uses a pair of linked files:
- The private key stays on your machine and never leaves it.
- The public key can be shared freely and is stored on the server.
During login, the server sends a random challenge. Your client signs it with the private key. The server checks the signature with the public key. A valid signature proves you hold the private key, without revealing it.
Choosing a Key Type
OpenSSH supports several key types. The two you will see most are:
| Key type | Typical size | Notes |
|---|---|---|
| Ed25519 | 256-bit curve | Fast, short, and recommended for new keys |
| RSA | 3072 bits or more | Widely compatible with older systems |
Use Ed25519 unless an older server or device does not support it. If you must use RSA, choose at least 3072 bits.
Generating a Key Pair with ssh-keygen
Run this command on your client machine:
ssh-keygen -t ed25519 -C "alice@laptop" -f ~/.ssh/id_ed25519The flags do the following:
-t ed25519selects the key type.-Cadds a comment to help you identify the key later.-fsets the file path for the private key.
The command creates ~/.ssh/id_ed25519 (private) and ~/.ssh/id_ed25519.pub (public). It also asks for a passphrase, which encrypts the private key on disk.
Exercise
Which file should you copy to the server, and why not the other one?
Show answer
Copy ided25519.pub, the public key. The private key (ided25519) must stay on your machine, because anyone holding it can authenticate as you.