Network security

How to Add an SSH Key to GitHub in 6 Steps (Windows, Mac, Linux)

An SSH key lets you push to GitHub without typing your password every time. Here is the full setup, step by step, on every platform.

Cysoro Security TeamPublished · 10 min read
$ ssh-keygen -t ed25519Generating public/privateed25519 key pair.$ ssh -T git@github.comHi you! You havesuccessfully authenticated. ssh-ed25519 AUTHENTICATED NETWORK SECURITY Add SSH key to GitHub Six steps, five minutes.
Quick answer

To add an SSH key to GitHub, generate a key with ssh-keygen -t ed25519 -C your_email@example.com, add the private key to the ssh-agent with ssh-add, copy the public key with cat ~/.ssh/id_ed25519.pub, then go to GitHub Settings, SSH and GPG keys, click New SSH key, paste the public key and save it. Test the connection with ssh -T git@github.com. The whole process takes about five minutes.

Key takeaways

  • Generate an Ed25519 key with ssh-keygen, add it to the ssh-agent, and paste only the public key into GitHub.
  • Always set a passphrase on your key, and never share or upload the private key file.
  • Test the connection with ssh -T git@github.com before you clone or push anything.

If you push code to GitHub with HTTPS, you type your username and a token every time, or you fight with credential helpers. An SSH key removes that friction: you authenticate once per session with a passphrase, and Git just works. Setting it up takes about five minutes.

What is an SSH key, and why does GitHub use one?

An SSH key is a pair of cryptographic keys: a private key that stays on your computer and a public key that you give to GitHub. When you connect, GitHub checks that your private key matches the public key on your account, and lets you in without a password.

GitHub's documentation explains it plainly: when you connect via SSH, you authenticate using a private key file on your local machine. The private key never leaves your computer. Only the public key goes to GitHub, and a public key is useless on its own, so sharing it is safe.

SSH authentication is more secure than a password for two reasons. First, the private key is a long random secret that cannot be guessed or phished the way a password can. Second, you can protect it with a passphrase, so even if someone steals the file, they still cannot use it.

What do you need before you start?

You need three things: a GitHub account, a terminal, and Git installed. On macOS and Linux, the terminal and OpenSSH come built in. On Windows, use Git Bash, which comes with Git for Windows, or the Windows Terminal with PowerShell.

Decide on the email address to attach to the key. GitHub's examples use your GitHub email address as a label on the key, which makes it easy to identify later. You can use any email, but your GitHub account email keeps things simple.

Also decide whether you want a passphrase. GitHub recommends one, and this guide assumes you will set one. The ssh-agent will remember it, so you only type it once per session. If you need a strong passphrase, our free password generator can create one.

How do you check for an existing SSH key?

Before generating a new key, check whether you already have one. Many developers set one up years ago and forget about it, and generating a second key over the first can cause confusion.

ls -al ~/.ssh

If you see files named id_ed25519 and id_ed25519.pub, or id_rsa and id_rsa.pub, you already have a key pair. The file ending in .pub is the public key. The file without an extension is the private key and must never be shared.

If you already have a key and it works, skip ahead to adding it to GitHub. If the key is old, uses DSA, or you no longer trust the computer it was made on, generate a fresh one. GitHub dropped support for DSA keys on March 15, 2022, so you cannot add new DSA keys to your account.

How do you generate a new SSH key?

Open your terminal and run the following command, replacing the email address with your GitHub email address. This creates a new Ed25519 key pair.

ssh-keygen -t ed25519 -C "your_email@example.com"

When prompted for a file location, press Enter to accept the default, which is ~/.ssh/id_ed25519. If you already have a key at that location and do not want to overwrite it, type a different file name instead, such as ~/.ssh/id_ed25519_work.

Next, type a secure passphrase when prompted, then type it again to confirm. GitHub's documentation notes that you can add a passphrase to further secure the key, and that the ssh-agent can remember it so you do not have to enter it every time.

Your terminal will show output like this:

Generating public/private ed25519 key pair.
Enter file in which to save the key (/home/you/.ssh/id_ed25519):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in id_ed25519
Your public key has been saved in id_ed25519.pub

If you are on a legacy system that does not support Ed25519, use RSA with 4096 bits instead, as GitHub's key generation guide explains:

ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
Your SSH key pair: what goes whereYour computerPrivate key~/.ssh/id_ed25519NEVER share thisProtect it with a passphraseGitHubPublic key~/.ssh/id_ed25519.pubSafe to uploadUseless without the private keypublic key onlyIf your private key leaks, delete it from GitHub and generate a new pair.
The private key stays on your computer; only the public key goes to GitHub. Illustration by Cysoro.

How do you add your SSH key to the ssh-agent?

