Network security

ssh-keygen: How to Generate SSH Keys Safely (Ed25519 Guide)

One command replaces passwords with a key pair that cannot be phished. Here is how to generate it, use it everywhere, and keep the private half safe.

Cysoro Security TeamPublished · 9 min read
$ ssh-keygen -t ed25519Generating public/privateed25519 key pair.SHA256:3IHVAkM0JE+9Qc...$ ▊ id_ed25519.pub NETWORK SECURITY ssh-keygen One command. No more passwords. Generate Ed25519 keys the right way
Quick answer

ssh-keygen is OpenSSH's command-line tool for creating SSH key pairs: a private key that stays on your computer and a public key you copy to servers. Run ssh-keygen -t ed25519 -C "[email protected]", accept the default location, set a strong passphrase, then add the public key to the server or service. Since OpenSSH 9.5, Ed25519 is the default key type, and it is the one GitHub recommends.

Key takeaways

  • ssh-keygen creates a key pair: a private key you never share and a public key you copy to servers, GitHub and other services.
  • Ed25519 is the recommended key type — run ssh-keygen -t ed25519 -C "[email protected]" and protect the key with a strong passphrase.
  • Use ssh-agent so you type the passphrase once per session, and never email, screenshot or upload your private key.

Passwords get phished, guessed and leaked. SSH keys do not: a private key on your computer and a public key on the server replace the password entirely, and one command creates them both. That command is ssh-keygen, and it is already installed on your machine.

What is ssh-keygen?

ssh-keygen is the command-line tool that ships with OpenSSH for generating, managing and converting SSH authentication keys. It creates a key pair: a private key that stays on your computer and a public key you copy to servers, GitHub, cloud hosts and anything else you log in to over SSH.

When you log in with a key, the server sends a challenge that only the holder of the private key can answer. The private key never crosses the network, which is why key login resists phishing in a way passwords cannot: there is no password to trick out of you. It is also why a stolen public key is harmless — it can only verify, never log in.

ssh-keygen is built into Linux and macOS as part of OpenSSH, and into Windows 10 and newer inside PowerShell and Command Prompt. Older Windows systems can get it through Git Bash or Windows Subsystem for Linux.

You do not need to install anything on a modern machine. If a server still lets you log in with a password, switching to keys is the single biggest security upgrade you can make to it — and our small business cybersecurity checklist lists it alongside MFA and patching for that reason.

How do you generate an SSH key with ssh-keygen?

Generate an Ed25519 key with one command, accept the default location, and set a strong passphrase. The whole process takes under a minute, and the tool walks you through every step.

  1. Check for an existing key first. Run ls ~/.ssh/id_*.pub on Linux or macOS, or dir $env:USERPROFILE\.ssh in PowerShell. If a key already exists, use it instead of overwriting it — overwriting breaks every server that trusts the old one.
  2. Open a terminal. Use Terminal on macOS or Linux, or PowerShell on Windows.
  3. Run the generation command, replacing the email with your own — it is only a label, so the key can be identified later:
    ssh-keygen -t ed25519 -C "[email protected]"
  4. Accept the default file location by pressing Enter. The key is saved as ~/.ssh/id_ed25519, or on Windows as C:\Users\yourname\.ssh\id_ed25519.
  5. Type a strong passphrase when prompted, then type it again. The passphrase encrypts the private key on disk.
  6. Note the fingerprint. ssh-keygen prints a SHA256 fingerprint and a small random-art image. The fingerprint is how you confirm later that a key is the one you think it is.

The output looks like this:

Generating public/private ed25519 key pair.
Your identification has been saved in /home/you/.ssh/id_ed25519
Your public key has been saved in /home/you/.ssh/id_ed25519.pub
The key fingerprint is:
SHA256:3IHVAkM0JE+9QcjcbQUEVURbkB5RTGnhk2wqc+2bQKM [email protected]

If ssh-keygen asks whether to overwrite an existing key, answer no and generate a named key instead with the -f flag, for example ssh-keygen -t ed25519 -f ~/.ssh/id_github -C "[email protected]". And if you are on a legacy system that does not support Ed25519, GitHub's documentation gives the fallback: ssh-keygen -t rsa -b 4096 -C "[email protected]".

