DEV Community

ClintWithK
ClintWithK

Posted on

Setting Up a Secure Connection to Git Using SSH Keys on Ubuntu (Beginner’s Guide)

In your tech journey, you will definitely come across SSH multiple times, so it’s in your best interest to understand “the what” and “the why.”

SSH (Secure Shell) is a secure protocol used to facilitate communication between computers and servers through the terminal. It is commonly preferred because it is secure, fast, and hard to compromise without the correct credentials.

SSH is widely used for:

  • Secure file transfers
  • System administration
  • Remote server access
  • Authenticating Git operations (push and pull requests)

In this guide, we’ll focus on using SSH keys to securely connect your Ubuntu machine to GitHub, allowing you to push and pull code without repeatedly entering your username and password.


Prerequisites

Before you begin, ensure you have the following:

Open your terminal using Ctrl + Alt + T.


Step 1: Confirm SSH Installation

Run the command below:

ssh -V
Enter fullscreen mode Exit fullscreen mode

If SSH is installed, you should see output similar to:

OpenSSH_9.6p1 Ubuntu-3ubuntu13.14, OpenSSL 3.0.13 30 Jan 2024
Enter fullscreen mode Exit fullscreen mode

If SSH Is Not Installed

Install it using:

sudo apt update
sudo apt install openssh-server
Enter fullscreen mode Exit fullscreen mode

Check if SSH is running:

sudo systemctl status ssh
Enter fullscreen mode Exit fullscreen mode

Enable SSH to start on boot (recommended):

sudo systemctl enable ssh
Enter fullscreen mode Exit fullscreen mode

Step 2: Check for Existing SSH Keys

Run:

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

If keys already exist, you should see something similar to:

.  ..  authorized_keys  id_ed25519  id_ed25519.pub  known_hosts  known_hosts.old
Enter fullscreen mode Exit fullscreen mode

id_ed25519 → Private key (keep this secret ❗)

id_ed25519.pub → Public key (shared with GitHub)

Step 3: Generate an SSH Key (If You Don’t Have One)

If no keys are found, generate one:

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

When prompted:

Press Enter to accept the default location

Optionally add a passphrase for extra security

Step 4: Start the SSH Agent and Add Your Key

Start the SSH agent:

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

Add your SSH private key:

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

Confirm the key is added:

ssh-add -l
Enter fullscreen mode Exit fullscreen mode

Step 5: Add the SSH Key to Your GitHub Account

Copy Your Public Key

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

Copy the entire output.

Add the Key on GitHub

-Log in to GitHub

-Click your profile picture → Settings

-Navigate to SSH and GPG keys

-Click New SSH key

Add:

-Title: e.g. Ubuntu Laptop

-Key: Paste the copied public key

-Click Add SSH key

Step 6: Test the SSH Connection

Verify the setup:

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

If successful, you should see:

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

Congratulations! Your Ubuntu machine is now securely connected to GitHub using SSH.

Conclusion

-SSH is more secure than HTTPS authentication

-No need to enter passwords when pushing or pulling code

-This setup is essential for professional Git workflows

What’s Next?

-In the next article, we’ll walk through:

-Initializing a Git repository

-Making commits

-Pushing and pulling changes from GitHub using SSH

                            Happy Coding!
Enter fullscreen mode Exit fullscreen mode

Top comments (0)