博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
如何在同一台电脑上使用两个github账户
阅读量:7165 次
发布时间:2019-06-29

本文共 1452 字,大约阅读时间需要 4 分钟。

1. 新建.ssh目录

 

 

2. 生成并添加第一个ssh key

$ ssh-keygen -t rsa -C "youremail@yourcompany.com" -f id_rsa_A

在 Bash中执行这条命令一路回车,会在 ~/.ssh/ 目录下生成 id_rsa_A 和 id_rsa_A.pub 两个文件,用文本编辑器将 id_rsa_A.pub 中的内容复制一下粘贴到账户A的github上

 

 

3. 生成并添加第二个ssh key

$ ssh-keygen -t rsa -C "youremail@yourcompany.com" -f id_rsa_B

在 Bash中执行这条命令一路回车,会在 ~/.ssh/ 目录下生成 id_rsa_B 和 id_rsa_B.pub 两个文件,用文本编辑器将 id_rsa_B.pub 中的内容复制一下粘贴到账户B的github上

 

 

4. 添加私钥

在 ~/.ssh 目录下新建一个known_hosts文件

$ touch known_hosts

 

git自动把新生成的私钥写到known_hosts中

$ ssh-add ~/.ssh/id_rsa_A$ ssh-add ~/.ssh/id_rsa_B

 

如果执行ssh-add时提示"Could not open a connection to your authentication agent",可以现执行命令:

$ ssh-agent bash

 

然后再运行ssh-add命令

# 可以通过 ssh-add -l 来确私钥列表$ ssh-add -l# 可以通过 ssh-add -D 来清空私钥列表$ ssh-add -D

 

 

5. 修改配置文件

在 ~/.ssh 目录下新建一个config文件

$ touch config

 

添加内容:(此处新加你的私钥目录和私钥对应的HostName)

# githubHost github_A    HostName github.com    PreferredAuthentications publickey    IdentityFile ~/.ssh/id_rsa_A# githubHost github_B    HostName github.com    PreferredAuthentications publickey    IdentityFile ~/.ssh/id_rsa_B

这样的话,你就可以通过使用github.com别名github_A(github_B)来明确说你是要使用id_rsa_A(id_rsa_B)的SSH key来连接github,即账号A(账号B)进行操作。

以后clone或者push时,git@github.com:xxxx/test.git   =>   git@github_A:xxxx/test.git或者git@github_B:xxxx/test.git

 

 

6. 测试

$ ssh -T git@github_A

或者

$ ssh -T git@github_B

输出:"Hi user! You've successfully authenticated, but GitHub does not provide shell access. ",就表示成功的连上github了

 

转载于:https://www.cnblogs.com/softwarefang/p/9350296.html

你可能感兴趣的文章