# SSH Key-Based Authentication
SSH key-based authentication replaces password login with asymmetric cryptography. A private key stays on the client machine, and its corresponding public key is placed on each server. This is the foundation of SSH hardening — without it, any attacker who can reach port 22 can brute-force passwords.
---
## How It Works
SSH key auth uses a **key pair** — two mathematically related files:
|File|Location|Permissions|Purpose|
|---|---|---|---|
|`id_ed25519`|`~/.ssh/` on client|`-rw-------` (600)|Private key — never leaves your machine|
|`id_ed25519.pub`|`~/.ssh/` on client|`-rw-r--r--` (644)|Public key — copied to servers|
|`authorized_keys`|`~/.ssh/` on server|`-rw-------` (600)|List of public keys allowed to connect|
The client proves identity by signing a challenge with the private key. The server verifies the signature using the stored public key. No password ever crosses the network.
> [!tip] Always set a passphrase on your private key. The passphrase encrypts the key file at rest — if someone steals `id_ed25519`, they still can't use it without the passphrase.
---
## Step 1 — Generate the Key Pair
On the **client machine** (the machine you SSH _from_):
```bash
ssh-keygen -t ed25519 -C "<your-user>@<your-machine>"
```
- `-t ed25519` — use the Ed25519 algorithm (shorter keys, faster, more secure than RSA)
- `-C "label"` — comment tag to identify the key
- Accept the default save location (`~/.ssh/id_ed25519`)
- **Set a passphrase** when prompted
Verify both files exist:
```bash
ls -la ~/.ssh/
```
You should see `id_ed25519` (private) and `id_ed25519.pub` (public).
---
## Step 2 — Copy the Public Key to the Server
```bash
ssh-copy-id username@server-ip
```
This command:
1. Connects to the server using password auth (one last time)
2. Creates `~/.ssh/authorized_keys` on the server if it doesn't exist
3. Appends your public key to that file
Repeat for each server. In my lab, the key was copied to every homelab host — the Mac Mini node and the Raspberry Pi nodes.
---
## Step 3 — Verify Key Auth Works
Open a **new terminal** and SSH into the server:
```bash
ssh <your-user>@<your-host>
```
It should prompt for your **key passphrase** (not your server password). If it still asks for a server password, something went wrong with the key copy.
> [!warning] Always verify key auth works in a new session before disabling password auth. If you disable passwords first and key auth isn't set up correctly, you'll be locked out.
---
## Why Ed25519 Over RSA
||RSA|Ed25519|
|---|---|---|
|Key size|3072–4096 bits|256 bits|
|Security|Equivalent at 3072+|Equivalent or better|
|Speed|Slower key generation and signing|Faster|
|Compatibility|Universal (legacy support)|Requires OpenSSH 6.5+ (2014)|
Unless you need to connect to very old systems, always choose Ed25519.
---
## Useful Commands
|Command|Purpose|
|---|---|
|`ssh-keygen -t ed25519 -C "label"`|Generate a new key pair|
|`ssh-copy-id user@host`|Copy public key to a server|
|`cat ~/.ssh/id_ed25519.pub`|View your public key|
|`ssh -v user@host`|Verbose login — debug auth issues|
|`chmod 600 ~/.ssh/id_ed25519`|Fix private key permissions if needed|
---
## See Also
- [[System Hardening|SSH Hardening]] — next steps: disable password auth, limit auth attempts, disable X11 forwarding, add UFW + Fail2Ban
## Resources
- [OpenSSH Manual — ssh-keygen](https://man.openbsd.org/ssh-keygen)
- [SSH Academy — Public Key Authentication](https://www.ssh.com/academy/ssh/public-key-authentication)