Using gh CLI to authenticate GitHub on Compute Canada and/or CBS Server

Somewhat recently (late 2021), GitHub stopped allowing users to authenticate local->remote pushes using just GitHub username and password. This change has and will affect any user looking to connect with remote repositories on systems such as Compute Canada and the CBS Server.

The most straightforward solution to meet the updated GitHub authentication requirements is to install and use gh CLI (https://cli.github.com/).

Installing gh CLI

For both Compute Canada and the CBS Server (and any other linux system), the steps for getting gh CLI installed are the same.

  1. Navigate to where you wish to install gh CLI. The package itself is only about 10MB, so it can be safely installed in your home directory on either system. You may wish to create a bin/ or software/ folder in which to install it, but this is entirely a matter of personal choice.

From a terminal prompt type: wget https://github.com/cli/cli/releases/download/v2.29.0/gh_2.29.0_linux_amd64.tar.gz

Note: The above version of gh CLI is the most recent as of May 2023. Please check https://github.com/cli/cli/releases/ to make sure you are downloading the most up-to-date version.

  1. Once the .tar.gz file finishes downloading (should only take a few minutes), run the following command from the terminal prompt to uncompress it:

tar -xf gh_2.29.0_linux_amd64.tar.gz

  1. Create a symbolic link to the main command for ease of use:

ln -s gh_2.29.0_linux_amd64/bin/gh gh

  1. Optional You can also add the path to gh to your .bashrc by adding the following line (updating the path to whatever it is for your account):

export PATH=$PATH:/full/path/to/gh_2.29.0_linux_amd65/bin/gh

Authenticating GitHub on the CBS Server (or linux systems that can see the outside world)

Before beginning the below steps, make sure that you are logged into GitHub via a web browser.

  1. From wherever you downloaded the gh CLI package, type the following in a terminal prompt:

gh auth login

  1. Follow the prompts in the terminal window.
  • What account do you want to log into? GitHub.com
  • What is your preferred protocol for Git operations? HTTPS
  • Authenticate Git with your GitHub credentials? Yes
  • How would you like to authenticate GitHub CLI? Login with a web browser
  • In a web browser, navigate to: https://github.com/login/device
  • Copy and paste the 8 character alphanumeric code printed in the terminal window. The format for this code should be A0A0-A0A0.
  • Follow the prompts in the web browser to accept the authentication. (Basically, click on the green buttons, accepting the default permissions to read/write and access local and remote repositories.)

You should be directed back to the terminal window and see the following message (or something similar with your GitHub user ID):

✓ Authentication complete.
- gh config set -h github.com git_protocol https
✓ Configured git protocol
✓ Logged in as gitUser

At this point you are now set to use git or VSCode or any other editor that has source/version control help that directly connects to GitHub.

Authenticating GitHub on Compute Canada (or linux systems that cannot see the outside world)

Since Compute Canada does not see the outside world, you will need to authenticate GitHub using a personal access token (PAT: https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/creating-a-personal-access-token)

  1. From wherever you downloaded the gh CLI package, type the following in a terminal prompt:

gh auth login

  1. Follow the prompts in the terminal window.
  • What account do you want to log into? GitHub.com
  • What is your preferred protocol for Git operations? HTTPS
  • Authenticate Git with your GitHub credentials? Yes
  • How would you like to authenticate GitHub CLI? Personal access token

Copy the URL printed in the terminal window to a web browser on your local machine. Or from GitHub.com, navigate to your account Settings->Developer settings->Personal access tokens->Generate new token. You most likely want to create a Tokens (classic). If you want to limit access to a specific repository (rather than your whole GitHub account), then you can set up a Fine-grained token.

In the Note text box, enter a brief description of what this token is for (e.g., graham, compute-canada, my server, etc.). This note will help you keep track of your tokens.

Then set an expiration date, or No expiration.

In the terminal prompt, there should be a list of the minimum permissions needed. From memory, I believe these are repo, write:packages, and admin:org. (Note: will update this list once I verify)

Once you click on the green Create token button, you will be taken to a web page with the personal access token you just created. Copy the full alphanumeric string (ghp_....) and enter it into the terminal window prompt.

Note: If you navigate away from the web page with the PAT before copying it, you will never be able to access the token again and will need to create a new one.

At this point, you should receive a message in the terminal prompt that Git and GitHub have now been authenticated.

1 Like