Configure SSH connection code repository locally

1. Create SSH

1. Check whether you have had ssh before and enter it in the terminal.

cd ~/.ssh

If No such file or directory is displayed, it means that ssh has not been created before, go directly to the next step; if you can enter directly, you need to clean up rss first, and execute the command on the terminal:

mkdir key_backup $ cp id_rsa* key_backup $ rm id_rsa*

2. Enter your own github email account to create

Enter in the terminal (replace the email account with your own Github login email)

ssh-keygen -t rsa -C "[email protected]"

You can press Enter all the way and create it at the default address.

2. Connect on github

1. Open github, click settings, then click SSH and GPG keys

2. Click New SSH key

3. Enter the following command in the terminal to enter the .ssh folder

cd ~/.ssh/

Then enter the following command to view all files

ls

Then enter the following command to open rsa.pub

cat id_rsa.pub

Then paste the displayed content into the key part of github

4. Enter the following command to determine whether it is successful.

ssh -T [email protected]

This is considered successful if “You`ve successfully authenticated”

Using SSH to push HTTP github links may result in an error. Try editing the ~/.ssh/config file (add it if it doesn’t exist). For Windows/Mac, add the following to the .ssh directory in the user directory. content:

Host github.com
    HostName ssh.github.com
    User git
    Port 443

5. Use git config to set the github login name and login email, and execute the following two commands:

git config --global user.name "your name"
 
git config --global user.email "[email protected]"

6. Execute the following command to upload your local folder

cd /Users/Tush/Documents/test
git init
git add .
git commit -m 'first commit'
git remote add origin https://github.com/XXX/XXX.git
git push -u origin master

Also note whether the last command in the previous step is “git push -u origin master” or “git push -u origin main”.

3. Configuration of multiple SSH keys to facilitate the use and management of different warehouses by Git

In the process of moving bricks, you will more or less deal with different code hosting platforms. For example, I like to use gitee (code cloud) as the image bed in md notes, and github as the code management warehouse for personal projects. The company will use other hosting platforms for management, such as Alibaba Cloud.

After a single SSH key is used and takes effect, configuring other platforms will cause various problems such as invalidation. After reading the SSH keys generation tutorials for each warehouse, basically only a single SSH key can be generated. It is not very helpful when we want to operate projects on multiple code hosting platforms at the same time.

In the process of configuring multiple SSH keys, I recorded the configuration process and some problems encountered in this article, hoping to provide you with some help.

1. Generate SSH-Keys specified for each platform

Open git Bash Here, enter the command, modify the email address and press Enter all the way. If you need to set a password, you can configure it yourself.

-t: “is the type of the specified key”

-C: “It is a comment designated to identify the key, that is, you can fill in any value, usually fill in the email address”

//Generate a default id_rsa first. Failure to generate it may cause the configuration keys of the alias to not take effect.

$ ssh-keygen -t rsa -C "[email protected]" -f ~/.ssh/id_rsa

// Then specify aliases in turn to generate other ssh_key files for the platform.

$ ssh-keygen -t rsa -C "[email protected]" -f ~/.ssh/github_rsa
$ ssh-keygen -t rsa -C "[email protected]" -f ~/.ssh/gitee_rsa
$ ssh-keygen -t rsa -C "[email protected]" -f ~/.ssh/aliyun_rsa

At this time, there should be 8 files in the user .ssh directory under the username of the system disk: id_rsa and id_rsa.pub, github_rsa and github_rsa.pub, gitee_rsa and gitee_rsa.pub, aliyun_rsa and aliyun_rsa.pub.

Possible errors:

  1. The generated public key file (.pub) is not in the .ssh folder, or the location of the generated file is incorrect. Please check the current path of the console and the generated path.

  2. Some consoles may not recognize “~”. To modify the added path, use “./” directly.

2. Configure public key

Copy and paste all the contents of their public key files (.pub) to the corresponding code hosting repository.

Take github as an example, as shown in the figure:

Add the public key of the corresponding platform to the Key box. The Title can be filled in at will, as long as it is identifiable.

Finally click Add SSH key to save.

3. Configure private key

//Execute the command first:

ssh-agent bash

//Add the private key again

$ ssh-add ~/.ssh/github_rsa
$ ssh-add ~/.ssh/gitee_rsa
$ ssh-add ~/.ssh/aliyun_rsa

// The addition is successful as shown in the figure below.

Some command parameters of ssh-add are as follows:

-D: Delete all keys in ssh-agent.
-d: Remove key from ssh-agent
-L: Display the public key in ssh-agent
-l: Display the private key in ssh-agent

Add the private key and run the command ssh-add -l to view the list of currently successfully configured private keys, as shown in the figure:

ssh-add -l

Possible errors:

  1. ssh-agent bash prompts error message: unable to start ssh-agent service, error:1058

Solution: Open Power Shell with administrator rights and execute:

Set-Service -Name ssh-agent -StartupType automatic
  1. Adding private key prompts error message: No such file or directory

Solution: Check whether the current console path matches the added path and adjust the path, as shown in the figure above.

4. Add config configuration file

If there is no config file in the .ssh directory under the username of the system disk, create a new config file. This file has no file extension, and the type is displayed as file. After double-clicking, it can be opened for editing with Notepad, etc.

My configuration file is as follows, please refer to the configuration file parameter description:

# Configuration file parameters
# Host: Configure the corresponding host name and ssh file (you can directly fill in the ip address)
# HostName: The host name of the host to be logged in (it is recommended to be consistent with Host)
# PreferredAuthentications: Configure login verification method (including password, you can query the configuration method by yourself)
# IdentityFile: Indicate the configuration path corresponding to User above
# User: Login name (such as github username)
# Port: port number (default 22)

#github.com
Hostgithub.com
HostName github.com
PreferredAuthentications publickey
IdentityFile C:/Users/tag_a12/.ssh/github_rsa
User [email protected]

#aliyun.com
Host code.aliyun.com
HostName code.aliyun.com
PreferredAuthentications publickey
IdentityFile C:/Users/tag_a12/.ssh/aliyun_rsa
User [email protected]

5. Test SSH link

//Command statement

$ ssh -T git@[Host configuration name]

//Use as follows:

$ ssh -T [email protected]
$ ssh -T [email protected]

Tips for success:

Welcome to GIT, your name!

You can also directly clone or submit the project for testing. My suggestion is to conduct a link test on each platform after the configuration is completed.

Possible errors:

  1. When submitting the code, it prompts Host key verification failed.

Reason: If your project is cloned locally and then the SSH keys are modified, you will not be able to log in because the public key is different when submitting.

Resolution: Only a link test is required to resolve this issue.

The above is the configuration process of multiple SSH-Keys. If you find other problems during the configuration process or there is something you don’t understand, please leave a message for discussion.