↓ 跳过正文
  1. Posts/

macOS配置多个Github账号的ssh-key

·290 字·1 分钟
目录

背景
#

目前我有两个常用的github账号,为了都能方便的提交代码,需要配置成ssh-key。

创建ssh-key
#

ssh-keygen -t ed25519 -f ~/.ssh/github-xlab -C "hello@xlab.im"
ssh-keygen -t ed25519 -f ~/.ssh/github-other -C "hello@xlab.im"

修改配置文件
#

编辑~/.ssh/config:

 Host github.com
   HostName github.com
   AddKeysToAgent yes
   UseKeychain yes
   IdentityFile ~/.ssh/github-other


  Host github-xlab
    HostName github.com
    User git
    AddKeysToAgent yes
    UseKeychain yes
    IdentityFile ~/.ssh/github-xlab

将私钥添加到 SSH Agent 和 macOS Keychain
#

一般情况下,直接使用ssh -T git@github.com和ssh -T git@github-xlab就能生效,以前我也是这么配置的,但是这次居然每次都返回了同一个用户。原因就是macOS 有时不会自动加载指定的私钥。

运行如下命令测试并添加

# 将私钥添加到 SSH Agent 和 macOS Keychain
ssh-add --apple-use-keychain ~/.ssh/github-xlab

# 确认密钥是否已在 Agent 列表中
ssh-add -l

此时,再去测试就正确了。

git submodule update --init --recursive