The SSH key-based authentication is more highly secured than the password and access token. It also allows automating git operation in scripts or background processes.
Follow the below steps for adding a new ssh key in Github account.
- Check Available SSH Keys
- Generate a New SSH Key
- Add SSH Key to Github Account
- Test Setup
1. Check Available SSH Keys
Log in to your Unix/Linux system and open a terminal. Check ~/.ssh/ directory for existing ssh key files in your system. If the system already have key pair files, you can use them to configure with Github.
In my case, the files already exists.
ls -la ~/.ssh
Output
-rw------- 1 root root 568 Mar 7 09:36 authorized_keys -rw------- 1 root root 2602 Apr 1 02:53 id_rsa -rw-r--r-- 1 root root 571 Apr 1 02:53 id_rsa.pub
2. Generate a New SSH Key
If no SSH key pair is available, use the ssh-keygen command-line tool to generate a new key. Open a terminal and type:
ssh-keygen
Hit enter for any input prompted by command:
Output
Generating public/private rsa key pair. Enter file in which to save the key (/home/rahul/.ssh/id_rsa):[Hit Enter] Enter passphrase (empty for no passphrase):[Hit Enter] Enter same passphrase again:[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 image is: ---[RSA 3072]---- | o.| | ...o=| | . .o..=| | o . .o.| | . S o. =.=| | . .o .E*| | . ..* | | =*o*.B.o| | .ooB%= ...| ----[SHA256]-----
Thats it. Now, you will see two key files created under ~/.ssh directory. One is the private key (id_rsa) and other is the public key (id_rsa.pub).
Keep the private key file secured and don’t share with anyone.
3. Add SSH Key to Github Account
As you have created a SSH key pair on your system. Copy the content of public key (file with .pub extension) and follow the below steps to add ssh key in Github account.
- At the top-right corner, click on profile icon and click Settings.
- In left sidebar, click SSH and GPG keys.
- Click New SSH key
click “New SSH Key” button - Input a title and paste the public key content in Key section.
Adding SSH Key - Click Add SSH Key
- Enter your password to authenticate.
- All done
4. Test Setup
To verify settings, just clone any repository accessible to your account with Git url. For example:
git clone [email protected]:username/reponame.git
This will authenticate the request using SSH key pair. On successful authentication the repository will cloned in your system.
Conclusion
In this tutorial, you have learn to configure SSH key to your Github account.