Adding an SSH Key
Getting a key to a new office takes a few steps: the key is cut, you put it on your key ring, reception records it against your name, and on the first morning you try it in the door. After that, you just walk in.
Setting up SSH for a hosting platform is the same short list, done once per computer: create a key pair, load it into the SSH agent, add the public key to your account, and test it. After that, git clone, git pull and git push over SSH no longer ask for a password or token. (If your key has a passphrase, you type it when the agent loads the key, and most systems can remember it for you.) The previous lesson, "What Is SSH", explained why this works. This lesson does it.
Step 1: Check for an Existing Key
You may already have a key from an earlier setup. Look in the .ssh folder in your home folder:
ls ~/.ssh 2>/dev/null || echo "No SSH keys yet"No SSH keys yetIf you see id_ed25519 and id_ed25519.pub, you already have a key and can skip to step 3. Here, there is none.
Step 2: Create a Key Pair
On your own computer, run ssh-keygen with the key type and your email as a label, and answer its questions:
ssh-keygen -t ed25519 -C "navika@example.com"ssh-keygen asks | Answer |
|---|---|
Enter file in which to save the key (.../.ssh/id_ed25519): | Press Enter to accept the default |
Enter passphrase (empty for no passphrase): | Type a passphrase (nothing appears as you type) |
Enter same passphrase again: | Type it again |
It then prints where it saved the two files, the key's fingerprint and a small picture (the "randomart"), which are different for every key.
So that this page can run it without questions, the checked example below passes the same answers as options: -f for the file, -N "" for an empty passphrase and -q for quiet. With -f, ssh-keygen doesn't create the .ssh folder by itself, so mkdir -p creates it first, and chmod 700 makes it private, as SSH expects. On your computer, use the interactive command above, with a passphrase:
mkdir -p ~/.ssh
chmod 700 ~/.ssh
ssh-keygen -q -t ed25519 -C "navika@example.com" -f ~/.ssh/id_ed25519 -N ""
ls ~/.ssh
cut -d " " -f 1,3 ~/.ssh/id_ed25519.pubid_ed25519
id_ed25519.pub
ssh-ed25519 navika@example.comid_ed25519 is the private key, and id_ed25519.pub the public key. The public key is one line: the type, the long key itself (left out here) and your email label.
Step 3: Load the Key into the SSH Agent
The SSH agent keeps your key ready in memory, so you type the passphrase once per login, not at every push:
# PowerShell, run as Administrator once, to start the agent automatically
Get-Service ssh-agent | Set-Service -StartupType Automatic
Start-Service ssh-agent
# Then, in a normal terminal:
ssh-add $env:USERPROFILE\.ssh\id_ed25519# Store the passphrase in the macOS Keychain
ssh-add --apple-use-keychain ~/.ssh/id_ed25519# Start an agent for this terminal if none is running, then add the key
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_ed25519On macOS, also add these lines to ~/.ssh/config, so the key is loaded from the Keychain automatically after a restart:
Host *
AddKeysToAgent yes
UseKeychain yes
IdentityFile ~/.ssh/id_ed25519ssh-add -l lists the keys the agent holds.
Step 4: Add the Public Key to Your Account
Copy the public key:
Get-Content $env:USERPROFILE\.ssh\id_ed25519.pub | Set-Clipboardpbcopy < ~/.ssh/id_ed25519.pubcat ~/.ssh/id_ed25519.pub
# then select the whole line and copy itThen paste it into your account settings on the platform:
1. Profile picture (top right) > Settings.
2. SSH and GPG keys > New SSH key.
3. Title: a name for this computer, such as "Navika's laptop".
4. Key type: Authentication Key.
5. Key: paste the line that starts with ssh-ed25519.
6. Click Add SSH key.1. Avatar (top) > Edit profile.
2. SSH Keys > Add new key.
3. Key: paste the line that starts with ssh-ed25519.
4. Title: a name for this computer, such as "Navika's laptop".
5. Usage type: Authentication & Signing. Choose an expiry date if you like.
6. Click Add key.1. Settings (gear icon) > Personal Bitbucket settings.
2. SSH keys > Add key.
3. Label: a name for this computer, such as "Navika's laptop".
4. Key: paste the line that starts with ssh-ed25519.
5. Click Add key.Menu names move around now and then. If a step looks different, search the platform's help for "add SSH key".
Step 5: Test the Connection
ssh -T connects as the user git without opening a shell, and prints a greeting:
ssh -T git@github.comThe first time, SSH asks you to confirm the server's fingerprint, as explained in "What Is SSH". Check it against the platform's documentation, then type yes. On success, each platform answers with its own greeting, which includes your username:
| Command | Success message |
|---|---|
ssh -T git@github.com | Hi <username>! You've successfully authenticated, but GitHub does not provide shell access. |
ssh -T git@gitlab.com | Welcome to GitLab, @<username>! |
ssh -T git@bitbucket.org | authenticated via ssh key. followed by You can use git to connect to Bitbucket. Shell access is disabled |
"Does not provide shell access" is expected: the platforms only accept Git commands over SSH. The important part is that your username appears.
Switch a Repository to SSH
A repository cloned with HTTPS keeps its HTTPS URL. Switch origin to the SSH URL from the platform's page:
git init --quiet fee-tracker
cd fee-tracker
git remote add origin https://github.com/navika-anamaneni/fee-tracker.git
git remote set-url origin git@github.com:navika-anamaneni/fee-tracker.git
git remote -vorigin git@github.com:navika-anamaneni/fee-tracker.git (fetch)
origin git@github.com:navika-anamaneni/fee-tracker.git (push)The next git push uses SSH and the key.
Two Accounts on One Computer
If you have a personal and a work account on the same platform, each needs its own key, because one key can belong to only one account. SSH's own config file can give each key a short host name. Here, Navika creates a second key for work and an entry for it:
ssh-keygen -q -t ed25519 -C "navika@work.example.com" -f ~/.ssh/id_ed25519_work -N ""
cat >> ~/.ssh/config <<'EOF'
Host github-work
HostName github.com
User git
IdentityFile ~/.ssh/id_ed25519_work
IdentitiesOnly yes
EOF
ls ~/.sshconfig
id_ed25519
id_ed25519.pub
id_ed25519_work
id_ed25519_work.pubgithub-work is a made-up name that SSH replaces with the real host and the work key. IdentitiesOnly yes makes SSH offer only that key. >> adds the entry to the end of the file, so any settings you already have are kept. Repositories of the work account then use URLs like git@github-work:school-org/fee-tracker.git. ssh -G shows what SSH will use for a host, without connecting. In your own terminal, ssh -G github-work is enough. On this page, -F names the config file explicitly and -T says no terminal is needed, which gives the same lines:
ssh -T -F ~/.ssh/config -G github-work | grep -E "^(hostname|user|identityfile) "user git
hostname github.com
identityfile ~/.ssh/id_ed25519_workSyntax
| Command | It is used to |
|---|---|
ssh-keygen -t ed25519 -C "<email>" | Create a key pair |
ssh-add <private-key-file> | Load a key into the SSH agent |
ssh-add -l | List the keys the agent holds |
ssh -T git@github.com | Test SSH sign-in (also gitlab.com, bitbucket.org) |
ssh -vT git@github.com | Test with details, to find problems |
git remote set-url origin <ssh-url> | Switch a repository to SSH |
ssh -G <host> | Show the settings SSH will use for a host |
Common Mistakes
Permission denied (publickey). The platform didn't accept any key your computer offered. Check that the public key is added to your account, that the agent has the key (ssh-add -l), and run ssh -vT git@github.com to see which key was offered.
Pasting the private key. Platforms usually refuse it, but if it was ever pasted somewhere, create a new pair and delete the old key.
Adding the same key to two accounts. The platform refuses it ("key is already in use"). Use one key per account, with a ~/.ssh/config entry as above.
Wrong file permissions. SSH refuses a private key that others can read: UNPROTECTED PRIVATE KEY FILE. On macOS and Linux, run chmod 600 ~/.ssh/id_ed25519.
Testing with ssh git@github.com and expecting a shell. There is no shell. The greeting is the success message.
Under the Hood
When Git uses an SSH URL, it runs the ssh program (or whatever core.sshCommand names) to connect as git@github.com, and asks it to run the platform's Git service for the repository path. ssh reads ~/.ssh/config to decide the real host, user and key, asks the agent for a signature, and checks the server's host key against ~/.ssh/known_hosts. Git itself never sees your key. It only talks through the connection that ssh opened, which is why fixing SSH problems is always about ssh, not Git.
Interview Corner
How do you set up SSH authentication for GitHub?
Generate a key pair with ssh-keygen -t ed25519 -C "<email>", add the private key to the SSH agent with ssh-add, paste the public key (~/.ssh/id_ed25519.pub) into GitHub's SSH keys settings, test with ssh -T git@github.com, and use SSH URLs for remotes.
How do you fix "Permission denied (publickey)"?
Make sure the public key is added to your account, the private key is loaded (ssh-add -l), and the remote uses an SSH URL for the right host. ssh -vT git@<host> shows which keys are offered and why they fail.
How do you use two GitHub accounts on one computer?
Create a separate key for each account, add each public key to its account, and define host aliases in ~/.ssh/config with HostName github.com, the matching IdentityFile and IdentitiesOnly yes. Use the alias in the remote URLs.
What does the SSH agent do?
It keeps decrypted private keys in memory for your login session, so you type a key's passphrase once instead of on every connection.
Practice
Challenge: a GitLab work account. Add a second entry to ~/.ssh/config for a work account on GitLab: host name gitlab-work, real host gitlab.com, user git, and the same work key. Then ask SSH which host, user and key it would use for gitlab-work.
What does Git print? Before you open the solution, write down the three lines you expect from ssh -G.
Show solution
cat >> ~/.ssh/config <<'EOF'
Host gitlab-work
HostName gitlab.com
User git
IdentityFile ~/.ssh/id_ed25519_work
IdentitiesOnly yes
EOF
ssh -T -F ~/.ssh/config -G gitlab-work | grep -E "^(hostname|user|identityfile) "user git
hostname gitlab.com
identityfile ~/.ssh/id_ed25519_workSSH resolves gitlab-work to gitlab.com, signs in as git, and offers only the work key. A remote URL such as git@gitlab-work:school-org/fee-tracker.git would use exactly these settings.
Checkpoint. On your own computer, create an Ed25519 key with a passphrase, load it into the agent, add it to your GitHub, GitLab or Bitbucket account, and test it with ssh -T. Switch one of your repositories to the SSH URL and push.
Key Takeaways
Quick reference
| Command | It is used to |
|---|---|
ssh-keygen -t ed25519 -C "<email>" | Create a key pair |
ssh-add ~/.ssh/id_ed25519 | Load the key into the agent |
pbcopy < ~/.ssh/id_ed25519.pub | Copy the public key (macOS) |
ssh -T git@github.com | Test the setup |
ssh -vT git@github.com | Debug a failing connection |
git remote set-url origin git@github.com:<user>/<repo>.git | Use SSH for a repository |
ssh -G <host> | See the resolved SSH settings for a host |
Was this tutorial helpful?