SSH key-based authentication provides a more secure alternative to password and access token. It also allows you to automate git operation using scripts or background processes.
Follow these steps to add a new ssh-key in your Github account.
- Check out the SSH Keys Available
- Create a new SSH key
- Add SSH Key to Github account
- Test Setup
1. Check out the SSH Keys Available
Log in to Unix/Linux to open a terminal. Check ~/.ssh/directory to search for ssh keys in your system. You can use key pair files from an existing system to configure Github.
In my case, I have the files.
-rw ——- 2 root roots 568 Mar 7, 09:36 authorized_keys.-rw ——- 2 root roots 2602 Apr 1 02.53 id_rsa.pub
2. Create a new SSH key
If there is no SSH key pair available, use the ssh-keygenCommand-line tool to generate new keys Type:
For any command-related input, hit enter
Generating public/private rsa key pair. Enter file in which to save the key (/home/rahul/.ssh/id_rsa):[Hit Enter]Enter your passphrase (empty if you don’t have one):[Hit Enter]Enter the same passphrase once more:[Hit 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:WG+/Js/iM3kNfJ7Vf4iKHu/Jd2q3VffAnNAdyh/Rc7s [email protected]The key’s randomart picture is +—[RSA 3072]—-+ | o.| | …o=| | . .o..=| | o . +.o.| | . . =.=| | . .o .E*| | . ..* ++| | =*o*.B.o| | .ooB%=+…| +—-[SHA256]—–+
That’s all. Now you will see two key file created under the /.ssh folder. The private key (id_rsa), and the public key (id_rsa.pub) are the two key files.
Keep the private key file secured and don’t share with anyone.
3. Add SSH Key to Github account
You have now created an SSH pair on your system. Copy the contents of the public key (file with the.pub extension), and follow these steps to add ssh keys in Github account.
- Click on the profile icon in the upper-right corner and click Settings.
- In left sidebar, click GPG and SSH keys.
- Click New SSH key
- In the Key section, enter a title.
- Click Add SSH Key
- To authenticate, enter your password.
- All done
4. Test Setup
To verify settings, simply clone any repository that is accessible to your account using Git URL. Take this example:
git clone [email protected]:username/reponame.git
This will authenticate your request using the SSH key pair. The repository will be cloned into your system after successful authentication.