Skip to main content

Understanding SSH-keygen

ssh-keygen is an OpenSSH utility for generating and managing cryptographic keys used by SSH. It is foundational for secure server access because it enables key-based authentication, which is stronger and more operationally reliable than password-based SSH when deployed correctly.

Key-based authentication is widely used in production because:

  • Keys are resistant to brute-force attacks (unlike passwords)
  • Keys can be scoped, rotated, and organized per environment
  • Keys integrate cleanly with automation (CI/CD, provisioning, configuration management)
  • Keys can be protected with passphrases and loaded with ssh-agent
Scope

Focus: practical understanding of ssh-keygen for creating, inspecting, rotating, converting, and troubleshooting SSH keys on modern Linux/macOS/WSL environments.

Security note

The private key must remain secret. Treat it like a password with higher impact. Anyone who obtains the private key can authenticate as you (especially if the key has no passphrase).

Key Pair Basics

A standard SSH key pair has two files:

FilePurposeExample
Private keySecret credential; never share~/.ssh/id_ed25519
Public keyInstalled on servers/services~/.ssh/id_ed25519.pub

Where keys are used

  • Server access: public key placed in ~/.ssh/authorized_keys
  • Git hosting: public key uploaded to GitHub/GitLab/Bitbucket
  • Automation: keys used by CI/CD runners or deployment agents

Modern OpenSSH best default:

  • ed25519 (fast, compact, strong, and widely supported)

Compatibility option:

  • rsa with 4096 bits (for older systems or strict compatibility requirements)

Avoid:

  • dsa (insecure/deprecated)
  • ecdsa (generally not preferred when ed25519 is available)
Recommendation

Use ed25519 for new keys unless you must support a legacy system that does not accept it.

Basic Syntax

ssh-keygen [options]

Running with no options starts an interactive wizard:

ssh-keygen

Typical interactive prompts:

  • File to save the key
  • Passphrase (optional but recommended)

Generate Keys (Common Patterns)

ssh-keygen -t ed25519

Generate ED25519 with comment (helps identify keys)

ssh-keygen -t ed25519 -C "you@example.com"
ssh-keygen -t ed25519 -f ~/.ssh/wpserver_key -C "wpserver"

Generate RSA 4096 (compatibility)

ssh-keygen -t rsa -b 4096 -C "you@example.com"
Passphrases

A passphrase encrypts your private key at rest. Without a passphrase, anyone with the key file can use it immediately.

Core Options and Flags

The table below covers commonly used and security-relevant flags.

FlagPurposeExampleNotes
------
-tKey typessh-keygen -t ed25519ed25519 recommended
-bKey size (bits)ssh-keygen -t rsa -b 4096Relevant for RSA; ignored by ED25519
-CComment labelssh-keygen -C "deploy@prod"Stored in public key line for identification
-fOutput file pathssh-keygen -f ~/.ssh/prod_keyAvoid overwriting defaults when managing many keys
-NPassphrase (non-interactive)ssh-keygen -N "pass" -f ~/.ssh/keyUseful for automation; treat carefully
-qQuiet modessh-keygen -q -t ed25519 -f ~/.ssh/keyUseful for scripts
-pChange passphrasessh-keygen -p -f ~/.ssh/id_ed25519Rotate passphrase without changing key material
-yExtract public key from privatessh-keygen -y -f ~/.ssh/id_ed25519Useful if .pub is missing
-lShow fingerprintssh-keygen -l -f ~/.ssh/id_ed25519.pubIdentifies a key uniquely
-EFingerprint hash formatssh-keygen -l -E sha256 -f key.pubSHA256 default; MD5 supported
-RRemove host from known_hostsssh-keygen -R example.comFix host key mismatch after rebuild
-FFind host in known_hostsssh-keygen -F example.comSearch known_hosts entries
-AGenerate missing host keyssudo ssh-keygen -AFor SSH server host keys (not user keys)
-kCreate Key Revocation List (KRL)ssh-keygen -k -f revoked.krl bad.pubAdmin use; advanced access control

Interactive Prompts (What They Mean)

When generating a key, typical output:

Generating public/private ed25519 key pair.
Enter file in which to save the key (/home/user/.ssh/id_ed25519):
  • Press Enter to accept default path
  • Or provide a custom key name (recommended per-server/per-purpose)

Passphrase prompt:

Enter passphrase (empty for no passphrase):
Enter same passphrase again:
  • A passphrase protects the private key at rest
  • If the passphrase is lost, the private key cannot be used

Inspect and Verify Keys

Show public key contents

