~/.ssh/config文件的使用

Host github-A
    HostName github.com
    User git
    IdentityFile /Users/xxx/.ssh/id_rsa_A
    IdentitiesOnly yes
Host github-B
    HostName github.com
    User git
    IdentityFile /Users/xxx/.ssh/id_rsa_B
    IdentitiesOnly yes

config文件保存不同host的所使用的ssh密钥。

使用场景:

问题

你在github有多个账号,当push代码时需要使用不同的ssh-key,但是git remote -v看到默认都是git@github.com:xxxRepo/xxx.git(git是用户名,github.com是Host名)。这样一来,使用的是同一份配置,同一份私钥。

解决方法:

如上,两份不同Host的配置。然后在repo下,git remote set-url origin git@github-A:repoA/xxx.git 就可以区分开了

config配置含义

  • Host
    用来指定该key的Host名字,此处必须使用本地repo的hostname github-A
    Host必须跟repo的hostname保持一致,也就是git到时候会用自己repo的hostname来ssh配置文件里面找是不是有对应的Host,找到了就使用该配置,具体访问的域名会采用HostName
  • Hostname
    此处指定Host对应的具体域名
  • User git
    说明该配置的用户得是git
  • IdentityFile /Users/xxx/.ssh/id_rsa.github
    这行最为关键,指定了该使用哪个ssh key文件,这里的key文件一定指的是私钥文件。
  • IdentitiesOnly yes
    配置为yes,具体意义可以参考讨论。配置文件中可以忽略此项。



作者:阳台的晾衣架
链接:https://www.jianshu.com/p/45201d18cc7c
来源:简书

原文地址:https://www.cnblogs.com/cangqinglang/p/11727867.html