background

In daily work, the company’s code is confidential, so the conventional means is to deploy a private GitLab service on the Intranet, and then add access rights for our domain account. Some of the code also needs to be open source, so we will also submit the code to Github, at this time we need to use two SSH-keys to manage different repositories. Here’s how to use SSH to manage both GitLab and Github accounts on the same MAC.

Generate sshkey

Create a.ssh directory in the user directory. If yes, skip this step
mkdir ~/.ssh

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

The following four files will now be generated

~/.ssh/id_rsa_github
~/.ssh/id_rsa_github.pub
~/.ssh/id_rsa_gitlab
~/.ssh/id_rsa_gitlab.pub
Copy the code

Configure it on the web page

Github Configures the public key

#Copy the generated public key to the clipboard
pbcopy < ~/.ssh/id_rsa_github.pub
Copy the code

Log in to github, go to Settings > SSH and GPG Keys > New SSH key location, and paste the public key.

Gitlab configures the public key

#Copy the generated public key to the clipboard
pbcopy < ~/.ssh/id_rsa_gitlab.pub
Copy the code

Log in to the Gitlab account, go to Settings > SSH Keys, paste the public key and use it.

Use ssh-agent to manage SSH keys

ssh-add ~/.ssh/id_rsa_github
ssh-add ~/.ssh/id_rsa_gitlab
Copy the code

Create an SSH configuration file

#Create a file
touch ~/.ssh/config
#Edit the file
vi ~/.ssh/config
Copy the code

Change the configuration using the following information as a template

# Personal github account
Host github.com
   HostName github.com
   User git
   IdentityFile ~/.ssh/id_rsa_github
# Personal gitlab account
Host gitlab.com
   HostName gitlab.com
   User bgit
   IdentityFile ~/.ssh/id_rsa_gitlab
Copy the code

Run the following command to test the configuration

ssh -T [email protected]
Copy the code

If successful, you will see the following tips