📚 SSH Series: This is Part 2. If you haven't generated your SSH keys yet, start with Part 1: Understanding SSH and Generating SSH Keys.
Introduction
Welcome to Part 2! In Part 1, we covered the basics of SSH and how to generate your key pair. If you haven't generated your SSH keys yet, make sure to check out Part 1: Understanding SSH and Generating SSH Keys first.
Now let's actually use those keys. I'm going to walk you through adding your SSH key to GitHub (which I do on every new machine) and setting it up on an Ubuntu server.
I'll be honest—the first time I set this up, I spent way too long figuring out why my GitHub pushes kept asking for a password. Turns out I was still using the HTTPS URL. Once I switched to SSH, everything just worked. Let me save you that headache.
Prerequisites
Before we dive in, make sure you've got:
- An SSH key pair ready (if you haven't generated one yet, Part 1 has you covered)
- Your public key handy—you'll need to copy it a couple times
- A GitHub account (obviously)
- An Ubuntu server you can access (for the server part)
Adding Your SSH Key to GitHub
I switched to SSH on GitHub years ago because I got tired of typing my token every time I pushed a commit. Once you add your key, GitHub just gets out of your way—no passwords, no tokens, nothing. And it's actually more secure than the HTTPS route, which was a nice bonus.
If you haven't generated your SSH key pair yet, head back to Part 1 to learn how to create one using ssh-keygen.
Step 1: Copy Your Public Key
First things first—grab your public key. Fire up your terminal and run:
cat ~/.ssh/id_ed25519.pub
You'll see something that looks like a long string of random characters. That's your public key. It should start with ssh-ed25519 and end with your email. This is the same public key we generated in Part 1.
Pro tip: If you're on macOS, you can copy it directly to your clipboard:
cat ~/.ssh/id_ed25519.pub | pbcopy
On Linux, it's:
cat ~/.ssh/id_ed25519.pub | xclip -selection clipboard
This saves you from manually selecting and copying that long string. Trust me, it's worth it.
Step 2: Add SSH Key to GitHub
Now for the fun part—adding it to GitHub. Here's the step-by-step:
- Head over to GitHub and sign in
- Click your profile picture (top right)
- Hit Settings
- In the left sidebar, find SSH and GPG keys
- Click that green New SSH key button
- Give it a name that makes sense to you—I usually use something like "MacBook Pro 2024" or "Work Desktop" so I remember which machine it's for
- Paste your public key into the Key field
- Click Add SSH key
- GitHub might ask for your password one last time (security thing)
You can add multiple keys, which is super handy if you work from different machines. I've got keys for my laptop, desktop, and even my old ThinkPad that I keep around for testing.
Step 3: Test Your GitHub SSH Connection
Let's make sure everything works. Run this in your terminal:
ssh -T git@github.com
The first time you do this, you'll get a message asking if you trust GitHub's host key. It looks scary, but it's normal:
The authenticity of host 'github.com (140.82.112.3)' can't be established.
ED25519 key fingerprint is SHA256:+DiY3wvvV6TuJJhbpZisF/zLDA0zPMSvHdkr4UvCOqU.
Are you sure you want to continue connecting (yes/no)?
Just type yes and hit Enter. You should see:
Hi username! You've successfully authenticated, but GitHub does not provide shell access.
Perfect! That "shell access" message is totally normal—GitHub doesn't let you SSH in like a regular server. They just use it for Git operations, which is exactly what we want.
Using SSH with Git
Alright, now the good stuff. With your SSH key set up, you can use SSH URLs for all your Git operations. No more password prompts, no more expired tokens. Just smooth, seamless Git.
Cloning a Repository with SSH
When you clone a repo, use the SSH URL. It looks like this:
git clone git@github.com:username/repository.git
If you already cloned something using HTTPS (which I've done way too many times), you can switch it over:
git remote set-url origin git@github.com:username/repository.git
Double-check it worked:
git remote -v
You should see the SSH URL now instead of the HTTPS one.
Pushing and Pulling with SSH
Once you're using SSH, everything just works. No special commands needed:
# Pull latest changes
git pull origin main
# Push your changes
git push origin main
# Create and push a new branch
git checkout -b feature-branch
git push -u origin feature-branch
That's it. No passwords, no tokens, no interruptions. Your SSH key handles everything behind the scenes.
I've been using SSH with Git for years, and honestly, I can't remember the last time I had to deal with authentication issues. It just works. Plus, unlike those personal access tokens that expire every few months, SSH keys stick around until you remove them.
Adding SSH Key to Ubuntu Server
Now let's set up SSH key authentication on an Ubuntu server. I set this up on a fresh DigitalOcean droplet last week, and it took me maybe five minutes. Once it's done, you'll never have to type a password again when connecting to that server.
Remember, you'll need your public key (the one we generated in Part 1) for this step. If you need a refresher on how SSH keys work, check out the SSH key basics in Part 1.
Method 1: Using ssh-copy-id (Recommended)
Personally, I prefer ssh-copy-id because it saves me from messing with permissions manually. It's the lazy way, and I'm all for that:
ssh-copy-id username@server-ip-address
So if your server IP is 192.168.1.100 and you're using the ubuntu user:
ssh-copy-id ubuntu@192.168.1.100
You'll be asked for the password one last time. After that, ssh-copy-id does all the heavy lifting—it creates the ~/.ssh directory if needed, adds your key to authorized_keys, and sets the right permissions. Magic.
If you don't have ssh-copy-id installed (it's usually there on Linux, but macOS might need it):
- macOS:
brew install ssh-copy-id - Linux: Usually pre-installed, but if not:
sudo apt-get install openssh-client
Method 2: Manual Setup
If ssh-copy-id isn't available (or you're feeling adventurous), you can do it manually. I've done this a few times when ssh-copy-id wasn't available:
Step 1: Copy your public key to the server:
cat ~/.ssh/id_ed25519.pub | ssh username@server-ip-address "mkdir -p ~/.ssh && cat >> ~/.ssh/authorized_keys"
Step 2: Set the permissions. SSH is really picky about this—if the permissions are wrong, it won't work:
ssh username@server-ip-address "chmod 700 ~/.ssh && chmod 600 ~/.ssh/authorized_keys"
I've messed this up before and spent way too long wondering why my key wasn't working. The permissions thing is non-negotiable.
Test Your Connection
Now try connecting:
ssh username@server-ip-address
If everything worked, you should just... connect. No password prompt, no nothing. It's beautiful when it works.
Here's what's happening behind the scenes: The server checks if your public key is in ~/.ssh/authorized_keys. If it finds it, it sends you a challenge encrypted with that public key. Your SSH client uses your private key to answer the challenge, and if you get it right, you're in. All of this happens automatically, and it's way more secure than passwords because your private key never leaves your machine.
Wrapping Up
So there you have it. You've set up SSH keys for GitHub and your Ubuntu server. No more password prompts, no more expired tokens. Just smooth, secure authentication.
I use SSH keys everywhere now—GitHub, GitLab, my personal servers, work servers. Once you get used to it, you'll wonder how you ever lived without it. The setup takes a few minutes, but it saves you time and headaches down the road.
One last thing: keep that private key safe. Never share it, never commit it to a repo (I've seen it happen), and consider using a passphrase for extra security. If you haven't generated your keys yet, make sure to check out Part 1: Understanding SSH and Generating SSH Keys to get started.
Complete SSH Series
- Part 1: Understanding SSH and Generating SSH Keys - Learn what SSH is and how to generate your first key pair
- Part 2: Using SSH Keys with GitHub and Ubuntu Servers (you're here!) - Add keys to GitHub and servers
That's it! Go forth and SSH without passwords. 🚀