Skip to content
Back to Series SSH from Scratch
Security

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
Alif Afriza Author
· · 4 min read
SSH Public Key Authentication with ssh-keygen

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:

  1. Read the public key: cat ~/.ssh/id_ed25519.pub.
  2. Log in to the server and paste its contents into ~/.ssh/authorized_keys.
  3. Make sure the permissions are correct: the ~/.ssh folder must be 700 and the authorized_keys file must be 600.

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

  1. A key without a passphrase on a work laptop: one stolen laptop means losing access to every server. Use a passphrase plus an agent.
  2. Copying the private key: a private key must never be sent over chat, email, or pastebin.
  3. 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.
  4. Not restricting keys: use the options in authorized_keys (such as from="client-ip", command=) to restrict when a key may be used.
  5. Loose file permissions: make sure of chmod 600 authorized_keys and chmod 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 the AUTHORIZED_KEYS FILE FORMAT section.
  • 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).
Open for Contributors

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.

Discussion & Comments