Which key type should you choose: Ed25519 or RSA?

Choose Ed25519. It is faster, smaller and the recommended default since OpenSSH 9.5, and it is the key type GitHub recommends in its documentation. Choose RSA with 4096 bits only when an old system cannot use Ed25519.

Key typeKey sizeSpeedWhen to use it
Ed25519256 bitsFastestDefault for all modern systems
RSA4096 bits recommendedSlowerLegacy systems without Ed25519
ECDSA256, 384 or 521 bitsFastSystems that require NIST curves
DSA1024 bits—Do not use; obsolete and unsupported

DSA is dead: GitHub stopped accepting new DSA keys on March 15, 2022, and OpenSSH has deprecated them. RSA keys generated after November 2, 2021 must use a SHA-2 signature algorithm on GitHub, which older clients may not support — another reason to prefer Ed25519. ECDSA works well but offers no advantage over Ed25519 for most people, and its security depends on good random number generation, which has historically been a weak point.

Hardware security keys are a step up again. With a FIDO2 key such as a YubiKey, generate a key bound to the device with ssh-keygen -t ed25519-sk -C "[email protected]" and touch the key when prompted. The private key never leaves the hardware, so even a compromised laptop cannot leak it.

Where are your keys stored, and what do the files mean?

Your keys live in the .ssh folder inside your home directory: ~/.ssh/ on Linux and macOS, C:\Users\yourname\.ssh\ on Windows. Two files matter, and confusing them is the most common beginner mistake.

One key pair, two very different filesPRIVATE KEY — never share~/.ssh/id_ed25519Stays on your computer only.Encrypted with your passphrase.Anyone with this file is you.Never email, upload or paste it.PUBLIC KEY — safe to share~/.ssh/id_ed25519.pubOne line: type, key, comment.Copy it to servers and GitHub.It can verify you, never log in as you.Safe to paste into any authorized_keys.
The private key proves who you are; the public key is what servers check. Illustration by Cysoro.

The private key file, id_ed25519, has no extension and must be readable only by you. SSH refuses to use a private key with loose permissions — set the folder to 700 and the key to 600 with chmod 700 ~/.ssh and chmod 600 ~/.ssh/id_ed25519. If SSH complains about permissions, this is almost always the cause.

The public key file, id_ed25519.pub, is one line of text with three parts: the algorithm (ssh-ed25519), a long block of characters that is the actual key, and the comment you set with -C. View it with cat ~/.ssh/id_ed25519.pub and copy the whole line. Other useful files in .ssh are known_hosts, which records server fingerprints you have accepted, and config, which stores per-host settings such as which key to use.

How do you use your key with ssh-agent, servers and GitHub?

A key only helps once the public half is where you log in and the private half is loaded. Three steps cover almost every use: start the agent, copy the public key to the server, and test the login.

Start ssh-agent and add your key

The ssh-agent holds your decrypted key in memory so you type the passphrase once per session instead of once per connection. Start it and add your key:

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

On macOS, add the key to the keychain so the passphrase is remembered across restarts with ssh-add --apple-use-keychain ~/.ssh/id_ed25519, and add AddKeysToAgent yes to ~/.ssh/config for the hosts you use. On Windows, the agent is a service: run Get-Service -Name ssh-agent | Set-Service -StartupType Manual then Start-Service ssh-agent from an elevated PowerShell window, then ssh-add the key.

Copy the public key to a server

The simplest way is ssh-copy-id, which appends your public key to the server's ~/.ssh/authorized_keys file:

ssh-copy-id -i ~/.ssh/id_ed25519.pub [email protected]

It asks for your password one last time, then key login works. If ssh-copy-id is not available, copy the public key line manually into ~/.ssh/authorized_keys on the server and set the permissions: 700 on ~/.ssh and 600 on authorized_keys.

Add the key to GitHub

