2023-07-01
AI Summary by GPT-4
This article provides a detailed guide on setting up SSH keys for GitHub and remote servers, highlighting the security benefits of using SSH keys for secure remote communication. It describes two main methods: using ssh-keygen and the 1Password SSH agent, providing step-by-step instructions and configuration guidelines for both. Additionally, the article emphasizes the importance of protecting keys with a passphrase and recommends using 1Password for managing SSH keys and other sensitive information. References for further reading and documentation are also provided.

Setup SSH keys for GitHub and remote servers

Why using SSH keys?

Method 1: Using ssh-keygen

Generate the SSH keys on your local machine

On your local machine terminal, run:

ssh-keygen -t rsa -b 2048

You will be prompted to choose a passphrase for the keys as following. Please be sure to enter a passphrase. This is the password that will protect your keys. It can be a sentence with spaces between the words. Be sure to use a combination of lower and upper case letters, numbers and punctuation marks.

Generating public/private rsa key pair.
Enter file in which to save the key (/home/localuser/.ssh/id_rsa):
Enter passphrase (empty for no passphrase): 
Enter your passphrase here

Copy the public key to the remote server or GitHub

cat ~/.ssh/temp.pub >> .ssh/authorized_keys
chmod 600 .ssh/authorized_keys && rm .ssh/temp.pub

Activate the SSH agent on your local machine

You will now need to set up the SSH agent on your local computer. The SSH agent will remember your passphrase and forward your key on, securely, to your remote server.

Use ssh-add to add your passphrase to the SSH agent for the current login session. On your local machine: run ssh-add and you will be prompted to enter your passphrase you just set up.

Within the same login session, you can now ssh to your remote server without entering the passphrase or password.

Sign your commits with SSH keys on GitHub

Configure Git

We need to configure Git to let it know who we are by setting up our name and email address.

git config --global user.name "Your Name"
git config --global user.email "Your Email"

Then we need to eanble GPG signing for Git commits and the format of the signature will be SSH keys.

git config --global commit.gpgsign true
git config --global gpg.format ssh

Then we want to tell Git to use the key we just generated locally and uploaded to GitHub.

List your public SSH keys with ssh-add -L and copy the key you just generated.

Then set our signing key in Git to the key we just copied.

git config --global user.signingkey <paste your key here>

Your commits will now be signed with your SSH key. you can test it by making a commit and checking the signature with git log --show-signature.

Add SSH key to GitHub

To sign your pushed commits on GitHub so that your commits will be marked as verified, you need to add your public SSH key to GitHub.

Login to GitHub SSH keys, copy and Paste your public key to both Authentication Keys and Signing keys.

Method 2: using 1Password SSH agent

1Password is a powerful password manager with easy-to-use auto-fill functionalities. I use it to store any sensitive documents and login credentials, to generate random passwords for all my online accounts. (no affiliation or sponsorship, just a happy user)

For students, you can get 1-year 1Password subscription via the GitHub Student Developer Pack - GitHub Education.

As an alternative method, you can generate SSH keys using 1Password or import your SSH keys from your local file (e.g. ~/.ssh/id_rsa).

1Password will generate a public key automatically and store the public and private key-pair in your 1Password vault.

Then what you need to do is quite similar with a few more steps: As tested in 1Password 8 in July 2023, these instructions will be automatically prompted in the 1Password client after you generate or import SSH keys, setting up SSH keys with 1Password is quite straightforward.

Please refer to official document for a detailed step-by-step documentation.

References and further reading

[1] How do I setup SSH keys? - Minnesota Supercomputing Institute

[2] SSH & Git - 1Password docs

[3] Sign Git commits with SSH - 1Password docs

[4] How to Sign git Commits with an SSH key - Jack Wallen

< AI 总结 RSS Feeds Hello World >
Created: 2023-07-01 Updated: 2023-07-09