Mac设置多个GitHub账号

为多个账号设置 ssh keys

假定我们有两个账号,一个lockshell,一个zhangsan。 分别用不同 github 账号对应的邮箱生成 ssh key,示例:

$ ssh-keygen -t rsa -C "[email protected]"
Generating public/private rsa key pair.
Enter file in which to save the key (/Users/lockshell/.ssh/id_rsa): id_rsa_lockshell
...

$ ssh-keygen -t rsa -C "[email protected]"
Generating public/private rsa key pair.
Enter file in which to save the key (/Users/lockshell/.ssh/id_rsa): id_rsa_zhangsan
...

将公钥添加到对应的 github 账号

添加配置文件管理私钥

.ssh目录下新建config文件,添加如下配置:

### Github
Host lockshell
    HostName github.com
    User lockshell
    IdentityFile ~/.ssh/id_rsa_lockshell
  
Host zhangsan
    HostName github.com
    User zhangsan
    IdentityFile ~/.ssh/id_rsa_zhangsan

更新Identities

清除缓存

$ ssh-add -D

All identities removed.

添加keys

$ ssh-add id_rsa_lockshell
Identity added: id_rsa_lockshell ([email protected])

查看keys

$ ssh-add -l
2048 SHA256:w0XXyh0hy/2yHSazwYerQ+8yKQ9LVxKWtEs3sl8RFq0 [email protected] (RSA)
2048 SHA256:QgaUvrNlbuwm9B2yRLzc5/eug4VjQxoToh3E6U+xq58 [email protected] (RSA)

测试key是否可用

$ ssh -T [email protected]
Warning: Permanently added the RSA host key for IP address '13.250.177.223' to the list of known hosts.
Hi lockshell! You've successfully authenticated, but GitHub does not provide shell access.

备注

本文由 络壳 原创或整理,转载请注明出处