使用ssh-keygen生成两个ssh key,目录内容如下:
$ ll ~/.ssh/
total 15
-rw-r--r-- 1 Cui 197121 336 九月 15 10:25 config
-rw-r--r-- 1 Cui 197121 1675 七月 19 22:56 id_rsa
-rw-r--r-- 1 Cui 197121 398 七月 19 22:56 id_rsa.pub
-rw-r--r-- 1 Cui 197121 1675 九月 15 00:09 id_rsa_other
-rw-r--r-- 1 Cui 197121 398 九月 15 00:09 id_rsa_other.pub
-rw-r--r-- 1 Cui 197121 1989 九月 12 21:57 known_hosts
ssh的config文件如下:
$ cat ~/.ssh/config
# one
Host github.com
HostName github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa
User legend9207
# two
#远程主机地址别名;git config中远程git服务地址可以配置成这个,以达到相同git服务地址使用不同ssh key的目的。
Host cuiyindeng.github.com
HostName github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa_other
User cuiyindeng
两个项目的git config文件如下:
项目1目录/.git/config
...
[remote "origin"]
url = git@github.com:legend9207/legend9207.github.io.git
fetch = +refs/heads/*:refs/remotes/origin/*
...
项目2目录/.git/config
...
[remote "origin"]
#此时的git服务地址用的是ssh config中的别名。
url = git@cuiyindeng.github.com:cuiyindeng/cuiyindeng.github.io.git
fetch = +refs/heads/*:refs/remotes/origin/*
...
参考文章:https://blog.csdn.net/wolfking0608/article/details/78512171