CrackOps
Interview PrepTracksLinuxAWS LabSoonInterview RoomSoon
Sign inStart free
CrackOps

Interview prep for the people who keep production up.

[email protected]
ProductInterview PrepTracksDashboardInterview Room
RolesDevOpsSREPlatformCloud
CompanyAboutContactPrivacyTerms
© CrackOps
Linux / Security & Hardening / ssh-keygen

ssh-keygen

Create and manage SSH keys

Beginnersshkeysauthentication

ssh-keygen creates the key pairs used to log in to servers and Git hosts without passwords, shows key fingerprints, changes passphrases and cleans stale host keys out of known_hosts. Ed25519 keys are the modern default: short, fast and secure.

Syntaxssh-keygen [-t ed25519|rsa|ecdsa] [-b BITS] [-C COMMENT] [-f FILE] [-N PASSPHRASE] | -l -f FILE | -y -f FILE | -p -f FILE | -R HOST

Watch it run

You need a key to log in to servers and push to Git without typing passwords.

devops@devops-lab: ~ — zsh — 96×28
➜ ~
Step 1 of 3
ssh-keygen -t ed25519 -C "devops@devops-lab"

Enter accepts the default path. Type a passphrase: if the laptop is stolen, the key file alone is useless. ssh-agent will remember it so you type it once per session. The randomart is just a visual fingerprint.

Anatomy of a command

$
commandssh-keygen

The OpenSSH key tool: generates, inspects and converts keys.

How it works

ssh-keygen generates a key pair with OpenSSL or OpenSSH's own crypto: for Ed25519, 32 random bytes from the kernel's random generator become the private key and the public key is derived from them. The private key is written in OpenSSH's format (-----BEGIN OPENSSH PRIVATE KEY-----) with mode 600; if you give a passphrase it is encrypted with AES-256 using a key derived from the passphrase by bcrypt with -a rounds. The public key is one line: type, base64 key blob, comment. That line is what goes into a server's ~/.ssh/authorized_keys or a Git host. During login the server never sees the private key: the client signs data from the session and the server checks the signature with the public key. A fingerprint is a SHA-256 hash of the public key blob, used to compare keys quickly. The same tool manages host keys (ssh-keygen -A creates missing /etc/ssh/ssh_host_* keys) and known_hosts entries (-F to find, -R to remove).

Options you'll actually use

-t TYPEKey type: ed25519 (default choice), rsa, ecdsa, or ed25519-sk for hardware keys
-b BITSKey size for RSA (use 4096); ignored for Ed25519
-C COMMENTComment at the end of the public key (who/where)
-f FILEOutput file (private key); .pub is added for the public key
-N PASSPassphrase on the command line ("" for none, e.g. CI deploy keys)
-a ROUNDSKDF rounds protecting the passphrase (default 16)
-l -f FILEShow a key's fingerprint (-E md5 for the old format)
-y -f FILEPrint the public key derived from a private key
-p -f FILEChange or add a passphrase without changing the key
-R HOSTRemove a host's entries from known_hosts
-F HOSTFind a host in known_hosts (works with hashed entries)
-qQuiet: no randomart, for scripts

Common mistakes

ssh-keygen -t rsa -b 1024 (or a default 2048-bit RSA key copied from an old guide)

Short RSA keys are weak, and some modern servers reject ssh-rsa signatures with SHA-1 altogether.

