~/.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密鑰。html

使用場景:

問題

你在github有多個帳號,當push代碼時須要使用不一樣的ssh-key,可是git remote -v看到默認都是git@github.com:xxxRepo/xxx.git(git是用戶名,github.com是Host名)。這樣一來,使用的是同一份配置,同一份私鑰。git

解決方法:

如上,兩份不一樣Host的配置。而後在repo下,git remote set-url origin git@github-A:repoA/xxx.git 就能夠區分開了github

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
來源:簡書

ssh

相關文章
相關標籤/搜索