step1: gitlab
$ ssh-keygen -t rsa -C xxx.yy@zzz.com Generating public/private rsa key pair. Enter file in which to save the key (/c/Users/xxx/.ssh/id_rsa): id_rsa_gitlab Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in id_rsa_gitlab Your public key has been saved in id_rsa_gitlab.pub
step2: github
$ ssh-keygen -t rsa -C xxx@163.com Generating public/private rsa key pair. Enter file in which to save the key (/c/Users/zzz/.ssh/id_rsa): id_rsa_github Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in id_rsa_github Your public key has been saved in id_rsa_github.pub
step3: 将得到的id_rsa/id_rsa.pub文件保存在git默认访问的.ssh的目录;
$ ls config id_rsa_github id_rsa_github.pub id_rsa_gitlab id_rsa_gitlab.pub known_hosts known_hosts.old
step4: 将github/gitlab的公钥分别上传到对应的服务器;
github:setting / SSH and GPG keys;
gitlab:setting / SSH keys
step5: 在.ssh目录创建config文本文件,完成对应的git配置;
每个账号单独配置一个Host,每个Host要取一个别名,每个Host主要配置HostName和IdentityFile两个属性即可
Host的名字可以取为自己喜欢的名字,不过这个会影响git相关命令,例如:
Host mygithub 这样定义的话,命令如下,即git@后面紧跟的名字改为mygithub;
HostName 这个是真实的域名地址 IdentityFile 这里是id_rsa的地址 PreferredAuthentications 配置登录时用什么权限认证--可设为publickey,password publickey,keyboard-interactive等 User 配置使用用户名
example:git clone git@mygithub:happyamyhope/IPM.git
# GitLab.xxx.yy Host gitlab.xxx.yy HostName gitlab.xxx.yy IdentityFile C:\Users\zzz\.ssh\id_rsa_gitlab RSAAuthentication yes PreferredAuthentications publickey User aaa.bbb@ccc.com # Github.com Host github.com HostName github.com IdentityFile /c/Users/zzz/.ssh/id_rsa_github RSAAuthentication yes PreferredAuthentications publickey User ddd@163.com
step6:测试;
$ ssh -T git@github.com /c/Users/zzz/.ssh/config line 6: Unsupported option "rsaauthentication" /c/Users/zzz/.ssh/config line 13: Unsupported option "rsaauthentication" Enter passphrase for key '/c/Users/zzz/.ssh/id_rsa_github': Hi happyamyhope! You've successfully authenticated, but GitHub does not provide shell access.
参考
1. Windows下Git多账号配置,同一电脑多个ssh-key的管理;
完