preface

Have you changed your computer recently? Remember how to configure multiple Git SSH connections locally? Generally, companies use the GitLab server deployed on their own Intranet for code management, and developers use the company’s user name and company’s email. However, in personal open source projects, our code is hosted on Github, so we need two or more SSH-keys to log in, which is convenient for pulling and pushing codes.

The article Outlines

  • View all SSH keys
  • Configure SSH for gitLab Intranet and Github extranet respectively
  • test

Step 1: View all sSH-keys

To open the bash/ ZSH terminal, run the following command

$ cd ~/.ssh/
$ ls
Copy the code

The output is as follows:

KaydeMBP:~ kayliang$ cd ~/.ssh/
KaydeMBP:.ssh kayliang$ ls
config			github_id-rsa.pub	gitlabnei_id-rsa.pub
github_id-rsa		gitlabnei_id-rsa	known_hosts
Copy the code

Above is the SSH key that has been configured on my computer. Next, I’ll walk you through the configuration step by step.

Step 2: Generate an SSH-key to configure the GitLab on the Intranet

The gitlabnei_id-rsa and gitlabnei_id-rsa.pub private and public keys are generated in the ~/. SSH/directory.

$ ssh-keygen -t rsa -C "[email protected]" -f ~/.ssh/gitlabnei_id-rsa
Copy the code

Check your public key. We pasted the contents of gitlabneiID-rsa. pub into the sSH-key configuration of the company’s GitLab server.

Print the contents of the file to the command line tool for easy copying
cat ~/.ssh/gitlabnei_id-rsa.pub
Copy the code

Remember to copy the entire text, including Ssh-RSA, to the clipboard as shown below.

ssh - rsa;

xxxxxxxxx;
xxxxx;
Copy the code

Then paste it into the site’s SSH configuration:

Step 3: Generate an SSH key to configure Github on the Internet

The github_id-rsa and github_id-rsa.pub private and public keys are generated in the ~/. SSH/directory. We pasted the contents of githubid-rsa. pub into the ssh-key configuration of github server.

ssh-keygen -t rsa -C "[email protected]" -f ~/.ssh/github_id-rsa
Copy the code

Step 4: Add it in ~/.sshconfigThe configuration file is used to distinguish multiple SSH-keys

1. Add a new private key for identifying SSH keys

By default, only ID_RSA is read. To enable SSH to recognize the new private key, add the new private key to SSH Agent

The private key has been added
ssh-add -l

3072 SHA256:xxxxxxxxxx [email protected] (RSA)
Copy the code
$ ssh-agent bash
$ ssh-add ~/.ssh/github_id-rsa
$ ssh-add ~/.ssh/gitlabnei_id-rsa
Copy the code

This step is important, otherwise you will get an error in git clone.

KaydeMBP:chengdu-project kayliang$ git clone ssh://[email protected]:5337/xdata/SH2019GH109/chengdu-natural-resources-cli3.git
Cloning into 'chengdu-natural-resources-cli3'. no such identity: gitlabnei_id-rsa: No such file or directory [email protected]: Permission denied (publickey). fatal: Could notread from remote repository.

Please make sure you have the correct access rights
and the repository exists.
Copy the code

2. Add the Config configuration file

Use vi to create or open the config file and press I to enter editing mode. After editing, press Esc + : and enter wq to save the configuration and exit. See the Vim quick Guide in this article for specific commands.

vi ~/.ssh/config
Copy the code

The configuration file reads as follows, but it is important to note that the configuration information must be the same as your SSH address in the repository, because sometimes the HTTP address and SSH address port are different.

Address: ssh://[email protected]:5337/xdata/SH2019GH109/xxxxx.git

Based on the preceding address, you can configure the Host as xxxxx.amazonaws.com.cn and port as 5337.

# gitlab
  Host xxxxx.amazonaws.com.cn This is the IP of the SSH address
  PreferredAuthentications publickey
  IdentityFile ~/.ssh/gitlabnei_id-rsa
  Port 5337 This is the port of the SSH address

# github
Host github.com
  HostName github.com
  PreferredAuthentications publickey
  IdentityFile ~/.ssh/github_id-rsa
  
# More configuration...
Copy the code

Configuration file parameters:

  • Host: can be thought of as a pattern that you want to recognizeThe host nameandsshFile.
  • HostName: indicates the HostName to be logged in to.
  • User: indicates the login name.
  • IdentityFile: specifies the IdentityFile path corresponding to the User.
  • Port: indicates the SSH Port

Step 5: Test

Example Test github extranet SSH address

ssh -T [email protected]
Copy the code

Output:

Hi Jecyu! You've successfully authenticated, but GitHub does not provide shell access.
Copy the code

Example Test the SSH address of the Intranet

ssh -T [email protected]
Welcome to GitLab, @linjy!
Copy the code

Represents a successful connection to the company’s GITLab.

The last

Use Git Clone to get your github project and gitLab Intranet project respectively:

Note: If you use the warehouse address starting with HTTPS, you will be asked to authenticate the password

git clone ssh://xxxx.amazonaws.com.cn:5337/xdata/SH2020GH036/xxxx.git 
cd my-project
touch README.md
git add README.md
git commit -m "add README"
git push -u origin master
Copy the code

Since the accounts and email addresses of personal and corporate projects are different, remember to configure them in their respective repositories so that the user and email address of the commit record are correct. Of course, this step does not affect whether you can use SSH to commit and obtain the commit record, just to make it clear when looking at the commit record.

 git config user.name xxxx
 git config user.email [email protected]
Copy the code

summary

This article is the author in the practice of the record, but also referred to some excellent online author experience, if there is a mistake please point out, if it is helpful to you, point to support.

The resources

  • Further reading, SSH knowledge: segmentfault.com/q/101000000… And SSH, The Secure Shell.
  • Git manual