When working with GitHub, cloning repositories over SSH is faster and more secure than using HTTPS. If you’re setting it up for the first time, it can feel like a maze of commands and errors. This guide walks you through generating an SSH key, adding it to GitHub, and configuring your machine to authenticate seamlessly.
1. Check for Existing SSH Keys
First, check if you already have SSH keys on your system:
ls -al ~/.ssh
If you see files like id_ed25519.pub
or id_rsa.pub
, you already have keys. Otherwise, continue to the next step.
2. Generate a New SSH Key
Generate a new Ed25519 key pair (recommended):
ssh-keygen -t ed25519 -C "your_email@example.com"
- Replace
your_email@example.com
with the email tied to your GitHub account. - When prompted for a file name, press Enter to use the default (
~/.ssh/id_ed25519
), or specify your own (e.g.,mygithubkey
).
If you chose a custom name, move the files into ~/.ssh
:
mv ~/path/to/mygithubkey ~/.ssh/
mv ~/path/to/mygithubkey.pub ~/.ssh/
Set the correct permissions:
chmod 600 ~/.ssh/mygithubkey
chmod 644 ~/.ssh/mygithubkey.pub
3. Start the SSH Agent and Add the Key
Start the SSH agent:
eval "$(ssh-agent -s)"
Add your private key:
ssh-add ~/.ssh/mygithubkey
Verify it was added:
ssh-add -l
4. Add the Public Key to GitHub
Display your public key:
cat ~/.ssh/mygithubkey.pub
It will look something like this:
ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAEXAMPLEKEYSTRING user@example.com
Copy the entire line, then go to:
GitHub → Settings → SSH and GPG keys → New SSH key.
- Give it a title (e.g., Linux Mint Laptop).
- Paste the key.
- Save.
5. Test the Connection
Run:
ssh -T git@github.com
If everything is set up correctly, you’ll see:
Hi username! You've successfully authenticated, but GitHub does not provide shell access.
6. Clone Repositories with SSH
Now you can clone repositories using the SSH URL:
git clone git@github.com:your-username/your-repo.git
7. Optional: Configure SSH for Multiple Keys
If you use multiple keys, configure SSH to always use the right one for GitHub. Edit your config:
nano ~/.ssh/config
Add:
Host github.com
HostName github.com
User git
IdentityFile ~/.ssh/mygithubkey
Save and exit. Now GitHub will always use that key automatically.
Top comments (0)