CodeBrowser

We earn commissions when you shop through the links below. Learn more

How to Generate an SSH Key on Windows, Mac and Linux

A step-by-step guide to creating an SSH key with ssh-keygen or PuTTYgen, protecting it with a passphrase, loading it into ssh-agent, and adding it to your server, GitHub or GitLab.

Windows, Mac & Linuxssh-keygen & PuTTYgenGitHub & GitLabTroubleshooting

Quick version

Generate an SSH key in 30 seconds

On Windows 10 or 11 (PowerShell), macOS or Linux, open a terminal and run:

ssh-keygen -t ed25519 -C "work-laptop"

Press Enter to save it in the default place, type a passphrase (or press Enter twice for none), and you’re done. You now have a private key, id_ed25519, and a public key, id_ed25519.pub, in your .ssh folder.

Prefer not to use the terminal?

Our free SSH Key Generator creates Ed25519 or RSA keys right in your browser and gives you both OpenSSH and PuTTY (.ppk) files to download. Nothing is uploaded.

For keys that protect production servers, generating them on your own machine with ssh-keygen is still the gold standard.

Windows

How to generate an SSH key on Windows

Option 1: PowerShell (recommended)

  1. Open Windows Terminal or PowerShell. The ssh-keygen command is built into Windows 10 (1809 and later) and Windows 11.
  2. Run ssh-keygen -t ed25519 -C "work-laptop".
  3. Press Enter to accept the location, C:\Users\YourName\.ssh\id_ed25519.
  4. Type a passphrase twice, or press Enter twice to skip it.
  5. Copy the public key: Get-Content $env:USERPROFILE\.ssh\id_ed25519.pub | Set-Clipboard

Option 2: PuTTYgen (for PuTTY users)

  1. Open PuTTYgen, which installs with PuTTY.
  2. Under Type of key to generate, choose EdDSA (Ed25519), or RSA with 4096 bits.
  3. Click Generate and move your mouse over the blank area until the bar fills.
  4. Add a Key comment and a Key passphrase, then click Save private key to get a .ppk file.
  5. Copy the text in Public key for pasting into OpenSSH authorized_keys file. That’s the key you put on servers.

Need the private key in OpenSSH format too, for Git or VS Code? In PuTTYgen, use Conversions → Export OpenSSH key.

macOS & Linux

How to generate an SSH key on Mac and Linux

Open Terminal

On a Mac, press Cmd+Space and type Terminal. On Linux, open your terminal app.

Run ssh-keygen

Type ssh-keygen -t ed25519 -C "work-laptop" and press Enter.

Choose where to save

Press Enter for ~/.ssh/id_ed25519, or type a new name to keep several keys.

Set a passphrase

Type a passphrase twice. It encrypts the key on disk if your laptop is ever lost.

Copy the public key on a Mac

pbcopy < ~/.ssh/id_ed25519.pub

Show the public key on Linux

cat ~/.ssh/id_ed25519.pub

Reference

Useful ssh-keygen options

CommandWhat it does
ssh-keygen -t ed25519Create an Ed25519 key, the modern default
ssh-keygen -t rsa -b 4096Create a 4096-bit RSA key for older systems that don’t support Ed25519
-C "label"Add a comment so you can tell keys apart, such as the computer name
-f ~/.ssh/github_workSave the key under a custom file name
ssh-keygen -p -f ~/.ssh/id_ed25519Add, change or remove the passphrase on an existing key
ssh-keygen -y -f ~/.ssh/id_ed25519Print the public key for a private key, if you lost the .pub file
ssh-keygen -l -f ~/.ssh/id_ed25519.pubShow the key’s fingerprint to check which key is which

Passphrases

Use ssh-agent so you only type your passphrase once

A passphrase protects your key, but typing it for every connection gets old fast. ssh-agent remembers the unlocked key until you log out. Set it up once:

Windows (admin PowerShell, once)

Get-Service ssh-agent | Set-Service -StartupType Automatic
Start-Service ssh-agent
ssh-add $env:USERPROFILE\.ssh\id_ed25519

