Multiple git accounts access different git repositories at the same time

We often encounter the need to access the github repository and the gitlab repository, but the ssh key switching is too troublesome. You can configure the config file to perform the directional pointing of the ssh keys of multiple warehouses.

 

1. Create a new config file in the ~/.ssh directory

 

[plain] view plain copy

  1. touch config  

 

2. Create a new gitlab and github file directory

 

[plain] view plain copy

  1. mkdir gitlab  
  2. mkdir github  

3. Add edit configuration in config file

 

 

[plain] view plain copy

  1. sudo vim config  

 

[plain] view plain copy

  1. Host gitlab  
  2.   HostName gitlab.com  
  3.   User *****@**.com  
  4.   IdentityFile ~/.ssh/gitlab/id_rsa  
  5. Host oschina  
  6.   HostName oschina.net  
  7.   User *****@**.com  
  8.   IdentityFile ~/.ssh/oschina/id_rsa  
  9. Host github  
  10.   HostName github.com  
  11.   User *****@**.com  
  12.   IdentityFile ~/.ssh/github/id_rsa  

 

Host is your own identification mark, you can write it casually

HostName is the host address of the warehouse

User is the mailbox of the warehouse account

IdentityFile is the path to the corresponding key store

 

Save the above information.
 

Put the respective keys in their respective file directories.

 

 

====================

https://www.cnblogs.com/BeginMan/p/3548139.html

 

SSH key switching for multiple github accounts

 

 

I have two github accounts, one for personal use and one for company projects. If it is a single-user (single-user), it is very convenient. By default, id_rsa is compared with the public key of your github server; if it is a multi-user (multi-user) such as user1, user2, then it cannot be used on user2. At this time, configure it:

1. Create a new SSH Key for user2

#新建SSH key:
$ cd ~/.ssh     # 切换到C:\Users\Administrator\.ssh
ssh-keygen -t rsa -C "[email protected]"  # 新建工作的SSH key
# 设置名称为id_rsa_work
Enter file in which to save the key (/c/Users/Administrator/.ssh/id_rsa): id_rsa_work

2. The new key is added to the SSH agent

Since only id_rsa is read by default, in order for SSH to recognize the new private key, it needs to be added to the SSH agent:

ssh-add ~/.ssh/id_rsa_work

If you get the Could not open a connection to your authentication agent error, try the following command:

ssh-agent bash
ssh-add ~/.ssh/id_rsa_work

3. Modify
the config file Find the config file in the ~/.ssh directory, if not, create it:

touch config        # 创建config

Then modify it as follows:
My config configuration is as follows:

# 该文件用于配置私钥对应的服务器
# Default github user([email protected])
Host github.com
 HostName github.com
 User git
 IdentityFile C:/Users/Administrator/.ssh/id_rsa

 # second user([email protected])
 # 建一个github别名,新建的帐号使用这个别名做克隆和更新
Host github2
 HostName github.com
 User git
 IdentityFile C:/Users/Administrator/.ssh/id_rsa_work

If it exists, it is actually adding a Host to this config:

#建一个github别名,新建的帐号使用这个别名做克隆和更新
Host github2
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa2

The rule is: read the content of config from top to bottom, and find the corresponding private key under each Host. Here, replace [email protected] in the address of the GitHub  SSH warehouse with a new host alias such as : github2, then the original address is: [email protected] :funpeng/Mywork.git, after the replacement it should be: github2:funpeng/Mywork .git.

4. Open the newly generated ~/.ssh/id_rsa2.pub file and add the contents to the GitHub background.

But don't forget to add it to your SSH Key under another github account.

5. Test:

Administrator@FANGPENG /e/work
$ ssh -T [email protected]
Hi BeginMan! You've successfully authenticated, but GitHub does not provide shel
l access.

Administrator@FANGPENG /e/work
$ ssh -T github2
Hi funpeng! You've successfully authenticated, but GitHub does not provide shell
 access.

6. Application

The test is successful, then I try to clone the remote repository under my @126.com account in my work directory . as follows:

Administrator@FANGPENG /e/work
$ git clone github2:funpeng/Mywork.git
Cloning into 'Mywork'...
remote: Counting objects: 3, done.
remote: Total 3 (delta 0), reused 0 (delta 0)
Receiving objects: 100% (3/3), done.
Checking connectivity... done

The clone was successful, and you're done!

Thanks for the reference:

1. SSH key switching for multiple github accounts

2. Add SSH public keys to multiple GitHub accounts

3.git.md

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324440682&siteId=291194637