一台机器挂两个github账号

使用ssh连github不需要输入密码。但是一个public key只能在github上用一次,所以记录下在网上找到的解决方法。同一个电脑上生成两个key,分布用于不同的github账号。

生成多个公钥

公钥的生成命令是’ssh-keygen’,但是如果本身已经存在一个公钥,会要求你输入一个新的名字,效果如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
cqzhao@imi-cqzhao:~/.ssh$ ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/home/cqzhao/.ssh/id_rsa): id_rsa_rxy
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in test.
Your public key has been saved in test.pub.
The key fingerprint is:
SHA256:pn1jf3OjCNI2Z0vntsYfm5lZ4FkY+7nZPsc3Y9WTNSY cqzhao@imi-cqzhao
The key's randomart image is:
+---[RSA 2048]----+
| |
| |
| . |
| E *.|
| S * *|
| +. . B+|
| ...=++..o+=|
| oo*o=+o*^|
| o+==&X|
+----[SHA256]-----+
cqzhao@imi-cqzhao:~/.ssh$

接着就会发现当先目录下多两个文件id_rsa_rxyid_rsa_rxy.pub。接着就是把id_rsa_rxy.pub里的公钥丢到github里去。注意该文件里所有的字符,包括空格都是公钥的一部分。

配置公钥文件

现在电脑里有了两对公钥私钥,效果如下:

1
2
3
4
5
6
7
8
9
10
qzhao@imi-cqzhao:~/.ssh$ ls -l
total 28
drwxrwxr-x 2 cqzhao cqzhao 4096 Feb 2 11:00 backup
-rw-rw-r-- 1 cqzhao cqzhao 155 Feb 2 14:53 config
-rw------- 1 cqzhao cqzhao 1766 Feb 2 11:20 id_rsa
-rw-r--r-- 1 cqzhao cqzhao 399 Feb 2 11:27 id_rsa.pub
-rw------- 1 cqzhao cqzhao 1766 Feb 2 14:13 id_rsa_rxy
-rw-r--r-- 1 cqzhao cqzhao 399 Feb 2 14:13 id_rsa_rxy.pub
-rw-r--r-- 1 cqzhao cqzhao 1770 Dec 1 15:26 known_hosts
cqzhao@imi-cqzhao:~/.ssh$ i

配置的目的就是根据主机的不用自动选择公钥私钥。github使用ssh连接时需要配置主机,也就是需要git remote一波。 那么使用两个github账号也就是说要两个github的仓库并且是由不同的人创建。而且在配置remote的时候,主机的名字要特别设置的不一样如下

1
2
3
4
5
6
7
8
9
10
11
12
13
cqzhao@imi-cqzhao:/data/projects/blog_github$ git remote -v
remote_github git@github.com:zhaochenqiu/zhaochenqiu.github.io.git (fetch)
remote_github git@github.com:zhaochenqiu/zhaochenqiu.github.io.git (push)
remote_imi /home/cqzhao/repository/blog/ (fetch)
remote_imi /home/cqzhao/repository/blog/ (push)
cqzhao@imi-cqzhao:/data/projects/blog_github$

cqzhao@imi-cqzhao:/data/projects/latex/draft_DFTL$ git remote -v
origin git@github.com:HsinyuJen/draft_DFTL.git (fetch)
origin git@github.com:HsinyuJen/draft_DFTL.git (push)
remote_github git@rxy.github.com:HsinyuJen/draft_DFTL.git (fetch)
remote_github git@rxy.github.com:HsinyuJen/draft_DFTL.git (push)
cqzhao@imi-cqzhao:/data/projects/latex/draft_DFTL$

注意上面的两个github主机,一个是`git@github.com:XXX,一个是git@rxy.github.com:`。而rxy.github其实是我们自己定义的一个主机。 根据这两个不同的主机名,就可以使用不用的key,所以在.ssh目录下要新建一个config文件。然后内容如下

1
2
3
4
5
6
7
8
9
Host rxy.github.com
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa_rxy

Host github.com
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa

这样提交的时候,ssh就会自动根据配置文件来选择合适的key。