How to generate an SSH key and add it to GitHub or a server
Tested: we reproduced this error and confirmed the fix on Ubuntu 24.04 with OpenSSH 9.6, November 29, 2026.
Run this, press Enter to accept the default file and choose a passphrase:
ssh-keygen -t ed25519 -C "you@example.com"
It creates two files in ~/.ssh/: id_ed25519 (private, keep it secret) and id_ed25519.pub (public, the one you paste into GitHub or a server). We ran it with OpenSSH 9.6 and checked both files, the agent and an RSA fallback.
1. Check for an existing key
ls -l ~/.ssh/
If you already have id_ed25519 and id_ed25519.pub, you can reuse them. Running ssh-keygen with the same file name asks before overwriting (/home/you/.ssh/id_ed25519 already exists. Overwrite (y/n)?); answer n unless you mean to replace it, because anything using the old key stops working.
2. Generate the key
ssh-keygen -t ed25519 -C "you@example.com"
Press Enter to accept the default file, then type a passphrase twice (or press Enter twice for none). In our test the full exchange looked like this:
Generating public/private ed25519 key pair.
Enter file in which to save the key (/root/.ssh/id_ed25519):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_ed25519
Your public key has been saved in /root/.ssh/id_ed25519.pub
The key fingerprint is:
SHA256:GmMXVB0uhqYGhDfH8NFhsD3iT4KKatpKUU4e8ASA4Z8 you@example.com
It then prints a small “randomart” picture of the key, which you can ignore. The -C comment is only a label so you can tell keys apart later. For an older system that doesn’t support ed25519:
ssh-keygen -t rsa -b 4096 -C "you@example.com"
In our test the private key was created with -rw------- permissions and the public key with -rw-r--r--. Keep those: SSH refuses a private key that others can read.
On Windows 10 and 11, the same command works in PowerShell. OpenSSH Client is included with current Windows, and the keys go in C:\Users\you\.ssh\ (from Microsoft’s docs; we didn’t test on Windows).
3. Add it to ssh-agent
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_ed25519
ssh-add -l
ssh-add -l should list a line ending in (ED25519). On a Mac, ssh-add --apple-use-keychain ~/.ssh/id_ed25519 saves the passphrase in your keychain. We tested the agent on Linux only.
4. Get your public key and copy it
Print it and copy the whole line, which starts with ssh-ed25519 and ends with your comment:
cat ~/.ssh/id_ed25519.pub
Or send it straight to the clipboard (we tested cat only):
| System | Command |
|---|---|
| macOS | pbcopy < ~/.ssh/id_ed25519.pub |
| Windows PowerShell | Get-Content $env:USERPROFILE\.ssh\id_ed25519.pub | Set-Clipboard |
| Linux with xclip | xclip -selection clipboard < ~/.ssh/id_ed25519.pub |
Then paste it where it’s needed:
- GitHub: Settings, SSH and GPG keys, New SSH key, paste. GitLab and Bitbucket have the same option under your profile.
- A server you can already log in to:
ssh-copy-id user@server, which appends it to~/.ssh/authorized_keyson the server.
Windows with PuTTY
PuTTY uses its own key format (.ppk). Open PuTTYgen, choose EdDSA (Ed25519), click Generate and move the mouse until it finishes. Save the private key, and copy the text from the box labelled “Public key for pasting into OpenSSH authorized_keys file”; that’s the line GitHub or a server expects. If you use PowerShell’s ssh instead of PuTTY, use ssh-keygen as above. We didn’t test PuTTYgen.
5. Test it
ssh -T git@github.com
GitHub replies with a greeting that includes your username (per GitHub’s docs; our test used a local server). For a server, ssh user@server should log you in without asking for the account password. If you get Permission denied (publickey), see our fix for that error.
FAQ
Should I use ed25519 or RSA?
ed25519 for anything current: the keys are short and fast. Use RSA with -b 4096 only for an older server or device that doesn't accept ed25519.
Should I set a passphrase?
Yes, for a key on your own laptop. With ssh-agent (and the macOS keychain) you type it once per session, not on every connection. Keys used by unattended scripts are usually left without one and kept tightly scoped instead.
How do I generate an SSH key for another user?
Run ssh-keygen as that user, for example sudo -u alice ssh-keygen -t ed25519, so the files land in their ~/.ssh with the right owner. If you generate it yourself and hand it over, they should replace it with their own as soon as they can.
Can I create an SSH key online?
Don't. Whoever runs the website could keep a copy of your private key. ssh-keygen is already installed on macOS, Linux and Windows 10 and 11, and it runs offline in a second.
Which file do I share?
Only the one ending in .pub. The file without an extension is your private key and never leaves your machine.