The ssh-agent is a small program that holds your private key in memory and remembers your passphrase, so you only type it once per session instead of on every Git operation.

On macOS and Linux, start the agent and add your key with these commands:

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

On macOS, you can store the passphrase in your keychain so it survives restarts. Add these lines to your ~/.ssh/config file, then add the key with ssh-add --apple-use-keychain ~/.ssh/id_ed25519. If you chose not to set a passphrase, omit the keychain option.

On Windows, start the agent from an elevated PowerShell window, then add the key from a normal terminal:

Get-Service -Name ssh-agent | Set-Service -StartupType Manual
Start-Service ssh-agent
ssh-add c:/Users/YOU/.ssh/id_ed25519

One Windows quirk to know: Git for Windows bundles its own SSH client, which may not talk to the Windows ssh-agent service. If Git keeps asking for your passphrase, force it to use the system SSH binary with git config --global core.sshCommand "C:/Windows/System32/OpenSSH/ssh.exe".

How do you add your SSH key to GitHub?

First, copy the public key to your clipboard. The command depends on your platform. On macOS, run pbcopy < ~/.ssh/id_ed25519.pub. On Windows, run cat ~/.ssh/id_ed25519.pub | clip. On Linux, run cat ~/.ssh/id_ed25519.pub and copy the output yourself. Do not add any newlines or extra spaces.

The key starts with ssh-ed25519 and ends with your email address. Make sure you copied the .pub file, not the private key. Then follow these steps on GitHub, as described in GitHub's guide to adding a new SSH key:

  1. Click your profile picture in the upper-right corner of any page, then click Settings.
  2. In the left sidebar, under Access, click SSH and GPG keys.
  3. Click New SSH key or Add SSH key.
  4. In the Title field, add a descriptive label, such as "Personal laptop".
  5. Select the key type: Authentication Key for pushing and pulling code.
  6. Paste your public key into the Key field.
  7. Click Add SSH key and confirm with your GitHub password if prompted.

If you prefer the command line, the GitHub CLI can do the same thing once you have run gh auth login:

gh ssh-key add ~/.ssh/id_ed25519.pub --type authentication --title "personal laptop"

You can also use the same key for commit signing, but GitHub requires you to upload it a second time as a signing key. Signing proves that your commits really came from you, and it is worth setting up for repositories you maintain.

How do you test your SSH connection?

Run this command in your terminal to check that GitHub accepts your key. It connects as the git user and asks GitHub to confirm the authentication.

ssh -T git@github.com

A successful connection replies with a message like Hi username! You've successfully authenticated, but GitHub does not provide shell access. That last part is normal: GitHub never gives you a shell, it only lets Git talk to it.

If you see Permission denied (publickey), your key is not set up correctly. Work through the fixes in the next section. If you see a warning about the host's authenticity the first time, check that the fingerprint matches GitHub's published ED25519 fingerprint, then type yes to continue.

Once the test passes, switch any existing local repositories from HTTPS to SSH with:

git remote set-url origin git@github.com:USERNAME/REPOSITORY.git

SSH vs HTTPS vs GitHub CLI: which should you use?

GitHub supports three ways to authenticate, and the right one depends on how you work. SSH is the best default for developers who push code regularly.

MethodHow it authenticatesBest forDrawbacks
SSH keyPrivate key on your computerDaily development, pushing and pullingNeeds setup on each machine
HTTPS with tokenPersonal access tokenQuick clones, scripts, CI systemsToken must be stored and rotated
GitHub CLI (gh)Browser-based loginCreating repos, PRs and issues from the terminalNeeds the CLI installed

HTTPS with a personal access token is simpler to set up, but tokens expire and must be stored safely. SSH keys do not expire, work offline from the browser, and are harder to phish. The GitHub CLI is a useful companion to either method, since it handles repository and pull request tasks from the terminal.

What if it does not work? Common errors and fixes

Most SSH problems come from a small set of causes. Check them in this order.

Permission denied (publickey)

This is the most common error. It usually means GitHub does not have your public key, or your computer is offering the wrong key. First, check that the key is listed under Settings, SSH and GPG keys on GitHub. Then check that the agent has your key with ssh-add -l, and add it again if the list is empty.

Git keeps asking for your passphrase

Your key is not in the ssh-agent, or the agent is not running. Start the agent and add the key again. On macOS, make sure your ~/.ssh/config file includes the keychain settings so the passphrase survives restarts.

GitHub accepts the connection but as the wrong account

If you have several GitHub accounts, your computer may offer the wrong key first. Create separate keys for each account and add a Host entry for each one in ~/.ssh/config, pointing at the right key file. See the multiple-keys section below.

Bad permissions on the key file

SSH refuses to use a private key that other users can read. Fix the permissions with chmod 700 ~/.ssh and chmod 600 ~/.ssh/id_ed25519, then try again.