Print the public key with cat ~/.ssh/id_ed25519.pub, copy the whole line, then go to GitHub Settings, SSH and GPG keys, New SSH key, and paste it. Test it with ssh -T [email protected] — GitHub replies with your username when the key works. The same pattern applies to GitLab, Bitbucket and cloud hosts: paste the public key into the service, and it becomes your login credential there.

Once key login works, disable password login on your servers. In /etc/ssh/sshd_config, set PasswordAuthentication no and restart the SSH service — but test the key login in a second terminal first, so you cannot lock yourself out. This is the step that turns a key from a convenience into real security.

How do you manage multiple keys?

Use one key per purpose with a clear name, and let ~/.ssh/config choose the right one. Generating a named key is the same command with the -f flag: ssh-keygen -t ed25519 -f ~/.ssh/id_work -C "[email protected]".

A config file entry looks like this:

Host github.com
  IdentityFile ~/.ssh/id_github
  AddKeysToAgent yes

Host work-server
  HostName 203.0.113.10
  User deploy
  IdentityFile ~/.ssh/id_work

Now ssh work-server uses the work key automatically. Other useful management commands from the ssh-keygen manual: ssh-keygen -l -f ~/.ssh/id_ed25519 shows a key's fingerprint, ssh-keygen -y -f ~/.ssh/id_ed25519 prints the public key from a private key file, and ssh-keygen -p -f ~/.ssh/id_ed25519 changes a key's passphrase without generating a new key — the public key on your servers stays valid.

What security mistakes should you avoid?

Most SSH key incidents are not cryptographic failures — they are handling mistakes. The key itself is strong; the ways people store and share it are not.

  • Sharing the private key. Never email it, paste it into a chat, commit it to a repository or upload it to a server. If a private key has ever been somewhere it should not be, generate a new pair and revoke the old public key everywhere.
  • No passphrase. A key without a passphrase is a passwordless login for anyone who copies the file. Use a strong, unique passphrase and keep it in a password manager — and check it against known breaches with our free pwned password checker.
  • Reusing one key forever. Rotate keys when someone leaves a team, when a laptop is lost, or at least once a year for production systems.
  • Leaving password login on. A key plus an enabled password login is only as strong as the password. Disable password authentication once keys work.
  • Ignoring host key warnings. If SSH warns that a server's host key has changed, stop. It can mean a reinstall — or a machine-in-the-middle attack. Verify with the server's administrator before continuing.
  • Copying keys between machines insecurely. Do not move a private key over email or a shared drive. Generate a fresh pair on each machine instead.
A private key in a git repository is compromisedAccidentally committing a private key is common and public scanners find it within minutes. If it happens, treat the key as stolen: generate a new pair, revoke the old public key everywhere, and remove the file from git history.

How do you fix common ssh-keygen problems?

Almost every ssh-keygen problem has a short fix, and the error message usually says which one. These are the five you will meet most often.

Four common ssh-keygen problems, fixedPermission denied (publickey)Key not on the server, or wrong permissions — check authorized_keys and run ssh -v to see which key was offeredUNPROTECTED PRIVATE KEY FILEPermissions too open — run chmod 600 on the private key and chmod 700 on ~/.sshWARNING: REMOTE HOST IDENTIFICATION HAS CHANGEDServer key changed — verify with the admin, then remove the old entry with ssh-keygen -R hostnameEnter passphrase, but you set noneThe agent is offering a different key — specify it with ssh -i ~/.ssh/id_ed25519 or fix ~/.ssh/config
Most SSH key errors point at permissions, the wrong key, or a changed host key. Illustration by Cysoro.

Permission denied (publickey) means the server did not accept any key you offered. Check that the public key is in the server's authorized_keys, that permissions are 700 and 600, and run ssh -v user@server to see which keys were tried.

UNPROTECTED PRIVATE KEY FILE means the permissions are too open. Fix them with chmod 600 on the key and chmod 700 on the folder — SSH refuses to use the key until you do.

REMOTE HOST IDENTIFICATION HAS CHANGED means the server's host key differs from the one recorded in known_hosts. If the server was reinstalled, remove the old entry with ssh-keygen -R hostname and reconnect. If nobody reinstalled anything, treat it as suspicious and verify out of band first. This warning is also a classic sign of a machine-in-the-middle attack, which is why knowing the signs of a compromised device matters beyond servers.

