在GitHub多个帐号上添加同一台机器的SSH公钥
本人工作时使用公司在阿里码云上的公司内部账号拉代码,突然有一天想要把自己业余时间做的一个小APP代码放在自己在码云上的私人号上,发现PC上的ssh公钥不能添加到私人账号,因为在同一个网站上自己的私人账号与公司的内部账号是不可以共享一份ssh公钥的,于是有了下面的经验:
转载
1、https://www.cnblogs.com/myall/p/6675741.html
2、https://blog.****.net/ChansonZuo/article/details/52944045
3、https://stackoverflow.com/questions/48328446/id-rsa-pub-file-ssh-error-invalid-format
GitHub后台可以添加多个SSH Keys,但是同一个SSH Keys只能在添加在一个帐号上(添加时提示“Key is already in use”)。理由很容易想到,SSH公钥使用时相当于用户名密码,不可能两个不同的帐号使用同一个用户名密码。要想在多个GitHub帐号上添加公钥,就要在本地生成多个SSH Keys,每个GitHub帐号对应一个不同的SSH Keys。步骤如下:
1、生成一个新的SSH KEY
- [email protected]:~$ ssh-****** -t rsa -C '[email protected]'
- Generating public/private rsa key pair.
- Enter file in which to save the key (~/.ssh/id_rsa): ~/.ssh/id_rsa2 #这里输入一个新的ssh key文件名
- Enter passphrase (empty for no passphrase):
- Enter same passphrase again:
- Your identification has been saved in ~/.ssh/id_rsa2.
- Your public key has been saved in ~/.ssh/id_rsa2.pub.
- The key fingerprint is:
- 3a:01:17:b3:f9:26:5b:53:b3:69:be:71:a8:66:f6:96 [email protected]
- The key's randomart image is:
- +--[ RSA 2048]----+
- | o |
- | = |
- | . + o |
- | . . . + |
- | o S + |
- | B + . |
- | + .+ + |
- | .E..+ |
- | +.oo |
- +-----------------+
- [email protected]:~$ ssh-add ~/.ssh/id_rsa2
- Identity added: ~/.ssh/id_rsa2 (~/.ssh/id_rsa2)
~/.ssh/id_rsa2为新SSH Keys文件名,根据实际情况修改,保证每次不一样即可。
2、打开新生成的~/.ssh/id_rsa2.pub文件,将里面的内容添加到GitHub后台。
3、打开~/.ssh/config文件(没有则创建),添加一个Host:
- #建一个github别名,新建的帐号使用这个别名做克隆和更新
- Host github2
- HostName github.com
- User git
- IdentityFile ~/.ssh/id_rsa2.pub
4、将GitHub SSH仓库地址中的[email protected]替换成新建的Host别名。
如原地址是:[email protected]:freehost/mail.git,替换后应该是:github2:freehost/mail.git
如果是新建的仓库,直接使用替换后的URL克隆即可。如果已经使用原地址克隆过了,可以使用命令修改:
- git remote set-url origin github2:freehost/mail.git
然后发现ss-add:
于是使用第二篇文章中的方法:
其中私钥文件路径自己手动输入多次均失败,最后是点击复制路径得到的内容才可以,不知道是不是自己手动输入有编码问题
可以git clone的时候又出问题:
然后再第三篇文章中得知:
第一篇文章中:
不应该指向公钥文件路径,应该指向私钥文件路径: