How To Create SSH key

Nov 18,2024 109
- by admin

Today’s quick tutorial comes inspired by many wonderful followers who watch my videos and ask me: “Why don’t you have to type a username and password into your terminal when you git push ?”

So if you are tired of typing your username and password in just to make a simple push to GitHub, then today is your lucky day. The madness ends today. We are going to make the task effortless, by generating an SSH key and setting it up in GitHub.

Setting up SSH Authentication

This process of not needing to use a username and password is called SSH authentication. Basically you are going to generate a long random string of characters and symbols and instead of submitting a username and password, your computer will submit this long string. GitHub will compare the string with the one I told it belongs to my computer and if there is a match, then GitHub lets us through. Of course it is more complex than this behind the scenes, but this is good enough to understand it for now.

The basic process of getting this set up is as follows:

  1. Generate the SSH Key on your Computer
  2. Add the SSH Key to the SSH Agent
  3. Save the SSH public key to your GitHub Account.

Part 1: Generate an SSH Key

Before we do anything, we need an SSH key to work with. We generate the key through the terminal or git bash. The following commands work on Windows, Linux, and Mac exactly the same. The only difference between the three operating systems is that Linux and Mac will use the terminal and Windows will use their Git Bash program.

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

You will be asked for a location to store the key and you will want to just click enter to save it in the default location.

Next it will ask you for a passphrase. This is optional but recommended. Type in your password now and click enter. It will ask you to confirm and obviously just do it again.

Part 2: Add Your SSH Key to your SSH Agent

Next we need to let our computer’s SSH agent (the program that distributes and uses the SSH keys) to know which SSH key to use. You probably only have one SSH key on your computer, so this won’t be very hard, but you could theoretically have an infinite number of them. This is why we tell the SSH agent which one to use.

Of course first we need to enable the SSH agent:

eval "$(ssh-agent -s)"

Now you should have it enabled and you can now set it to use the key we just generated:

ssh-add ~/.ssh/id_rsa

Now our SSH Agent, knows which key to use, so it is time to tell GitHub all about our adventures.

Part 3: Add the SSH Key to GitHub

All that is really needed at this point is to copy the ssh key and paste it into GitHub. The problem is that copying the code isn’t the easiest thing in the world. We need to use the terminal again. The problem is that this will vary depending on which OS you use.

MacOS:

pbcopy < ~/.ssh/id_rsa.pub

Windows:

clip < ~/.ssh/id_rsa.pub

Linux:

You unfortunately don’t have a copy feature built into all distributions of Linux, so if you don’t have one already we will install xClip.

sudo apt-get install xclip

Now that you have it, you can copy it to your clipboard:

xclip -sel clip < ~/.ssh/id_rsa.pub

There are no comments yet.
Your message is required.

Related Posts