GitHub is a popular platform for hosting Git repositories, and it supports accessing repositories over SSH (Secure Shell). In order to access a GitHub repository over SSH, you will need to generate an SSH key pair and add the public key to your GitHub account.
Here are the steps for adding a new SSH key to your GitHub account:
Before you can add an SSH key to your GitHub account, you will need to do the following:
- Generate an SSH key pair. This consists of a private key and a public key. The private key is kept on your local machine, and the public key is uploaded to your GitHub account.
- Install Git on your local machine. You will need Git installed in order to use the ssh-keygen command to generate the SSH key pair.
The next step will help you to generate SSH key pair on your system.
Generating an SSH Key Pair
To generate an SSH key pair, follow these steps:
- Open a terminal window (Git Bash on Windows, or any terminal emulator on macOS or Linux).
- Run the following command to generate an SSH key pair:
ssh-keygen -t rsa -b 4096Output:[email protected]:~$ ssh-keygen -t rsa -b 4096 Generating public/private rsa key pair. Enter file in which to save the key (/home/rahul/.ssh/id_rsa):
[PRESS ENTER]Enter passphrase (empty for no passphrase): [PRESS ENTER]Enter same passphrase again: [PRESS ENTER]Your identification has been saved in /home/rahul/.ssh/id_rsa Your public key has been saved in /home/rahul/.ssh/id_rsa.pub The key fingerprint is: SHA256:r/ohd9s7kHb2SygRRpdvAiO6x7cEdO9yDAZkpQnvQXs [email protected] The key's randomart image is: +---[RSA 4096]----+ | ..+.o .. | | =+=+.. | | o*+E+ . | | ...+o.o o | | oSo.= o | | . o.B.*. | | ..o++B... | | o +.+... | | .oo . oo.. | +----[SHA256]-----+
This will generate a new SSH key pair using the RSA algorithm with a key length of 4096 bits.
- When prompted, enter a file name for the private key. This is the file where the private key will be stored. You can use the default file name (id_rsa) or choose a different name.
- When prompted, enter a passphrase for the private key. This is an optional security measure that adds an additional layer of protection to your private key. If you do not want to use a passphrase, just press Enter.
- The ssh-keygen command will generate the SSH key pair and store the private key in the file that you specified. The public key will be stored in a file with the same name, but with a .pub extension.
For example, if you specified the file name “id_rsa”, the private key will be stored in “id_rsa” and the public key will be stored in “id_rsa.pub”.
Adding the Public Key to Your GitHub Account
Now that you have generated an SSH key pair, you can add the public key to your GitHub account as follows:
- Go to your GitHub account settings.
- Select the “SSH and GPG keys” tab.
- Click the “New SSH key” button.
- In the “Title” field, enter a name for the key. This can be any name that helps you identify the key.
- In the “Key” field, paste the contents of the public key file (e.g., id_rsa.pub).
You can use the cat command to view the contents of the file, or you can open it in a text editor.
- Click the “Add SSH key” button to save the key.
That’s it! You should now see the new SSH key listed in your GitHub account settings. You can use this key to access GitHub repositories over SSH.
I hope this helps! Let me know if you have any questions or need further clarification on any of the steps.