git 配置ssh key

第一次生成 ssh key

  1. 进入ssh目录  cd ~/.ssh再输入:ls ,查看文件,如果有.pub的文件,说明已有ssh key
  2. 如果没有, 则生成秘钥:ssh-keygen -t rsa -C "[email protected]",否则参考配置多个 ssh key
  3. 查看 id_rsa.pub里面的内容,cat ~/.ssh/id_rsa.pub key,并且复制到github/gitlab的settings 里

若要配置多个 ssh key

  1. ssh-keygen -t rsa -C "[email protected]" //记得取名字,不然会覆盖原来的(id_rsa_personal.pub)

  2. 重复上述步骤2,3

  3. ssh服务器默认是去找id_rsa,现在需要把这个key添加到ssh-agent中,这样ssh服务器才能认识id_rsa_personal。

ssh-add -K ~/.ssh/id_rsa_personal 通过指定-K把SSH key导入到密钥链中 查看添加的结果: ssh-add -l

     4.新建config文件进行配置,命令: vim config,使得不同的ssh key 对应不同的账号

# gitlab
Host gitlab           
    HostName tianrang-inc.com
    User git
    IdentityFile ~/.ssh/id_rsa


# github
Host github
     HostName github.com
     User git
     IdentityFile ~/.ssh/id_rsa_personal

 

说明:

  • Host 可随意设置,只是后面ssh -T测试时要用到
  • HostName 是[email protected]:aloehh/react-mobx-demo.git中的 github.com
  • IdentityFile 是私钥路径

最后: 

全部配置好后,用ssh -T git@github测试,出现'Hi aloehh! You've successfully authenticated, but GitHub does not provide shell access.'则说明成功了(记得测试时把vpn关掉)

猜你喜欢

转载自www.cnblogs.com/aloehui/p/9347581.html