SSH Public Key Authentication with ssh-keygen
Learn how to replace SSH passwords with an ed25519 key pair, install the public key on a server, use a passphrase and ssh-agent, and avoid common security pitfalls.
A password can be guessed, recorded, or stolen. Public key authentication makes SSH login depend not on something a human can remember, but on a cryptographic key pair that is extremely hard to forge. This is a core step in the SSH curriculum.
How Public Keys Work
SSH uses asymmetric cryptography: every user has a private key (secret, never share it) and a public key (safe to distribute to any server).
Here is how it works: the server sends a cryptographic challenge that can only be answered with the private key. The client answers, and the server verifies the answer with the stored public key. No password ever crosses the network, so there is nothing for a password thief to steal.
Creating a Key Pair
Create a key with ssh-keygen. The recommended modern algorithm is Ed25519, because it is secure and the keys are short:
ssh-keygen -t ed25519 -C "[email protected]"
The command will ask where to store the key. Leave the default ~/.ssh/id_ed25519, then set a passphrase.
Passphrase and ssh-agent
A passphrase protects the private key stored on disk: without a passphrase, anyone who steals the key file can log in as you. But typing the passphrase on every login is annoying. The solution is ssh-agent: the key is unlocked once per session, and then the agent remembers it:
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_ed25519
After ssh-add, you do not need to type the passphrase again until the agent is shut down.
Installing the Key on the Server
There are two ways to install a public key into a user account on the server.
The Easy Way with ssh-copy-id
ssh-copy-id -i ~/.ssh/id_ed25519.pub user@server-address
This command copies the public key to ~/.ssh/authorized_keys on the server and fixes the file permissions automatically.
The Manual Way
If ssh-copy-id is not available:
- Read the public key:
cat ~/.ssh/id_ed25519.pub. - Log in to the server and paste its contents into
~/.ssh/authorized_keys. - Make sure the permissions are correct: the
~/.sshfolder must be700and theauthorized_keysfile must be600.
Incorrect permissions are often the cause of the classic “key not readable” error. SSH rejects files whose mode is too permissive for security reasons.
Logging In with the Key
Once the key is installed, log in without typing a password:
ssh user@server-address
If you did not type a passphrase when creating the key (not recommended for production), the login happens immediately.
Commonly Overlooked Security Pitfalls
- A key without a passphrase on a work laptop: one stolen laptop means losing access to every server. Use a passphrase plus an agent.
- Copying the private key: a private key must never be sent over chat, email, or pastebin.
- One key for every server: if a single server is compromised, the key you stored there is at risk. For sensitive environments, consider a different key per server or per role.
- Not restricting keys: use the options in
authorized_keys(such asfrom="client-ip",command=) to restrict when a key may be used. - Loose file permissions: make sure of
chmod 600 authorized_keysandchmod 700 ~/.ssh, otherwise SSH will refuse.
Auditing Installed Keys
On a server, the public keys from various users’ devices all look the same length. The key is management: manage keys through configuration management (for example Ansible) and regularly audit the contents of authorized_keys for every account:
find /home -name authorized_keys -exec cat {} \;
Every account that can SSH is one more attack surface, so keep the list narrow.
Wrapping Up
Public keys are a far stronger replacement for passwords. Once they are in place, the next step is to lock down the server by disabling passwords and streamlining the configuration.
Next in the curriculum: continue to securing the sshd configuration and hardening.
References
- RFC 4252, The Secure Shell (SSH) Authentication Protocol, rfc-editor.org/rfc/rfc4252 (the key authentication protocol).
- The
ssh-keygen(1)man page, OpenSSH. - The
sshd(8)man page and theAUTHORIZED_KEYS FILE FORMATsection. - Daniel J. Barrett and Richard E. Silverman, SSH, The Secure Shell: The Definitive Guide, O’Reilly Media.
- OpenSSH Security, openssh.com/security.html (security practices and fix history).
Related Articles
Hardening Homelab Server Security: A Complete Guide
Secure your homelab server with essential hardening steps from SSH key authentication to service isolation and automated updates using this practical checklist.
SecuritySSH Hardening: Securing a Server's sshd_config on the Internet
Harden any internet-facing SSH server by disabling password logins, restricting users, adding fail2ban, and applying a proven best-practices checklist.
SecurityInside the SSH Protocol: Cryptography, Handshake, and Specification
Take SSH apart from the inside: key exchange, host keys, ciphers, MACs, perfect forward secrecy, and how clients cryptographically verify the server.
Your ideas deserve to be read by thousands of people.
ByteNesia is built for everyone. If you have experience, tutorials, or opinions about technology, write and share them here. Your name will be credited as a contributor.
Write anything: tool reviews, homelab tutorials, security tips, or your tech stories.