How to Login to GitHub Command Line with 2FA
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:
sudo apt install git (For Ubuntu/Debian)
Or, for MacOS users, you can use Homebrew:
brew install git
For SSH, use these commands:
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:
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:
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:
- Click on your profile icon, then Settings.
- In the left sidebar, click SSH and GPG keys.
- 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 "
Replace "
Enabling Two-Factor Authentication
Now, enable 2FA using the following command:
curl -u "
Replace "
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.