DEV Community

Vincent Tommi
Vincent Tommi

Posted on

How to Set Up GitHub SSH on Ubuntu: A Beginner-Friendly Guide

If you're using Git and GitHub on Ubuntu, setting up SSH authentication is a useful step. It allows you to connect your local machine to GitHub securely without entering your GitHub credentials every time you push or pull code.

While setting up SSH on my Ubuntu machine, I made a small mistake when generating the SSH key. Instead of accepting the default file location, I entered a custom name.

This article explains what I did, what went wrong, and how I fixed it.

What Is SSH?

SSH stands for Secure Shell. It is a secure way of connecting and authenticating between computers.

With GitHub, SSH allows your local machine to authenticate with your GitHub account.

Instead of using an HTTPS URL like:

https://github.com/username/repository.git
Enter fullscreen mode Exit fullscreen mode

you can use an SSH URL:

git@github.com:username/repository.git
Enter fullscreen mode Exit fullscreen mode

Once SSH is configured, commands such as:

git clone
git pull
git push
Enter fullscreen mode Exit fullscreen mode

can communicate with GitHub using your SSH key.


Step 1: Generate an SSH Key

On Ubuntu, open your terminal and run:

ssh-keygen -t ed25519 -C "your-email@example.com"
Enter fullscreen mode Exit fullscreen mode

For example:

ssh-keygen -t ed25519 -C "vincenttommikorir@gmail.com"
Enter fullscreen mode Exit fullscreen mode

You should see something similar to:

Generating public/private ed25519 key pair.
Enter file in which to save the key (/home/backend-developer/.ssh/id_ed25519):
Enter fullscreen mode Exit fullscreen mode

The important part

At this point, SSH is asking where you want to save the key.

The recommended approach for a first setup is simply to press Enter.

That will use the default location:

/home/backend-developer/.ssh/id_ed25519
Enter fullscreen mode Exit fullscreen mode

You will then have:

~/.ssh/id_ed25519
~/.ssh/id_ed25519.pub
Enter fullscreen mode Exit fullscreen mode

The first file is your private key.

The second file is your public key.


A Mistake I Made

When I generated my key, I entered:

Tommi
Enter fullscreen mode Exit fullscreen mode

when SSH asked:

Enter file in which to save the key (/home/backend-developer/.ssh/id_ed25519):
Enter fullscreen mode Exit fullscreen mode

SSH therefore created:

~/Tommi
~/Tommi.pub
Enter fullscreen mode Exit fullscreen mode

instead of:

~/.ssh/id_ed25519
~/.ssh/id_ed25519.pub
Enter fullscreen mode Exit fullscreen mode

The key itself was still valid. It was simply saved in a different location.

This is an important lesson: entering a filename at that prompt changes where SSH saves the key.


Step 2: Check Where Your Key Was Saved

If you accidentally entered a custom name, you can check your home directory:

ls -l ~/Tommi*
Enter fullscreen mode Exit fullscreen mode

You may see:

/home/backend-developer/Tommi
/home/backend-developer/Tommi.pub
Enter fullscreen mode Exit fullscreen mode

This confirms that the key exists.


Step 3: Move the Key to the .ssh Directory

The .ssh directory is the standard location for SSH keys.

If your files are currently:

~/Tommi
~/Tommi.pub
Enter fullscreen mode Exit fullscreen mode

you can move them using:

mv ~/Tommi ~/.ssh/id_ed25519
mv ~/Tommi.pub ~/.ssh/id_ed25519.pub
Enter fullscreen mode Exit fullscreen mode

Now your files should be:

~/.ssh/id_ed25519
~/.ssh/id_ed25519.pub
Enter fullscreen mode Exit fullscreen mode

You can check them with:

ls -la ~/.ssh
Enter fullscreen mode Exit fullscreen mode

Step 4: Set the Correct Permissions

SSH keys should have appropriate file permissions.

Run:

chmod 700 ~/.ssh
chmod 600 ~/.ssh/id_ed25519
chmod 644 ~/.ssh/id_ed25519.pub
Enter fullscreen mode Exit fullscreen mode

The private key should be readable only by your user.

That's why we use:

chmod 600 ~/.ssh/id_ed25519
Enter fullscreen mode Exit fullscreen mode

Your public key can have less restrictive permissions:

chmod 644 ~/.ssh/id_ed25519.pub
Enter fullscreen mode Exit fullscreen mode

Step 5: Start the SSH Agent

The SSH agent helps manage your private keys.

Start it with:

eval "$(ssh-agent -s)"
Enter fullscreen mode Exit fullscreen mode

You should get something similar to:

Agent pid 38799
Enter fullscreen mode Exit fullscreen mode

The process ID will be different on different systems.


Step 6: Add Your SSH Key to the Agent

Run:

ssh-add ~/.ssh/id_ed25519
Enter fullscreen mode Exit fullscreen mode

If you created a passphrase for your key, you may be asked to enter it.

You can verify that the key has been added:

ssh-add -l
Enter fullscreen mode Exit fullscreen mode

You should see your key fingerprint.

For example:

256 SHA256:AR9yA2U3it1bo5y6erYw/yg/XLsQr8CX/wKw+hw/XAQ
Enter fullscreen mode Exit fullscreen mode

Step 7: Get Your Public Key

Now display your public key:

cat ~/.ssh/id_ed25519.pub
Enter fullscreen mode Exit fullscreen mode