Host key verification failed

This usually means the known_hosts entry for github.com is stale, which can happen after GitHub rotates its host keys. Remove the old entry with ssh-keygen -R github.com and reconnect to fetch the new one.

How do you manage multiple SSH keys?

Use one key pair per computer, and one per GitHub account if you have more than one. Give each key a descriptive title on GitHub, such as "Work laptop" or "Home desktop", so you can revoke a single key later without breaking the others.

For multiple GitHub accounts on one computer, add a section to ~/.ssh/config for each account:

Host github.com
  HostName github.com
  User git
  IdentityFile ~/.ssh/id_ed25519

Host github-work
  HostName github.com
  User git
  IdentityFile ~/.ssh/id_ed25519_work

Then clone work repositories with git@github-work:ORG/REPO.git instead of git@github.com. Git will use the work key for those repositories and your personal key for the rest.

Audit your keys once a year. Open Settings, SSH and GPG keys on GitHub and delete any key you no longer recognize or no longer use. A key on a laptop you sold or lost is a key someone else may hold.

If your device was ever infected with malware, treat the key as compromised: our keylogger guide explains how a logger can capture a typed passphrase, which is another reason to use the ssh-agent instead of typing it every time.

Add an SSH key to GitHub in six steps1. Check for an existing key: ls -al ~/.ssh2. Generate: ssh-keygen -t ed25519 -C "your_email@example.com"3. Add to the agent: eval "$(ssh-agent -s)" then ssh-add4. Copy the public key: cat ~/.ssh/id_ed25519.pub5. Paste it in GitHub: Settings → SSH and GPG keys → New SSH key6. Test it: ssh -T git@github.com
The six steps to add an SSH key to GitHub. Illustration by Cysoro.

How do you keep your SSH key safe?

Your private key is the credential that protects your GitHub account, so treat it like a password. Set a strong passphrase, never email the private key to yourself, never paste it into a chat or a ticket, and never commit it to a repository.

If you think a private key has leaked, act immediately: delete the public key from GitHub, generate a new pair, and add the new public key. Also review your account for unfamiliar activity, since anyone with the old key could have pushed code as you.

For teams, the same habits scale. Our small business cybersecurity checklist covers SSH keys alongside MFA, backups and updates.

Our spear phishing guide explains why phishing-resistant sign-in matters even with strong keys in place.

The bottom line

Adding an SSH key to GitHub takes five minutes and saves you from typing credentials on every push. Generate an Ed25519 key, protect it with a passphrase, add it to the ssh-agent, paste the public key into GitHub, and test the connection with ssh -T git@github.com.

Use a separate key on each computer, give each one a clear title, and audit your keys once a year. If you need a strong passphrase for the key, generate one with the free password generator.

Frequently asked questions

How do I add an SSH key to GitHub?

Generate a key with ssh-keygen -t ed25519 -C your email, add it to the ssh-agent with ssh-add, copy the public key from the .pub file, then go to GitHub Settings, SSH and GPG keys, click New SSH key, paste the key and click Add SSH key.

Where do I paste my SSH key on GitHub?

Click your profile picture in the upper-right corner of any GitHub page, then Settings, then SSH and GPG keys in the left sidebar. Click New SSH key, add a descriptive title, choose Authentication Key as the type, and paste your public key into the Key field.

Should I use Ed25519 or RSA for GitHub?

Use Ed25519. GitHub recommends it as the default because it is modern, fast and secure with a short key. Only use RSA with 4096 bits if you are on a legacy system that does not support Ed25519.

Do I need a passphrase on my SSH key?

GitHub recommends adding one. A passphrase encrypts the private key on your disk, so anyone who steals the file still cannot use it without the passphrase. The ssh-agent remembers the passphrase so you only type it once per session.

How do I test my SSH connection to GitHub?

Run ssh -T git@github.com in your terminal. A successful connection replies with a message like Hi username! You've successfully authenticated, but GitHub does not provide shell access. If you see Permission denied, your key is not set up correctly.

Can I use the same SSH key on multiple computers?

You can, but you should not. Generate a separate key pair on each computer and add each public key to GitHub with a descriptive title. That way you can revoke one computer's key without breaking the others.

What is the difference between an authentication key and a signing key on GitHub?

An authentication key lets you clone, push and pull over SSH. A signing key verifies that your commits and tags really came from you. You can use the same key for both, but GitHub requires you to upload it twice, once as each type.

Sources

  1. GitHub Docs — Adding a new SSH key to your GitHub account
  2. GitHub Docs — Generating a new SSH key and adding it to the ssh-agent
  3. GitHub Docs — Connecting to GitHub with SSH
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 GitHub's official documentation for SSH authentication.

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