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:
|
|
We’ll then use GitHub CLI to log in to your GitHub account:
|
|
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
.
Then, select HTTPS
as 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.
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.
Open the provided link to create a new token on GitHub.
Click on the “Generate new token” dropdown menu in the top-right corner of the page, then select “Generate 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.
Return to your terminal, paste the token, and press enter. Congratulations, you’re now logged in!
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:
|
|
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.
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.
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.
This should complete the configuration of your SSH key. If you encounter any issues, I recommend checking these pages:
- If you have trouble creating the SSH key: Generating a new SSH key and adding it to the ssh-agent from the GitHub documentation.
- If you have trouble configuring it on your GitHub account: Adding a new SSH key to your GitHub account from the GitHub documentation.
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.
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:
|
|
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!