Preface
I am used to using the company's GitLab, but I haven't used GitHub to maintain my own projects for a while. Recently, I need to create a new personal project for use on the Alibaba Cloud server, and I even forgot how to use SSH to connect to GitHub.
Therefore, this article records the method of connecting to GitHub via SSH for your reference later.
The whole process is relatively simple, but you need to pay attention to the following:
- When generating a key, you do not need to set a protection password;
- Of course, you can also set a password and enter it every time you use the key;
- You can also add the SSH key to ssh-agent after setting the password, and let the SSH agent manage the SSH key and remember your password, so you don’t have to enter the password every time you use the key.
Which method to use can be chosen based on your actual needs.
Detailed reference documentation: https://docs.github.com/zh/authentication/connecting-to-github-with-ssh/about-ssh
Generate new SSH keys
- Connect to your own cloud server
- Generate a key. The command is as follows (please replace your login email). The first step is to enter the path and name where the key will be stored after generation (you can enter it as needed, or you can just press Enter to use the default name and save it to the default path), and then you are prompted to enter the key protection password (if you press Enter directly, no password is set)
ssh-keygen -t ed25519 -C "[email protected]"
Add SSH key to GitHub account
- First copy the SSH public key to the clipboard (after generating the key in the previous step, you will be prompted "Your public key has been saved in xxx", just copy the contents of xxx)
cat /root/.ssh/id_ed25519.pub
-
Log in to GitHub and enter
Settings
page
-
Enter
access > SSH and GPG keys
, clickNew SSH Key
Add new SSK configuration
-
Give the newly added configuration a name that is easy to identify, then paste the public key content into the Key input box, and finally click
Add SSH key
Save settings
Test SSH connection
- Return to the cloud server and enter the following command to verify the SSH connection. If an SSH key protection password is set, enter the password after pressing Enter. If you see
You've successfully authenticated
, the connection is successful.
ssh -T [email protected]
Add SSH keys to ssh-agent
- Start the ssh agent on the cloud server
eval "$(ssh-agent -s)"
- Add the SSH private key to ssh-agent (note that the SSH key path below is replaced with your own path)
ssh-add /root/.ssh/id_ed25519