Connecting to GitHub with SSH
Github official documentation - Connecting to GitHub with SSH:https://docs.github.com/en/authentication/connecting-to-github-with-ssh
What is Github SSH Connection
When managing Git projects, login authentication is required to perform Clone, Commit, Pull, Push, etc. There are two ways to login:
- HTTPS: Data is transmitted in an encrypted way. Login is verified through account password.
- SSH: Provides a secure channel over an insecure network. Login is verified through SSH key.
The main differences between these two methods are:
- HTTPS does not require configuration, but it requires frequent input of account password in various scenarios.
- SSH configuration of keys is slightly more cumbersome, but subsequent use of the key allows login without an account password.
- SSH is more suited for executing git operations in scripts, as git login is an interactive command, and handling git login in scripts can be troublesome.
Generating SSH Key
Checking Existing SSH Key
On your local machine, you need to have an SSH Key to use ssh connection. To check for an existing SSH Key, open the terminal and enter the following command:
1 | ls -al ~/.ssh |
If any SSH Keys are listed in the ssh directory, you can choose to use an existing SSH Key. However, for security reasons, it's always considered safe to create different SSH Keys for different applications/connections, so that if one SSH Key is compromised, other connections remain secure.
Generating SSH Key
Open the terminal and enter the ssh-keygen command:
1
ssh-keygen -t rsa -C "<your email>"
Then the following two lines will be displayed:
1
2$ Generating public/private rsa key pair.
$ Enter file in which to save the key (/c/Users/16627/.ssh/id_rsa):This prompts you to enter a filename to save the newly generated SSH Key code. To avoid trouble, don't input anything and press Enter, then id_rsa and id_rsa.pub, two SSH Key files, will be generated by default.
At this point, the .ssh folder has been created, and you will be prompted:
1
$ Created directory ‘/c/Users/16627/.ssh’.
Next, you need to set a password:
1
$ Enter passphrase (empty for no passphrase):
If you set a password, you'll need to enter this password when you use ssh to transfer files. To avoid trouble, it's recommended not to set a password and just press Enter.
Enter the password again.
1
$ Enter same passphrase again:
If you didn't set a password in the previous step, you can just press Enter here. At this point, your SSH Key is set up, and you will receive this code prompt:
1
2
3$ Your identification has been saved in ~/.ssh/id_rsa
$ Your public key has been saved in ~/.ssh/id_rsa.pub
# It will also specifically show you your SSH KeyWhen you see the above code, it means your SSH key has been successfully created. You can use
ls -al ~/.ssh
again to view your Key.Then, you will be prompted "Enter file in which to save the SSH Key." You can press Enter to use the default location, or specify your own file location. To avoid future conflicts, it's recommended to use the default directory and press Enter.
Adding to the SSH-agent
Now that we have the SSH Key, we can add it to the ssh-agent. SSH-agent is a SSH Key manager for SSH. Adding the SSH Key to ssh-agent
can prevent you from repeatedly entering the passphrase. To add the SSH Key to ssh-agent
, first start ssh-agent
in the background.
1 | eval "$(ssh-agent -s)" |
Now that our ssh-agent is started, let's add our SSH Key pair to ssh.
1 | ssh-add ~/.ssh/id_rsa |
Note: If you used a different directory/filename when creating the key, replace ~/.ssh/id_ed25519 with the key location
Adding SSH Key to GitHub
Open the GitHub webpage and click on "Profile avatar - Settings" in the top right corner.
Select "SSH and GPG keys"
Click on "New SSH Key"
- Title: Give it any name
- Key Type: Default is fine
Retrieve SSH Key
- Open the
~/.ssh
path - Open the
id_rsa.pub
file - Copy its content
- Paste it into the Key section of the form
- Open the
Click "Add SSH Key" to create it successfully
Testing if the SSH Key is Effective
Enter the following command in the terminal or git Bash:
1 | ssh -T git@github.com |
Note that it's git@github.com, not your email.
Then you will be prompted:
1 | $ The authenticity of host ‘github.com (13.229.188.59)’ can’t be established. |
Type yes, then press Enter:
Next, you will be prompted to enter a password. If you did not set a password when setting up SSH, you will see:
1 | $ Warning: Permanently added ‘github.com,192.30.255.112’ (RSA) to the list of known hosts. |
After the warning, if you see the following message, then you have successfully set up your SSH Key.
1 | $ Hi “<username>”! You’ve successfully authenticated, but GitHub does not provide shell access. |