{ "id": "ssh-keys", "title": "SSH Key Authentication", "category": "access", "tags": ["ssh", "authorized_keys", "keys", "permissions"], "updated": "2025-11-03", "summary": "How SSH key auth works and how to set it up correctly.", "sections": [ { "heading": "How It Works", "body": "

SSH key authentication replaces passwords with a cryptographic key pair. The private key stays on your machine. The public key goes into ~/.ssh/authorized_keys on the target host. When you connect, the server checks whether your private key corresponds to one of the public keys it trusts.

There is no password transmitted. Either the key matches or the connection fails.

" }, { "heading": "Generating a Key Pair", "body": "

Use ed25519 unless something forces you onto RSA. It is smaller and more secure.

", "code": "ssh-keygen -t ed25519 -C \"your-comment-here\"\n# Accept the default path (~/.ssh/id_ed25519) or specify one.\n# Passphrase is optional but recommended for keys that leave your machine." }, { "heading": "Installing the Public Key", "body": "

Copy the public key to the remote host:

", "code": "# Option 1 — if password auth is still working\nssh-copy-id -i ~/.ssh/id_ed25519.pub user@host\n\n# Option 2 — manually\ncat ~/.ssh/id_ed25519.pub >> ~/.ssh/authorized_keys" }, { "heading": "File and Directory Permissions", "body": "

This is the most common reason key auth fails. SSH will silently reject keys if the permissions are too open.

", "code": "chmod 700 ~/.ssh\nchmod 600 ~/.ssh/authorized_keys\nchown -R youruser:youruser ~/.ssh" }, { "heading": "Troubleshooting", "body": "

Run ssh -v user@host for verbose output. The auth failure reason is usually in the first 20 lines.

Common causes:

" }, { "heading": "Checking the sshd Config", "body": "

Relevant lines in /etc/ssh/sshd_config:

", "code": "PubkeyAuthentication yes\nAuthorizedKeysFile .ssh/authorized_keys\n\n# After editing sshd_config, test before reloading:\nsshd -t\nsystemctl reload ssh" } ] }