cat ~/.ssh/id_ed25519.pub

Show fingerprint (most common audit command)

ssh-keygen -lf ~/.ssh/id_ed25519.pub

Example output:

256 SHA256:abcdEFghijk... you@example.com (ED25519)

Show fingerprint using a specific hash format

ssh-keygen -lf -E sha256 ~/.ssh/id_ed25519.pub

MD5 (legacy display; not recommended for new workflows):

ssh-keygen -lf -E md5 ~/.ssh/id_ed25519.pub

Confirm file permissions (important)

ls -l ~/.ssh/id_ed25519*

Recommended:

-rw- id_ed25519
-rw-r--r-- id_ed25519.pub

Recover a Missing Public Key

If the .pub file is deleted but the private key still exists:

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

Verify:

ssh-keygen -lf ~/.ssh/id_ed25519.pub

Change or Remove a Private Key Passphrase

Change passphrase:

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

Remove passphrase (enter old passphrase, then set new passphrase empty):

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

Known Hosts Management (Host Key, Not User Key)

Host keys are server identity records stored locally in ~/.ssh/known_hosts. They prevent man-in-the-middle attacks by detecting unexpected server identity changes.

Find an entry

ssh-keygen -F your_server_ip

Remove an entry (common after server rebuild)

ssh-keygen -R your_server_ip

Typical output:

# Host your_server_ip found: line 12
/home/user/.ssh/known_hosts updated.

SSH Server Host Keys (System-Level)

These are not your personal keys. They belong to the SSH server (sshd) and identify the server to clients.

Generate missing host keys (server-side, as root):

sudo ssh-keygen -A

Check host key fingerprints (server-side):

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

Key Rotation Strategy

Two common approaches:

A) Rotate passphrase only (same key material)

  • Useful when the key is still trusted but you want better protection
ssh-keygen -p -f ~/.ssh/wpserver_key

B) Rotate the key pair (new key material)

  • Recommended after a suspected compromise, personnel changes, or compliance requirements

Steps:

  1. Generate a new key with a distinct name
  2. Install the new public key on the server
  3. Test login with the new key
  4. Remove the old public key from authorized_keys

Example:

ssh-keygen -t ed25519 -f ~/.ssh/wpserver_key_2026 -C "wpserver-rotation"

Common Use Cases

Use CaseCommandWhy it matters
---
One key per VPSssh-keygen -t ed25519 -f ~/.ssh/vps_hetzner -C "hetzner"Limits blast radius if one key leaks
Deployment key for Gitssh-keygen -t ed25519 -f ~/.ssh/id_github_deploy -C "github-deploy"Separates deploy access from admin access
Restore missing .pubssh-keygen -y -f ~/.ssh/id_ed25519 > ~/.ssh/id_ed25519.pubAvoids regenerating keys unnecessarily
Passphrase rotationssh-keygen -p -f ~/.ssh/vps_keyImproves security without replacing key
Fix host key mismatchssh-keygen -R server_ipCleans old host identity after rebuild
Audit known_hostsssh-keygen -lf ~/.ssh/known_hostsReview stored server identities

Best Practices

PracticeReason
---
Prefer ed25519Strong, fast, modern default
Use a unique key per purposeSeparates admin, deploy, and automation access
Add meaningful comments (-C)Easier key inventory and server auditing
Protect private keys with passphrasesReduces impact if file is stolen
Use ssh-agent instead of removing passphrasesKeeps keys encrypted on disk
Enforce strict file permissionsPrevents SSH from refusing keys
Back up private keys securelyRecovery if the local device fails (use encrypted storage)
Rotate keys on compromise or policy scheduleLimits long-term exposure
Key storage

Do not store private keys in plain text cloud drives, public repos, or shared folders. Use encrypted vaults or OS keychains where possible.

Quick Cheat Sheet

TaskCommand
--
Generate ED25519 keyssh-keygen -t ed25519 -C "you@example.com"
Generate RSA 4096 keyssh-keygen -t rsa -b 4096 -C "you@example.com"
Custom filenamessh-keygen -t ed25519 -f ~/.ssh/myserver_key -C "myserver"
Change passphrasessh-keygen -p -f ~/.ssh/myserver_key
Extract public keyssh-keygen -y -f ~/.ssh/myserver_key > ~/.ssh/myserver_key.pub
Show fingerprintssh-keygen -lf ~/.ssh/myserver_key.pub
Remove known_hosts entryssh-keygen -R your_server_ip
Find known_hosts entryssh-keygen -F your_server_ip
Generate server host keyssudo ssh-keygen -A