Configuring SSH Keys and Tokens

Tutorial 2 of 5

1. Introduction

1.1 Tutorial's Goal

This tutorial aims to provide a step-by-step guide on how to configure SSH keys and tokens in GitHub. You'll learn how to generate, use, and manage these vital security features, helping you to enhance your project security and simplify your workflow.

1.2 What You Will Learn

By the end of this tutorial, you’ll be able to:
- Understand the concepts of SSH keys and tokens.
- Generate SSH keys and tokens.
- Associate the SSH keys and tokens with your GitHub account.
- Use these keys and tokens for authentication purposes.

1.3 Prerequisites

Before starting this tutorial, you should have:
- A basic understanding of Git and GitHub.
- A GitHub account.
- Git installed on your local machine.

2. Step-by-Step Guide

2.1 SSH Keys

SSH (Secure Shell) keys are a secure method of authenticating with GitHub without having to type your username and password each time.

2.1.1 Generating SSH Key

Open your terminal and type the following command. Replace "your_email@example.com" with your GitHub email address.

ssh-keygen -t rsa -b 4096 -C "your_email@example.com"

This command will create a new SSH key, using the provided email as a label.

2.1.2 Adding SSH Key to the ssh-agent

Start the ssh-agent in the background with:

eval "$(ssh-agent -s)"

Now add your SSH private key to the ssh-agent with:

ssh-add ~/.ssh/id_rsa

2.1.3 Adding SSH Key to GitHub Account

  1. In the upper-right corner of any page, click your profile photo, then click Settings.
  2. In the user settings sidebar, click SSH and GPG keys.
  3. Click New SSH key or Add SSH key.
  4. Paste your key into the "Key" field.
  5. Click Add SSH key.

2.2 Tokens

Tokens (also called access tokens) are a way to grant limited access to your GitHub account without sharing your password. They can be used in place of a password for Git over HTTPS.

2.2.1 Generating a Token

  1. In the upper-right corner of any page, click your profile photo, then click Settings.
  2. In the left sidebar, click Developer settings.
  3. In the left sidebar, click Personal access tokens.
  4. Click Generate new token.
  5. Give your token a descriptive name.
  6. Select the scopes you wish to grant this token, such as repo or user.
  7. Click Generate token.

3. Code Examples

3.1 Cloning a Repository Using SSH

git clone git@github.com:username/repo.git

This command clones a repository over SSH. Replace "username" and "repo" with the repository's details. No password is required due to the SSH key.

3.2 Cloning a Repository Using a Token

git clone https://username:token@github.com/username/repo.git

This command clones a repository over HTTPS using a token. Replace "username", "token", and "repo" with the appropriate details.

4. Summary

In this tutorial, we learned about SSH keys and tokens in GitHub. We covered how to generate, use, and manage these security features. These keys and tokens can be used to authenticate with GitHub, enhancing security and simplifying workflows.

5. Practice Exercises

5.1 Exercise 1

Generate a new SSH key and add it to your GitHub account.

5.2 Exercise 2

Generate a new access token with "repo" and "user" permissions.

5.3 Exercise 3

Clone a repository using both SSH and a token.

Don't forget to delete your keys and tokens when you're done practicing for security reasons. Keep practicing these steps to become more familiar with the process.

6. Additional Resources