Featured image of post Configure SSH and Clone GitHub Repos on a Raspberry Pi

Configure SSH and Clone GitHub Repos on a Raspberry Pi

Learn how to configure SSH access on your Raspberry Pi and clone private GitHub repositories remotely via GitHub CLI. Step-by-step guide included

I’ve just received a new Raspberry Pi 🎉

With each new computer comes a delightful moment of configuration. Among these configurations is setting up access to my private GitHub repositories. In my case, I need this to check out a repository containing a welcome script. This script displays, upon SSH connection to the instance, the instance name, information like the number of users currently connected, uptime, IP address, and finally, a customized prompt and color scheme. If you’re interested in this project, I’ve made it publicly accessible here. I also need access to private repositories that contain default configuration templates for Portainer, Prometheus, Loki, Watchtower, and more.

In this short article, I’ll detail how I performed this configuration remotely via an SSH connection to my Raspberry Pi to clone my private GitHub repositories locally.


❯  Login to GitHub via gh

First, open an SSH connection to the instance you want to configure.

Once connected, install GitHub CLI (see the official website) by executing these commands:

1
2
sudo apt update
sudo apt install -y gh

We’ll then use GitHub CLI to log in to your GitHub account:

1
gh auth login

This command will prompt a series of questions. I’ll detail one method to successfully log in to your GitHub account. Other options work too, but this article aims to provide a working configuration rather than an exhaustive guide.

At the first prompt, select GitHub.com.

gh auth login prompt asking for what account to log into
gh auth login prompt asking for what account to log into

Then, select HTTPS as your preferred protocol for Git operations.

gh auth login prompt asking for your preferred protocol for Git operations
gh auth login prompt asking for your preferred protocol for Git operations

The next step asks, “How would you like to authenticate GitHub CLI?” Select Paste an authentication token.

Note. This option allows you to keep track of the different devices connected to your GitHub account. Depending on your configuration, connecting to GitHub via your web browser might be complex. During my tests, I often encountered situations where GitHub refused to continue the flow, even after completing the authentication process in the browser and entering the provided code.

gh auth login prompt asking for which authentication method to use
gh auth login prompt asking for which authentication method to use

It will then provide a link to create a token, a list of scopes to grant to this token, and finally, a prompt waiting for you to paste the token after its creation.

gh auth login prompt displaying the link to create the token, scopes to set and asking for the token
gh auth login prompt displaying the link to create the token, scopes to set and asking for the token

Open the provided link to create a new token on GitHub.

the page containing the tokens on the user's GitHub account
the page containing the tokens on the user's GitHub account

Click on the “Generate new token” dropdown menu in the top-right corner of the page, then select “Generate new token (classic)”.

the creation form to generate a new token (classic)
the creation form to generate a new token (classic)

On the new page, give your token a name (typically describing on which instance this token will be used and for what purpose), set an expiration duration (select the shortest duration that meets your needs, or “No expiration” if you know you’ll need long-term access to your GitHub account). For security reasons, only select the required rights: repo, read:org, and workflow.

Then, click “Generate token” at the bottom of the page.

Back on the list of tokens associated with your account, copy the displayed token. Make sure to copy it now, as you won’t have access to this token again in the future.

back to the page containing the tokens, with the token newly created to copy
back to the page containing the tokens, with the token newly created to copy

Return to your terminal, paste the token, and press enter. Congratulations, you’re now logged in!

gh auth login successfully completed
gh auth login successfully completed

If you plan to use HTTPS to interact with your repositories, everything should now be set up, and you can skip the next section. However, if you prefer SSH, you’ll need to create and configure an SSH key.

I’ll detail how to create such an SSH key on your Raspberry Pi and add it to your GitHub account.


❯  Create and configure an SSH key

First, in your terminal, type:

1
ssh-keygen -t ed25519 -C "your_email@example.com"

Replace your_email@example.com with either the primary email address configured on your GitHub emails Settings page, or if you’ve checked “Keep my email addresses private” on this page, use the email address provided in the same section (typically ending with “@users.noreply.github.com”).

Then, follow the prompts to enter the file in which to save the key and to give and confirm a passphrase. You can leave these three steps blank to use the default file name (/home/your_username/.ssh/id_ed25519) and to avoid entering a passphrase when using your SSH key, for example, when cloning GitHub repositories via SSH.

It will then display that your identification has been saved in the specified file (either the default value or the value you entered), the public key has been saved in the same path as the authentication key but with a .pub suffix, and will show a key fingerprint and a randomart image. We don’t need these. Instead, open the public key file and copy its content.

the ssh key successfully created
the ssh key successfully created

Next, go to your GitHub account. Either click on your avatar in the top-right corner, click on “Settings”, and click on “SSH and GPG keys”, or simply follow this link.

the SSH and GPG keys settings page on GitHub
the SSH and GPG keys settings page on GitHub

Then click on “New SSH Key”.

On the new screen, give your key a name (typically including the hostname where the key will be used), and paste the key you copied from the public key file into the “Key” text field. Click “Add SSH key” to finalize your configuration.

the SSH and GPG keys settings page on GitHub
the SSH and GPG keys settings page on GitHub

This should complete the configuration of your SSH key. If you encounter any issues, I recommend checking these pages:

Finally, let’s test the success of your configuration in the last section.


❯  Test that everything works

To perform our test, let’s first create a private repository (if you already have one, you can skip this step and replace the links below with those of your repository). From the GitHub homepage, click on the blue “New” button on the left of your screen. This opens the page for creating a new repository. Fill in “Repository name*” with a suitable name for your test (like “test-RPI-connection”), and select the “Private” radio button. Click “Create repository” to complete its creation.

the form of creation of a new repository
the form of creation of a new repository

You’ll then land on the home page of your repository. In the blue callout “Quick setup — if you’ve done this kind of thing before”, there’s a link. Select either HTTPS or SSH, depending on your preference. Copy it, and in your terminal, type and execute:

1
git clone $link

Replace $link with the link you copied.

If your configuration is successful, you should see a message saying Cloning into 'test-RPI-connection'..., and a warning that your repository is empty. This is expected since we created a repository without any default data. Type ls to see that a folder with your repository’s name has appeared.

Congratulations, this validates the correctness of the configuration. And also concludes the target of this article!