Krysti.Engineer Official Chat
You are viewing an inferior version of the site because your browser does not support WebP. Do upgrade to something like Chrome or Firefox. Loading websites like this causes them to require a fallback set of images and they are almost always lower quality and larger in size.

GitHub + SSH + Personal Access Token

This is a simple rundown to get using GitHub using authenticated SSH keys as well as storing your personal access token.
GitHub Logo
2022-07-22T:00:00:00+00:00 July 22 2022 00:00:00

ssh-keygen -t ed25519 -C "your_email@example.com" -f ~/.ssh/github


ssh-keygen -t rsa -b 4096 -C "your_email@example.com" -f ~/.ssh/github


chmod 700 ~/.ssh
chmod 600 ~/.ssh/*

First we need an SSH key to be generated. GitHub recommends using ed25519 alogorithm but also a recommended legacy setup. Pick one of these two commands to process this. Be sure to not overwrite any existing keys, use the -f switch in the example. We will be setting up 'keychain' to handle multiple keys without any headache of management (easiest method) later in these instructions. For the sake of instructions we are keeping our keys in the ~/.ssh directory and the github key will reside at ~/.ssh/github.
Login to Github and add this key by going to Settings -> SSH and GPG keys and click "New SSH Key". Add your PUBLIC key to the text box (~/.ssh/github.pub).

echo export GH_TOKEN="your_api_key_here" >> ~/.bashrc
export GH_TOKEN="your_api_key_here"

Now we need an API key to store into an environment variable. Go back to your Settings. Scroll down the page and find "Developer settings < >". Click "Personal access tokens" and then "Generate New Token". This information generated will be what goes into our GH_TOKEN environment variable.

echo "keychain \$HOME/.ssh/id_rsa \$HOME/.ssh/github" >> ~/.bashrc
echo "source \$HOME/.keychain/\$HOSTNAME-sh" >> ~/.bashrc
keychain $HOME/.ssh/id_rsa $HOME/.ssh/github
source $HOME/.keychain/$HOSTNAME-sh

Install the 'keychain' command. This should be available in any default Linux repository. We need to enable this to execute on login and execute it now so you can use it.

ssh -T git@github.com
Hi itskrystibitch! You've successfully authenticated, but GitHub does not provide shell access.

Now we can test if our key is working. You should receive similar output.

cd /path/to/yourgitrepo
echo "# test" >> README.md
git init
git add README.md
git commit -m "first commit"
git branch -M "main"
git remote add origin https://$GH_TOKEN@github.com/yourgituser/yourgitrepo.git
git push -u origin "main"

Now login to your GitHib account and add a new repository without any extras (default). You should see simialarly the same instructions at this point but we're adding the $GH_TOKEN we configured earlier so we don't get any prompts when we push this thing. Make sure your repository is created on GitHub BEFORE you push. The second "git remote add" command (below) is just an example of how to integrate the SSH authentication we enabled. Use either the https OR the ssh version. You don't need to do both of these commands. That's it! You should be jumping into your first GitHub repository now.

git remote add origin git@github.com:yourgituser/yourgitrepo.git

Remember this command is an either/or situation. You don't need both but these examples do serve to show SSH and the authentication token working.

echo "hello world" >> somefile.txt
git add somefile.txt
git commit -m "adding somefile"
git push -u origin "main"

.. and this is how you add more to your repository.

xbps-install -Rs github-cli # Void Linux install
apk add github-cli # Alpine Linux install
gh config set -h github.com git_protocol ssh
gh auth login # Have GH_TOKEN handy, select SSH
? What account do you want to log into? GitHub.com
? What is your preferred protocol for Git operations? SSH
? Upload your SSH public key to your GitHub account? /home/your_user/.ssh/github.pub
? Title for your SSH key: GitHub CLI
? How would you like to authenticate GitHub CLI? Paste an authentication token
Tip: you can generate a Personal Access Token here https://github.com/settings/tokens
The minimum required scopes are 'repo', 'read:org', 'admin:public_key'.
? Paste your authentication token: ***************
gh repo create test --public

TIP: You *CAN* create a repository from CLI if you have the GitHub CLI tools installed. Search your repository for github-cli and install. Bear in mind the 'gh' command (for reasons!) doesn't maintain any functionality for 'push' and 'commit'. However, it is useful for the exmaple purpose when you want a fluid setup directly in terminal. Do note, the permissions scope mentioned in the above example. Your access token needs "'repo', 'read:org', 'admin:public_key'" scopes enabled. I added a couple non-standard package manager examples.. figuring I probably don't need to cover apt-get? Right?

Contact Krysti

@itskrystibitch Twitter Photo
Twitter:
@KrystiOnline
Facebook:
@KrystiOnline
IRC:
Official Chat
Direct.Me:
@Krysti
Ko-fi:
@Krysti
Photos:
Virtual Krysti
E-Mail / Notify of errors:
coder [@] krysti.engineer
Please be patient contacting me, I don't really check much of social media or anything. If you use the IRC be sure to stick around because that's how IRC works, silly. :P