Bad permissions on known_hosts or config usually comes from editing the files as the wrong user. Keep the whole .ssh directory owned by you and no more permissive than 700.

Agent prompts for the passphrase every time means the key is not loaded in the agent. Run ssh-add -l to list loaded keys; if yours is missing, add it again with ssh-add ~/.ssh/id_ed25519.

The bottom line

ssh-keygen turns a password problem into a key problem — and keys are easier to protect than passwords are to remember. Generate an Ed25519 key, give it a strong passphrase, load it in ssh-agent, and copy the public half wherever you log in.

Then finish the job: disable password login on your servers, keep a backup of the private key somewhere encrypted, and rotate keys when people or laptops leave. Five minutes of setup buys you logins that cannot be phished, guessed or leaked in a breach.

Frequently asked questions

What is ssh-keygen?

ssh-keygen is the command-line tool that comes with OpenSSH for generating, managing and converting SSH authentication keys. It creates a key pair: a private key that stays on your computer and a public key you copy to servers and services such as GitHub.

How do I generate an SSH key with ssh-keygen?

Open a terminal and run ssh-keygen -t ed25519 -C "[email protected]". Press Enter to accept the default file location, type a strong passphrase twice, and the tool creates ~/.ssh/id_ed25519 and ~/.ssh/id_ed25519.pub. Then copy the .pub file to the server or service you want to log in to.

What is the difference between Ed25519 and RSA keys?

Ed25519 keys are smaller, faster and the recommended default since OpenSSH 9.5, using a 256-bit key. RSA keys need 4096 bits for comparable security and are slower, but work on older systems that do not support Ed25519. Use Ed25519 unless a legacy system forces RSA.

Where are SSH keys stored?

On Linux and macOS they live in ~/.ssh/, usually as id_ed25519 for the private key and id_ed25519.pub for the public key. On Windows they live in C:\Users\yourname\.ssh\. The .ssh folder should only be accessible by you, and the private key file must not be readable by anyone else.

Do I need a passphrase on my SSH key?

Yes. The passphrase encrypts the private key, so a stolen laptop does not immediately give the thief access to your servers. Use a strong, unique passphrase and store it in a password manager. If you use ssh-agent, you only type it once per session.

How do I add my SSH key to GitHub?

Copy your public key with cat ~/.ssh/id_ed25519.pub, then go to GitHub Settings, SSH and GPG keys, New SSH key, and paste it in. GitHub accepts Ed25519 keys and RSA keys of 4096 bits; it stopped accepting new DSA keys on March 15, 2022.

What happens if I lose my SSH private key?

You cannot recover it. Generate a new key pair, add the new public key everywhere the old one was used, and remove the old public key from servers and services. A lost passphrase is the same problem: the manual states there is no way to recover a lost passphrase, so a new key must be generated.

Can I use the same SSH key on multiple servers?

Yes. The same public key can be added to as many servers and services as you like, and that is the normal setup. Some security teams prefer one key per device or per service so a compromise can be revoked narrowly, but for personal use one well-protected key is fine.

Sources

  1. GitHub Docs — Generating a new SSH key and adding it to the ssh-agent
  2. OpenBSD manual — ssh-keygen(1)
  3. Lafayette College — SSH Access
Written & fact-checked byCysoro Security Team

The engineers and analysts who build Cysoro's domain scanner, email-authentication checks and free security tools. We test what we write about every day — SPF, DKIM, DMARC, certificates, look-alike domains and phishing — and check each guide against the relevant standards, government guidance and vendor documentation.

  • Email authentication
  • Phishing & BEC
  • Domain & DNS security
  • Small-business security

Checked against the GitHub SSH documentation, the OpenBSD ssh-keygen manual and OpenSSH key-type guidance.

How we write and fact-check →

Free tools for this topic

No account needed.

See where your domain stands

Free for up to 3 domains. No card, no installs.

Start free