You have a new cloud server and a password prompt every time you connect. Or you have cloned a repository over HTTPS and Git keeps asking for a token. SSH keys solve both: you prove who you are with a key pair instead of a password, which is both more convenient and much harder to guess or phish.
This guide explains how SSH key authentication works, why Ed25519 is the usual choice today, and how to set up a key for a Linux server and for GitHub, step by step. It finishes with the permission and agent problems that cause most "Permission denied (publickey)" errors.
How SSH keys work
An SSH key is a pair of mathematically linked files:
- The private key (for example
~/.ssh/id_ed25519) stays on your computer. It is the secret. Anyone who has it can log in as you. - The public key (
~/.ssh/id_ed25519.pub) is a single line of text you can share freely. You place it on every server or service you want to access.
When you connect, the server sends a challenge that only the holder of the private key can sign. Your SSH client signs it, the server checks the signature against the public key in its authorized_keys file, and you are in. The private key never leaves your machine.
Ed25519 or RSA?
| Ed25519 | RSA | |
|---|---|---|
| Key size | Fixed, very short public key | 2048 to 4096 bits; 3072 or more recommended for new keys |
| Speed | Fast to generate and use | Slower, especially 4096-bit generation |
| Support | Current OpenSSH, GitHub, GitLab, Bitbucket | Practically universal, including very old systems |
| Recommendation | Default choice for new keys | Use when a legacy system doesn't accept Ed25519 |
Unless you are connecting to old equipment or software that rejects it, choose Ed25519.
Generate a key with ssh-keygen (recommended)
For keys you will use for real servers, generate them on the computer that will use them. The ssh-keygen command ships with macOS, Linux and current versions of Windows 10 and 11 (in PowerShell or Command Prompt).
ssh-keygen -t ed25519 -C "alice@laptop"
Press Enter to accept the default location, then type a passphrase when asked. The passphrase encrypts the private key file, so a stolen laptop or leaked backup doesn't hand over your server access. A few random words from a Passphrase Generator, stored in your password manager, work well. If a system requires RSA:
ssh-keygen -t rsa -b 4096 -C "alice@laptop"
To see a key's fingerprint, which is how servers and GitHub identify it:
ssh-keygen -lf ~/.ssh/id_ed25519.pub
Generate a key in the browser
The SSH Key Generator creates key pairs with your browser's Web Crypto API; nothing is uploaded. It is handy for test environments, workshops, containers you are about to throw away, or simply for seeing what the OpenSSH formats look like.
- Open the SSH Key Generator and choose a Key type: Ed25519 (recommended), RSA 4096-bit, RSA 3072-bit or RSA 2048-bit. If your browser doesn't support Ed25519 in Web Crypto, only the RSA options appear.
- Enter a Comment, usually
you@computeror an email address, so you can recognise the key later inauthorized_keysor on GitHub. - Click Generate key pair. RSA 4096 can take a few seconds.
- Note the SHA256 Fingerprint shown above the output.
- Download or copy the Public key (
id_ed25519.puborid_rsa.pub) and the Private key, OpenSSH format. A PEM PKCS#8 copy of the private key is also provided for libraries that don't read the OpenSSH format. - Save the private key as
~/.ssh/id_ed25519and, as the on-page warning says, add a passphrase locally, since the browser-generated file is not encrypted:
chmod 600 ~/.ssh/id_ed25519 && ssh-keygen -p -f ~/.ssh/id_ed25519
For production servers and long-lived access, ssh-keygen on the machine that will use the key is still the better habit: the private key is never displayed on a web page, copied through the clipboard or saved in a downloads folder.
Install the public key on a Linux server
The server needs your public key in the ~/.ssh/authorized_keys file of the account you log in as.
If you can still log in with a password, ssh-copy-id does it for you on macOS and Linux:
ssh-copy-id -i ~/.ssh/id_ed25519.pub deploy@203.0.113.10
Otherwise, append it manually from an existing session:
mkdir -p ~/.ssh && chmod 700 ~/.ssh
echo "ssh-ed25519 AAAA... alice@laptop" >> ~/.ssh/authorized_keys
chmod 600 ~/.ssh/authorized_keys
Many cloud providers also let you paste a public key when creating a server. Then connect:
ssh -i ~/.ssh/id_ed25519 deploy@203.0.113.10
Once key login works for every account that needs it, consider disabling password logins in the SSH server configuration. Keep a second session open while you test the change so you can't lock yourself out.
Add the key to GitHub
- Copy the contents of your
.pubfile (the whole single line). - In GitHub, open your account settings, find SSH and GPG keys, and add a new SSH key. Give it a title that names the computer.
- Test the connection:
ssh -T git@github.com
A greeting with your username means it works. Switch existing repositories to SSH with git remote set-url origin git@github.com:owner/repo.git. GitLab and Bitbucket work the same way from their own settings pages.
Use ssh-agent so you type the passphrase once
A passphrase-protected key asks for the passphrase each time it is used. An SSH agent keeps the decrypted key in memory for your session:
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_ed25519
macOS can store the passphrase in the Keychain, and Windows has an OpenSSH Authentication Agent service you can enable. With the agent running you get the convenience of no prompts without leaving an unencrypted key on disk.
Fixing "Permission denied (publickey)"
Wrong permissions
OpenSSH refuses keys when files are too open. On the server, ~/.ssh should be 700 and authorized_keys 600, owned by the user. On your computer, the private key should be 600.
The wrong key is offered
If you have several keys, tell SSH which one to use with -i, or set it per host in ~/.ssh/config:
Host prod
HostName 203.0.113.10
User deploy
IdentityFile ~/.ssh/id_ed25519
Run ssh -v prod to see which keys are offered and why they are rejected.
The public key was pasted with line breaks
Each key in authorized_keys must be one line. Copying from some editors or emails wraps it, which silently breaks it.
Keep private keys private
Never commit a private key to a repository, paste it into chat or store it in application code. Use one key per device so a lost laptop means removing one line from each server rather than replacing keys everywhere. For services that need RSA key files in PEM or JWK format, such as signing tokens, see the RSA Key Generator, and for random API tokens use the Secret Key Generator.
Frequently asked questions
Is Ed25519 more secure than RSA?
Both are considered secure at recommended sizes. Ed25519 is preferred for new keys because it is fast, compact and avoids some implementation pitfalls, while RSA remains useful for compatibility with older systems.
Should my SSH key have a passphrase?
Yes, for keys on laptops and desktops. The passphrase encrypts the private key file, and an SSH agent means you only type it once per session.
Can I use the same key on several servers?
Yes. The public key can be installed on as many servers and services as you like. Using a separate key per device is more useful than a separate key per server.
What is the comment at the end of a public key?
It is a free-text label, usually user@host or an email, that helps you identify the key. It has no effect on security or authentication.
Is it safe to generate SSH keys in a browser?
A tool that runs locally, like ours, never uploads the key, and it is fine for testing. For long-lived production access, generating the key with ssh-keygen on the machine that will use it is the safer practice.