Filters
Zo zoek je
  • Gewone woorden vinden namen, teksten en voorbeelden.
  • "twee woorden" vindt precies die zin.
  • --naam of -x vindt een optie.
  • #woord zoekt alleen in commando's.
  • @grep vindt lessen en labs die grep gebruiken.
  • Filters: cat:network · in:labs · in:notes · level:rhcsa · ch:8 · root:yes. Zet ! voor een woord om het weg te laten. OR tussen woorden betekent één ervan.
  • ↑ ↓ bewegen · Enter openen · Esc sluiten · / zoeken

ssh-keygen

Create SSH key pairs, change their passphrase, show fingerprints and clean the known_hosts file.

Niveau
RHCSA-examen
Groep
SSH en werken op afstand
Root nodig
Nee
Pakket
openssh

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

Wat het doet

ssh-keygen makes a key pair: a private key (keep it secret) and a public key (.pub, you copy it to servers). The default type today is ed25519: small, fast and secure. RSA keys should be at least 3072 bits.

It also does key housekeeping: show the fingerprint of a key, add or change a passphrase, remove an old host key from known_hosts, and sign keys with a certificate authority.

Zo schrijf je het

ssh-keygen [-q] [-t TYPE] [-b BITS] [-C COMMENT] [-f FILE] [-N PASSPHRASE]
ssh-keygen -p [-f FILE]
ssh-keygen -l [-v] [-E HASH] -f FILE
ssh-keygen -R HOSTNAME [-f KNOWN_HOSTS]
ssh-keygen -F HOSTNAME [-f KNOWN_HOSTS]
ssh-keygen -s CA_KEY -I ID [-h] [-n PRINCIPALS] [-V VALIDITY] PUBKEY

Opties 42

Meest gebruikt

Make keys

-t type
Key type: ed25519 (recommended), rsa, ecdsa, ed25519-sk / ecdsa-sk (hardware keys), dsa (old, do not use).
-b bits
Key size (for RSA: 3072 or 4096).
-C comment
A comment in the public key, like "student@server1" or an email.
-f file
The key file name (default ~/.ssh/id_<type>).
-N passphrase
The new passphrase (-N "" = no passphrase, for automation).
-a rounds
KDF rounds that protect the passphrase (higher = slower to crack).
-A
Make all missing host keys (used by the system for /etc/ssh).
-q
Quiet.
-Z cipher
The cipher that encrypts the private key file.
-O option
Options for FIDO keys or certificates (like verify-required).
-w provider
A FIDO security key provider library.
-K
Download resident keys from a FIDO security key.

Look at and change keys

-l
Show the fingerprint of a key file.
-v
Verbose (with -l: also show the random art picture).
-E hash
The fingerprint hash: sha256 (default) or md5.
-B
Show the bubblebabble digest of a key.
-y
Print the public key of a private key file.
-p
Change (or add) the passphrase of a private key.
-P passphrase
The old passphrase (with -p).
-c
Change the comment of a key.
-e
Export a key in another format (like RFC4716 for other SSH tools).
-i
Import a key from another format.
-m format
The format for -e / -i / -p: RFC4716, PKCS8, PEM.

known_hosts

-R host
Remove all keys of a host from known_hosts.
-F host
Find a host in known_hosts.
-H
Hash the host names in known_hosts (hide which servers you use).
-r host
Print a DNS SSHFP record for the host keys.
-g
Use the generic DNS format with -r.

Certificates and revocation

-s ca_key
Sign a public key with a CA key (make a certificate).
-I id
The certificate identity (with -s).
-h
Make a host certificate instead of a user certificate.
-n principals
The users or hosts the certificate is valid for.
-V interval
How long the certificate is valid, like +52w.
-z serial
The serial number of the certificate.
-L
Print the content of a certificate.
-D pkcs11
Get the public keys from a PKCS#11 library (smart card).
-U
The CA key is in ssh-agent.
-k
Make a Key Revocation List (KRL).
-u
Update a KRL.
-Q
Test if keys are in a KRL.

Signing files and other

-Y sign, -Y verify, -Y check-novalidate, -Y find-principals, -Y match-principals
Sign and verify files or git commits with SSH keys.
-M generate, -M screen
Make and test moduli for Diffie-Hellman.

Voorbeelden 12

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

  1. 1Make a modern key pair

    ssh-keygen -t ed25519 -C "student@server1"

    Press Enter for the default file. Type a passphrase (recommended for people).

  2. 2Make a key without passphrase for automation

    ssh-keygen -t ed25519 -N "" -f ~/.ssh/backup_job -C "backup job server1"

    For cron jobs and CI. Protect the file and limit the key on the server.

  3. 3Make an RSA key for old systems

    ssh-keygen -t rsa -b 4096 -f ~/.ssh/id_rsa_legacy

    Only when a device does not support ed25519.

  4. 4Show the fingerprint of a key

    ssh-keygen -lf ~/.ssh/id_ed25519.pub
    256 SHA256:Xh7… student@server1 (ED25519)

    Compare fingerprints when you check which key is allowed on a server.

  5. 5Add or change a passphrase

    ssh-keygen -p -f ~/.ssh/id_ed25519

    The key stays the same; only its protection changes. No change needed on servers.

  6. 6Remove an old host key after a server reinstall

    ssh-keygen -R server2

    Removes server2 from ~/.ssh/known_hosts. Verify the new key before you connect again.

  7. 7Check a server's host key fingerprint on the server itself

    ssh-keygen -lf /etc/ssh/ssh_host_ed25519_key.pub

    Compare with the fingerprint your client shows at the first connect.

  8. 8Get the public key back from a private key

    ssh-keygen -y -f ~/.ssh/id_ed25519 > ~/.ssh/id_ed25519.pub

    When the .pub file is lost.

  9. 9Find a host in known_hosts

    ssh-keygen -F server2

    Shows if (and which) key you trust for server2.

  10. 10Recreate missing host keys on a server

    ssh-keygen -A
    systemctl restart sshd

    Needed after a VM template was cleaned of its host keys.

  11. 11Sign a user key with your CA (short-lived access)

    ssh-keygen -s ~/ca/user_ca -I alice-oncall -n alice -V +8h alice.pub

    Servers that trust the CA accept alice for 8 hours, without editing authorized_keys.

  12. 12Sign a git commit file with an SSH key

    ssh-keygen -Y sign -f ~/.ssh/id_ed25519 -n file release.tar.gz

    Makes release.tar.gz.sig. Others verify it with -Y verify.

Let op

  • Never copy or send the private key (the file without .pub).
  • Private key files must be mode 600; otherwise ssh refuses to use them.
  • DSA keys are disabled in new OpenSSH versions. Use ed25519.

Bestanden

~/.ssh/id_ed25519, ~/.ssh/id_ed25519.pub
Default user key pair.
~/.ssh/known_hosts
Trusted host keys.
/etc/ssh/ssh_host_*_key
The host keys of the server.

Tips om te onthouden