Geavanceerd
Zoeken inHow-to-gidsen
Zo zoek je
  • Typ de taak in je eigen woorden (poort vinden, firewall openen, schijf vol) of plak een foutmelding tussen aanhalingstekens.
  • Kies "Snel antwoord" om alleen de commando's te zien, of een gebied om in één vakgebied te blijven.
  • "twee woorden" vindt precies die zin.
  • Gewone woorden vinden namen, teksten en voorbeelden.
  • ↑ ↓ bewegen · Enter openen · Esc sluiten · / zoeken
Probeer:

How-to 3Security · Linux

How to log in with SSH keys instead of passwords

SSH keys are safer than passwords (they can not be guessed) and faster (no typing, perfect for Ansible and scripts). You make a key pair on your computer, put the public key on the server, and log in. This how-to does that from Linux, macOS and Windows, adds the key to ssh-agent, makes a short host name, and shows how to check the login really uses the key.

  • 5 Lessen
  • 11 Commando's

Dit deel is nog niet vertaald. Je ziet de Engelse tekst.

$ als gewone gebruiker# als root (of met sudo)

3.1

The quick answer

On your computer (not on the server):

ssh-keygen -t ed25519 -C "laptop-2026"
ssh-copy-id admin@192.0.2.10
ssh admin@192.0.2.10

Press Enter for the default file, and give the key a passphrase.

3.2

Step by step

  1. Make a key pair. ed25519 is short, fast and secure (use -t rsa -b 4096 only for very old systems):
ssh-keygen -t ed25519 -C "laptop-2026"
Generating public/private ed25519 key pair.
Enter file in which to save the key (/home/alice/.ssh/id_ed25519):
Enter passphrase (empty for no passphrase):
Your identification has been saved in /home/alice/.ssh/id_ed25519
Your public key has been saved in /home/alice/.ssh/id_ed25519.pub

id_ed25519 is the private key — it never leaves your computer. id_ed25519.pub is the public key — that one goes to servers.

  1. Copy the public key to the server (you log in with the password one last time):
ssh-copy-id -i ~/.ssh/id_ed25519.pub admin@192.0.2.10

No ssh-copy-id (Windows PowerShell, old systems)? Do the same by hand:

cat ~/.ssh/id_ed25519.pub | ssh admin@192.0.2.10 'mkdir -p ~/.ssh && chmod 700 ~/.ssh && cat >> ~/.ssh/authorized_keys && chmod 600 ~/.ssh/authorized_keys'
  1. Type the passphrase only once per session with the agent:
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_ed25519
ssh-add -l

(macOS and most Linux desktops start the agent for you; Windows has the "OpenSSH Authentication Agent" service.)

  1. Give the server a short name in your client config:
~/.ssh/config
Host web1
    HostName 192.0.2.10
    User admin
    IdentityFile ~/.ssh/id_ed25519
    IdentitiesOnly yes

Now ssh web1, scp file web1:/tmp/ and rsync … web1: all use the key.

  1. When keys work for every admin, turn off password login on the server (PasswordAuthentication no) — see the OpenSSH guide. Keep a second session open while you change it.

3.3

Check that it worked

Force key-only login — if this works, the key is used:

ssh -o PasswordAuthentication=no -o BatchMode=yes web1 'echo key login OK'
key login OK
ssh -v web1 2>&1 | grep -E 'Authenticated|Offering'
debug1: Offering public key: /home/alice/.ssh/id_ed25519 ED25519 SHA256:…
Authenticated to 192.0.2.10 ([192.0.2.10]:22) using "publickey".

3.4

Common problems

ProblemCauseFix
Still asks for the passwordThe key is not in authorized_keys, or file rights are too open.On the server: chmod 700 ~/.ssh; chmod 600 ~/.ssh/authorized_keys; owner must be the user.
Permission denied (publickey) on RHEL although the key is thereSELinux label of .ssh is wrong (copied files).restorecon -Rv ~/.ssh.
Too many authentication failuresThe agent offers many keys before the right one.IdentitiesOnly yes and IdentityFile in ~/.ssh/config.
UNPROTECTED PRIVATE KEY FILE!Your private key is readable by others.chmod 600 ~/.ssh/id_ed25519.
Works for your user, not for rootRoot login is (correctly) not allowed.Log in as your user and use sudo.
no matching host key type foundVery old server or client algorithms.Update the old side; use ed25519/rsa-sha2 keys.

3.5

Go further

1Which key file goes to the server, and which never leaves your computer?

The .pub file goes to the server; the private key (id_ed25519) stays on your computer.

2How do you prove that a login uses the key and not a password?

Alle commando's in dit hoofdstuk