Set Up SSH Keys for GitHub to Prevent Using the Username and Password Every Time

Set Up SSH Keys for GitHub to Prevent Using the Username and Password Every Time

What is GitHub SSH?

GitHub offers a convenient SSH key feature for accessing your repositories on the command line. To communicate across the public internet is to use SSH. Instead of using usernames and passwords every time you push to or pull from your repository, you can use your key. GitHub SSH key setup is a top priority for users of the popular Git service.

Fortunately, the GitHub and SSH key configuration process is relatively straightforward, especially on a Linux distribution such as Ubuntu.

Why use an SSH Key?

An SSH key is a convenient and secure way to access your account. It’s convenient because you don’t need to remember a long password. And you can make your actual password so brutally long and secure that no human or bot could guess it. The SSH key works like a real key that only you possess.

GitHub does not want to use the username and password every time for authentication. There are a few good reasons for doing this, such as the dangers of having passwords being reused from multiple websites.

So then what GitHub wants me to use instead? The answer is Token-Based Authentication. There are a few different options:

  • SSH Key
  • Personal Access Key
  • OAuth.

SSH is arguably the easiest to implement and today we're going to focus on this. The thing that is so much great about SSH keys is security.

GitHub does not need your username and password each time when using SSH keys. Essentially each key uniquely identifies your computer and after initial setup, GitHub can trust that this computer can do the operations.

Another great thing about SSH keys is that GitHub will remove inactive keys after a year.

Implementation

if you have not generated SSH then first generate it. Use the ssh-keygen command to create GitHub SSH key pairs:

github@ubuntu:~/.ssh$ ssh-keygen -t ed25519 -C ""

The ssh-keygen command will also ask you to protect your GitHub SSH key with an optional passphrase. It’s permissible to leave the passphrase blank, so click return when prompted.

You can write your own email and it will show that the email is associated with the key.

  • The -t flag specifies the type of SSH keys to create.
  • The -C flag allows for comments that get added as metadata at the end of the public key.

Now the key is generated, we need to make sure that the computer's SSH agent knows about it. You can think of the agent as your wallet to hold your different SSH keys.

Start up the SSH agent by typing:

eval "$(ssh-agent -s)"

Now we can add our private key to the SSH agent. Think of it as putting our ID into our wallet.

Let's find the config file in the .ssh directory. To find it, let's write:

~/.ssh/config

If this file does not exist then it will show me an error message. To create this, write:

touch ~/.ssh/config

Now the file is created. Let's edit it using Vim by writing a command in it vi ~/.ssh/config and write the following things in it:

Host *
  AddKeysToAgent yes
  IdentityFile ~/.ssh/id_ed25519

Save the file and run it by writing the following command in the terminal:

ssh-add ~/.ssh/id_ed25519

Now the next step is to tell GitHub about the SSH key that's being used by our computer.

  • Open your GitHub account, click on your avatar and choose settings.

Screenshot from 2022-10-21 08-29-41.png

  • Go ahead and look on the sidebar until you find SSH and GPG Keys. Click on it and it will lead you to another page.

Screenshot from 2022-10-21 08-32-49.png

  • On the next page, there will be two options for you. The first one is to create a New SSH key or create a New GPG key. Click on the SSH key option.

Screenshot from 2022-10-21 08-40-52.png

  • Now that you want to create the new SSH key, it will give you the option to enter a title and the key.

Screenshot from 2022-10-21 08-43-12.png

  • Now open the terminal and write the following command to open a file and that file will contain the key.

cat ~/.ssh/id_ed25519.pub

  • Select the whole data and copy it. The data will be like this:

ssh-ed25519 long-value email-address.com

  • Paste the data in the key section in GitHub and give the title to the SSH key.

  • Click on the add SSH Key button and you will be asked for your GitHub password.

Once the key is generated, open the terminal and write the following command in the terminal to test the SSH key.

ssh -T

If this is your first time then write yes to continue.

That's it. You have successfully authenticated but GitHub does not provide shell access. That's how we have expected. We just want that our SSH key works.

I hope it will help you. Please share it with others if you find it valuable.

Follow me on YouTube, Twitter, and LinkedIn.

Thank you!