How to Set Up SSH Key Authentication on Linux

Generate an SSH key pair, copy the public key to a server, and log in without a password using ssh-keygen and ssh-copy-id.

Published September 21, 2026

Generate a key pair with ssh-keygen, copy the public key to the server with ssh-copy-id, then log in without a password. The private key never leaves your machine.

The concept

Key authentication uses a pair: a private key you keep secret and a public key placed in the server's ~/.ssh/authorized_keys. The server challenges you and you prove possession without sending the key.

Add a passphrase to the private key and use ssh-agent so you type it once per session.

Setup in 3 commands

ssh-keygen -t ed25519 -C "ada@laptop"
Your public key: ~/.ssh/id_ed25519.pub
ssh-copy-id ada@server.example.com
ssh ada@server.example.com
Welcome (no password asked)

Practical examples

Fix permissions on the server

chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys

A host alias

Host prod
  HostName server.example.com
  User ada
  IdentityFile ~/.ssh/id_ed25519

Disable password login (after testing!)

sudo sed -i 's/^#\?PasswordAuthentication.*/PasswordAuthentication no/' /etc/ssh/sshd_config
sudo systemctl reload ssh

Use the agent

eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_ed25519

Common mistakes

  • Sharing or committing the private key.
  • Wrong permissions on ~/.ssh, making sshd ignore the key.
  • Disabling passwords before verifying key login works.
  • Using no passphrase on a laptop.

Key takeaways

  • Private stays local; public goes in authorized_keys.
  • Prefer ed25519.
  • Test before disabling passwords.

FAQ

RSA or ed25519?

ed25519 is shorter, faster and recommended; use RSA 4096 only for old systems.

How do I use different keys per host?

Define Host blocks with IdentityFile in ~/.ssh/config.

More Linux articles