how to login in command line github 2 factor authentication

How to Login to GitHub Command Line with 2FA

By Joseph King
AD

Securing your GitHub account is paramount in today's digital age, and two-factor authentication (2FA) is a robust way to enhance your login security. While the GitHub web interface provides an intuitive way to manage 2FA, sometimes you might want to enable or manage it via the command line, especially if you're working with GitHub from the terminal. This guide will walk you through the process of setting up and managing GitHub's 2FA using the command line.

Before we dive in, ensure you have Git and SSH installed on your system. If not, you can install them using the following commands in your terminal:

Setting Up Git and SSH

For Git, use the following command:

AD
an image of a black screen with text on it that reads,'how do you use the command? '
an image of a black screen with text on it that reads,'how do you use the command? '
the login screen is open and shows that you can log in to your account
the login screen is open and shows that you can log in to your account
the login screen for google's homepage is shown in this screenshot
the login screen for google's homepage is shown in this screenshot

sudo apt install git (For Ubuntu/Debian)

Or, for MacOS users, you can use Homebrew:

How To Enable Automatic Login In Fedora 39 - OSTechNix
How To Enable Automatic Login In Fedora 39 - OSTechNix
Top 25 Eye-Catching Email Signatures Gmail Users Will Love
Top 25 Eye-Catching Email Signatures Gmail Users Will Love

brew install git

For SSH, use these commands:

AD

sudo apt install openssh-server (For Ubuntu/Debian)

brew install openssh (For MacOS users)

Generating SSH Key Pair

Once Git and SSH are installed, generate an SSH key pair using the following command:

AD

ssh-keygen -t ed25519 -C "your_email@example.com"

Replace "your_email@example.com" with your GitHub email address. This command generates a new SSH key pair with the specified email.

Adding SSH Key to GitHub

After generating the key pair, add the public key to your GitHub account:

AD

ssh-add ~/.ssh/id_ed25519

Then, copy the public key:

cat ~/.ssh/id_ed25519.pub

Finally, add the key to your GitHub account by following these steps on the GitHub website:

  1. Click on your profile icon, then Settings.
  2. In the left sidebar, click SSH and GPG keys.
  3. Under "New SSH key," paste the key copied from your terminal, give it a title, and click "Add key."

Enabling Two-Factor Authentication

Now that your SSH key is added, let's enable 2FA using the command line.

First, ensure you're logged in to your GitHub account using the SSH key:

ssh -T git@github.com

Issuing a Personal Access Token (PAT)

To manage 2FA via the command line, you'll need a PAT. Generate one using the following command:

curl -u "" -X POST -d "notes=token%20for%20two%20factor%20authentication" https://api.github.com/authorizations

Replace "" with your GitHub username. Keep this PAT handy for later use.

Enabling Two-Factor Authentication

Now, enable 2FA using the following command:

curl -u ":" -X POST -H "Accept: application/vnd.github.v3+json" -d '{"-enabled":true}' https://api.github.com/user/setup_two_factor_auth

Replace "" with your GitHub username and "" with the Personal Access Token issued earlier.

Congratulations! You've successfully enabled 2FA for your GitHub account using the command line. To manage 2FA settings in the future, use the curl commands provided in GitHub's API documentation, replacing the URL with the appropriate one, and your PAT and username where necessary.

spokes to your infrastructure.

AD