macOS (saves it in Keychain)

ssh-add --apple-use-keychain ~/.ssh/id_ed25519

Linux

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

On a Mac, add AddKeysToAgent yes and UseKeychain yes under Host * in your SSH config so the key loads automatically after a restart. Our SSH Config Generator can add those lines for you.

Next step

Add your SSH key to a server, GitHub or GitLab

Always share the public key, the one ending in .pub. Your private key never leaves your computer.

A Linux server or VPS

Run ssh-copy-id -i ~/.ssh/id_ed25519.pub user@server, or paste the key into ~/.ssh/authorized_keys on the server. Full steps in our guide on how to SSH into a server.

GitHub

Go to Settings → SSH and GPG keys → New SSH key, paste the public key and click Add SSH key. Test it with ssh -T git@github.com.

GitLab

Go to Edit profile → SSH Keys → Add new key, paste the public key and save. Test it with ssh -T git@gitlab.com.

Troubleshooting

SSH key problems and how to fix them

Most SSH key problems come down to file permissions or the wrong key being offered. Not sure what a permission number means? Check it with our Chmod Calculator.

ProblemFix
WARNING: UNPROTECTED PRIVATE KEY FILE!The private key is readable by other users. Run chmod 600 ~/.ssh/id_ed25519 and chmod 700 ~/.ssh.
Permission denied (publickey)The server didn’t accept your key. Check the username, that the public key is in that user’s authorized_keys, and try ssh -i ~/.ssh/id_ed25519 user@server.
Could not open a connection to your authentication agentssh-agent isn’t running. Start it with the commands for your system above, then run ssh-add again.
ssh-keygen: command not found on WindowsThe OpenSSH client is missing. Install it in Settings → Optional features, or use PuTTYgen.
GitHub says Permission denied but the key works on serversGitHub may be getting a different key. Run ssh -vT git@github.com to see which keys are offered, and set IdentityFile for github.com in your SSH config.

Need a server?

Put your new SSH key to work

Most VPS hosts let you paste your public key when you create a server, so you can log in with it from the very first connection.

Best overall

DigitalOcean

Simple cloud servers with great docs. Add your SSH key when you create a Droplet.

From about $4/month

View DigitalOcean deals
Most RAM for the money

Hostinger

4 GB of RAM and NVMe storage on the entry VPS plan, with an easy control panel.

From about $6.49/month on a 24-month term

View Hostinger deals
Best budget

RackNerd

Yearly KVM VPS deals for about the price of one month elsewhere. Great for learning.

From about $11/year on promo

View RackNerd deals

Prices are approximate and change often. Check each provider’s site before you buy.

FAQ

SSH key questions, answered

Should I use Ed25519 or RSA?

Use Ed25519. It is the default in modern OpenSSH, recommended by GitHub, faster and shorter. Only use RSA, with at least 3072 or 4096 bits, if you must connect to an older system that does not support Ed25519.

Do I need a passphrase?

It is strongly recommended for keys on laptops and personal computers, because it protects the key if the device is lost or stolen. Keys used by automated scripts on a secured server are often created without one.

Where are my SSH keys stored?

In the .ssh folder in your home directory: ~/.ssh on macOS and Linux, and C:\Users\YourName\.ssh on Windows. The private key has no extension and the public key ends in .pub.

Can I use the same SSH key on multiple computers?

You can copy it, but it is safer to create a separate key on each computer and add each public key where you need it. That way you can revoke one device without affecting the others.

How do I have more than one SSH key?

Give each key its own file name with ssh-keygen -f, for example ~/.ssh/github_work, then tell SSH which key to use for each host with IdentityFile in your SSH config file.

What happens if I lose my private key?

There is no way to recover it. Create a new key pair, add the new public key to your servers and accounts, and remove the old public key from them.

Need a server to use your new key on?

Compare the best VPS hosting for developers, from budget yearly deals to developer-friendly clouds. Most let you add your SSH key when you create the server.

Compare VPS hosting