Hello, previously we got to know about Introduction to Git & GitHub for Beginners & How to Set Up Git?.
Today we will learn about how to connect to GitHub using the Secure Shell Protocol (SSH), which provides a secure channel over an unsecured network. With the help of SSH keys, we can connect to GitHub without providing our personal access (username and password) at each visit.
What is SSH Key?
An SSH key is an access credential for the SSH (Secure Shell) network protocol. SSH is used for remote file transfer, network management, and remote operating system access. SSH keys always come in pairs, and every pair is made up of a private key and a public key.
- Public key – Everyone can see it, no need to protect it. (for encryption function)
- Private key – Stays in computer, must be protected. (for decryption function)
Check if you have any existing SSH Key on your local computer
Open Git Bash
Enter
ls -al ~/.ssh
to see if existing SSH keys are present. Here you can see that I already have some SSH Keys.
Generate a new SSH Key
1) If there is no key then generate a new SSH key
$ ssh-keygen -t ed25519 -C "your_email@example.com"
and this will generate a new SSH key:
Generating public/private ed25519 key pair.
2) When you're prompted to "Enter a file in which to save the key," press Enter. This accepts the default file location.
Enter a file in which to save the key (/c/Users/you/.ssh/id_ed25519): [Press enter]
3) At the prompt, type a secure passphrase, press enter or you can give your passphrase.
Enter passphrase (empty for no passphrase): [Type a passphrase]
Enter same passphrase again: [Type passphrase again]
4) Then you will get something like this:
Your identification has been saved in (/c/Users/you/.ssh/id_ed25519)
Your public key has been saved in (/c/Users/you/.ssh/id_ed25519)
Add your SSH Key to your ssh-agent
- Start the ssh-agent in the background.
eval "$(ssh-agent -s)"
- You will get your unique Agent pid.
Agent pid 59566
Add your private SSH Key to your ssh-agent
$ ssh-add ~/.ssh/id_ed25519
- You will get something like:
Identity added: /c/Users/you/.ssh/id_ed25519 (your_email@example.com)
Add your SSH Key to your GitHub Account
- Copy the SSH public key
$ clip < ~/.ssh/id_ed25519.pub
to your clipboard.
- From your GitHub Account go to Settings.
- In the user settings sidebar, click SSH and GPG keys.
- Click the New SSH Key or Add the SSH Key.
- In the Title field, add a descriptive label for the new key.
- Paste your key into the Key field.
- Click Add SSH Key.
- If prompted, confirm your GitHub password.
Test your Connection
1) Open Git Bash
2) Enter the following:
$ ssh -T git@github.com
3) You may see a warning like this:
The authenticity of host 'github.com (IP ADDRESS)' can't be established.
RSA key fingerprint is SHA256:nThbg6kXUpJWGl7E1IGOCspRomTxdCARLviKw6E5SY8.
Are you sure you want to continue connecting (yes/no)?
4) Verify the message by typing yes:
Hi username! You've successfully authenticated, but GitHub does not provide shell access.
Thanks
Thanks for reading this post. Stay tuned for more.
You can find me here also.
Top comments (2)
Another recommended tip is as follows:
Thanks for sharing.