ssh-keygen -t ed25519 (or -t rsa -b 4096 where Ed25519 isn't supported)
Sharing one private key between people or between laptops and CI

You can't tell who logged in, and revoking access for one person means rotating the key everywhere.

One key per person per device, one deploy key per pipeline
Pasting the private key (id_ed25519) into authorized_keys or a Git host

Only the .pub belongs there. Anyone who sees the private key can impersonate you.

cat ~/.ssh/id_ed25519.pub, or ssh-copy-id
Answering 'yes' to the overwrite prompt when ssh-keygen says the key already exists

Your old private key is replaced; every server that only knew the old public key now locks you out.

Use -f to write the new key to a different file

Interview questions

Practice

Try these on a Linux machine of your own. Each task has a hint and a solution.

  1. 1

    Create an Ed25519 key at ~/.ssh/lab_key with the comment devops@lab and no passphrase.

  2. 2

    Show the fingerprint of that key, then rebuild its public key from the private key and compare.

  3. 3

    Add a passphrase to ~/.ssh/lab_key without changing the key itself, and confirm the fingerprint is unchanged.

  4. 4

    Make the private key readable by everyone, observe what ssh-keygen -y says, then fix the permissions.

Related commands

ssh-copy-idInstall your public key on a serversshLog in to remote servers securelyopensslInspect certificates, test TLS and create keys and CSRschmodChange file permissions
Previousbash: argumentsNextssh-copy-id
Linux / Security & Hardening / ssh-keygen

ssh-keygen

Create and manage SSH keys

Beginnersshkeysauthentication

ssh-keygen creates the key pairs used to log in to servers and Git hosts without passwords, shows key fingerprints, changes passphrases and cleans stale host keys out of known_hosts. Ed25519 keys are the modern default: short, fast and secure.

Syntaxssh-keygen [-t ed25519|rsa|ecdsa] [-b BITS] [-C COMMENT] [-f FILE] [-N PASSPHRASE] | -l -f FILE | -y -f FILE | -p -f FILE | -R HOST

Watch it run

You need a key to log in to servers and push to Git without typing passwords.

devops@devops-lab: ~ — zsh — 96×28
➜ ~
Step 1 of 3
ssh-keygen -t ed25519 -C "devops@devops-lab"

Enter accepts the default path. Type a passphrase: if the laptop is stolen, the key file alone is useless. ssh-agent will remember it so you type it once per session. The randomart is just a visual fingerprint.

Anatomy of a command

$
commandssh-keygen

The OpenSSH key tool: generates, inspects and converts keys.

How it works

ssh-keygen generates a key pair with OpenSSL or OpenSSH's own crypto: for Ed25519, 32 random bytes from the kernel's random generator become the private key and the public key is derived from them. The private key is written in OpenSSH's format (-----BEGIN OPENSSH PRIVATE KEY-----) with mode 600; if you give a passphrase it is encrypted with AES-256 using a key derived from the passphrase by bcrypt with -a rounds. The public key is one line: type, base64 key blob, comment. That line is what goes into a server's ~/.ssh/authorized_keys or a Git host. During login the server never sees the private key: the client signs data from the session and the server checks the signature with the public key. A fingerprint is a SHA-256 hash of the public key blob, used to compare keys quickly. The same tool manages host keys (ssh-keygen -A creates missing /etc/ssh/ssh_host_* keys) and known_hosts entries (-F to find, -R to remove).

Options you'll actually use

-t TYPEKey type: ed25519 (default choice), rsa, ecdsa, or ed25519-sk for hardware keys
-b BITSKey size for RSA (use 4096); ignored for Ed25519
-C COMMENTComment at the end of the public key (who/where)
-f FILEOutput file (private key); .pub is added for the public key
-N PASSPassphrase on the command line ("" for none, e.g. CI deploy keys)
-a ROUNDSKDF rounds protecting the passphrase (default 16)
-l -f FILEShow a key's fingerprint (-E md5 for the old format)
-y -f FILEPrint the public key derived from a private key
-p -f FILEChange or add a passphrase without changing the key
-R HOSTRemove a host's entries from known_hosts
-F HOSTFind a host in known_hosts (works with hashed entries)
-qQuiet: no randomart, for scripts

Common mistakes

ssh-keygen -t rsa -b 1024 (or a default 2048-bit RSA key copied from an old guide)

Short RSA keys are weak, and some modern servers reject ssh-rsa signatures with SHA-1 altogether.

ssh-keygen -t ed25519 (or -t rsa -b 4096 where Ed25519 isn't supported)
Sharing one private key between people or between laptops and CI

You can't tell who logged in, and revoking access for one person means rotating the key everywhere.

One key per person per device, one deploy key per pipeline
Pasting the private key (id_ed25519) into authorized_keys or a Git host

Only the .pub belongs there. Anyone who sees the private key can impersonate you.

cat ~/.ssh/id_ed25519.pub, or ssh-copy-id
Answering 'yes' to the overwrite prompt when ssh-keygen says the key already exists

Your old private key is replaced; every server that only knew the old public key now locks you out.

Use -f to write the new key to a different file

Interview questions

Practice

Try these on a Linux machine of your own. Each task has a hint and a solution.

  1. 1

    Create an Ed25519 key at ~/.ssh/lab_key with the comment devops@lab and no passphrase.

  2. 2

    Show the fingerprint of that key, then rebuild its public key from the private key and compare.

  3. 3

    Add a passphrase to ~/.ssh/lab_key without changing the key itself, and confirm the fingerprint is unchanged.

  4. 4

    Make the private key readable by everyone, observe what ssh-keygen -y says, then fix the permissions.

Related commands

ssh-copy-idInstall your public key on a serversshLog in to remote servers securelyopensslInspect certificates, test TLS and create keys and CSRschmodChange file permissions
Previousbash: argumentsNextssh-copy-id