It should look similar to:

ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAA... your-email@example.com
Enter fullscreen mode Exit fullscreen mode

Copy the entire line.

Important Security Note

There are two files:

id_ed25519
id_ed25519.pub
Enter fullscreen mode Exit fullscreen mode

The difference is important.

Private key

id_ed25519
Enter fullscreen mode Exit fullscreen mode

Never share this file with anyone.

Public key

id_ed25519.pub
Enter fullscreen mode Exit fullscreen mode

This is the key you add to GitHub.

You can share your public key, but keep your private key secret.


Step 8: Add the Public Key to GitHub

Log in to GitHub and go to:

Settings → SSH and GPG keys → New SSH key

Give the key a recognizable title, for example:

Lenovo X390 Ubuntu
Enter fullscreen mode Exit fullscreen mode

Select:

Authentication Key
Enter fullscreen mode Exit fullscreen mode

Then paste the contents of:

cat ~/.ssh/id_ed25519.pub
Enter fullscreen mode Exit fullscreen mode

into the key field.

Save the key.


Step 9: Test the GitHub SSH Connection

Now test the connection:

ssh -T git@github.com
Enter fullscreen mode Exit fullscreen mode

The first time you connect, you may see:

Are you sure you want to continue connecting (yes/no/[fingerprint])?
Enter fullscreen mode Exit fullscreen mode

Type:

yes
Enter fullscreen mode Exit fullscreen mode

If everything is configured correctly, GitHub should respond with something similar to:

Hi username! You've successfully authenticated, but GitHub does not provide shell access.
Enter fullscreen mode Exit fullscreen mode

This means your SSH authentication is working.


Step 10: Use SSH When Cloning Repositories

Once SSH is configured, use the SSH repository URL when cloning.

Instead of:

git clone https://github.com/username/repository.git
Enter fullscreen mode Exit fullscreen mode

use:

git clone git@github.com:username/repository.git
Enter fullscreen mode Exit fullscreen mode

For example:

git clone git@github.com:vincenttommi/my-project.git
Enter fullscreen mode Exit fullscreen mode

What If I Already Have a Git Repository?

If you already cloned a project using HTTPS, check the remote URL:

git remote -v
Enter fullscreen mode Exit fullscreen mode

You might see:

origin  https://github.com/username/repository.git (fetch)
origin  https://github.com/username/repository.git (push)
Enter fullscreen mode Exit fullscreen mode

You can change it to SSH:

git remote set-url origin git@github.com:username/repository.git
Enter fullscreen mode Exit fullscreen mode

Then check again:

git remote -v
Enter fullscreen mode Exit fullscreen mode

You should now see:

origin  git@github.com:username/repository.git (fetch)
origin  git@github.com:username/repository.git (push)
Enter fullscreen mode Exit fullscreen mode

You can then use:

git pull
Enter fullscreen mode Exit fullscreen mode

and:

git push
Enter fullscreen mode Exit fullscreen mode

using SSH authentication.


Common Mistake: "No Such File or Directory"

One error I encountered was:

cat: /home/backend-developer/.ssh/id_ed25519.pub: No such file or directory
Enter fullscreen mode Exit fullscreen mode

This happened because I had saved the key as:

Tommi
Enter fullscreen mode Exit fullscreen mode

rather than accepting the default:

~/.ssh/id_ed25519
Enter fullscreen mode Exit fullscreen mode

I also tried:

cat ~/.ssh/id_Tommi.pub
Enter fullscreen mode Exit fullscreen mode

but that didn't work either because the file wasn't saved as id_Tommi.pub.

The actual file was:

~/Tommi.pub
Enter fullscreen mode Exit fullscreen mode

The lesson is to check where SSH actually created the files instead of assuming their names.


Useful Commands

Here are the main commands from this setup:

Generate a key

ssh-keygen -t ed25519 -C "your-email@example.com"
Enter fullscreen mode Exit fullscreen mode

Check SSH files

ls -la ~/.ssh
Enter fullscreen mode Exit fullscreen mode

Start SSH agent

eval "$(ssh-agent -s)"
Enter fullscreen mode Exit fullscreen mode

Add your key

ssh-add ~/.ssh/id_ed25519
Enter fullscreen mode Exit fullscreen mode

List keys loaded into the agent

ssh-add -l
Enter fullscreen mode Exit fullscreen mode

Display public key

cat ~/.ssh/id_ed25519.pub
Enter fullscreen mode Exit fullscreen mode

Test GitHub

ssh -T git@github.com
Enter fullscreen mode Exit fullscreen mode

Check Git remote

git remote -v
Enter fullscreen mode Exit fullscreen mode

Change an HTTPS remote to SSH

git remote set-url origin git@github.com:USERNAME/REPOSITORY.git
Enter fullscreen mode Exit fullscreen mode

Final Thoughts

Setting up SSH with GitHub is straightforward once you understand the difference between the private key, public key, and SSH agent.

The biggest lesson I learned from my setup was not to overlook this prompt:

Enter file in which to save the key (/home/username/.ssh/id_ed25519):
Enter fullscreen mode Exit fullscreen mode

If you're not sure what to enter, simply press Enter and use the default location.

If you accidentally save the key somewhere else, don't panic. The key is still usable—you can move it into the .ssh directory and configure it correctly.

Learning from small mistakes like this is part of working with Linux and development tools. Documenting these problems can also help another developer who runs into the same issue.

Happy coding!

Top comments (0)