0%

本地支持多Github账号

创建非默认本地SSH密钥

GitHub不支持相同SSH Key在不同GitHub账户下使用。所以除默认账户外,需要单独为新GitHub账号建立一个SSH Key。注意,使用ssh-keygen生成新key,出现提示输入文件名的时候(Enter file in which to save the key (~/.ssh/id_rsa): id_rsa_new)要输入与默认配置不一样的文件名,比如:我这里填的是 id_rsa_new。否则会覆盖之前生成的默认key。并用ssh-add命令将新创建的Key添加到由ssh-agent 维护的列表中。

ssh-agent 是用于管理SSH private keys的, 长时间持续运行的守护进程(daemon). 唯一目的就是对解密的私钥进行高速缓存。

ssh-add 提示并将用户的使用的私钥添加到由ssh-agent 维护的列表中. 此后, 当使用公钥连接到远程 SSH 或 SCP 主机时,不再提示相关信息.

1
2
ssh-keygen -t rsa -b 2048 -C "[email protected]"
ssh-add ~/.ssh/id_rsa_new

如果出现 Could not open a connection to your authentication agent 的错误,就试着用以下命令:

1
2
$ ssh-agent bash
$ ssh-add ~/.ssh/id_rsa_new

配置SSH

配置~/.ssh/config

1
2
3
4
5
6
7
8
9
10
11
# 默认的GitHub连接
Host [email protected]
HostName github.com
User username #<- GitHub的用户名
IdentityFile ~/.ssh/id_rsa_github

# 另一个GitHub的连接,带上GitHub用户名作为name前缀
Host name.github.com
HostName github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa_new

测试一下

1
2
3
4
5
✦2 ➜ ssh -T [email protected]
Hi <name>! You've successfully authenticated, but GitHub does not provide shell access.

✦2 ➜ ssh -T [email protected]
Hi <default>! You've successfully authenticated, but GitHub does not provide shell access.

在向非默认仓库提交代码时,需要对原远程Push地址做一下转换

1
git remote add origin [email protected]:name/hello.git

试一下吧!

参考

https://github.com/jawil/notes/issues/2