配置gitlab/github/gitee多个ssh-key

gitlab的密钥配置

  1. git bash执行下面命令生成ssh公钥和私钥对
    ssh-keygen -t rsa -C '你的邮箱' 一路回车 
    
  2. 查看公钥内容:cat ~/.ssh/id_rsa.pub (~用户目录)
  3. 配置远程仓库公钥:个人头像 -> settings -> SSH And GPG Key -> 点击 新增SSH Key按钮 -> 复制公钥
    配置gitlab/github/gitee多个ssh-key_第1张图片

配置多个ssh-key

  1. 为gitlab生成一对秘钥ssh key
    ssh-keygen -t rsa -C '[email protected]' -f ~/.ssh/gitlab-rsa
    
  2. 为github生成一对秘钥ssh key
    ssh-keygen -t rsa -C '[email protected]' -f ~/.ssh/github-rsa
    
  3. 添加私钥
    ssh-add ~/.ssh/gitlab-rsa 
    ssh-add ~/.ssh/github-rsa
    
    执行ssh-add时提示"Could not open a connection to your authentication agent",可以现执行命令:
    ssh-agent bash
    # 确私钥列表
    ssh-add -l
    # 清空私钥列表
    ssh-add -D
    
    exit
    
    再运行ssh-add命令
  4. ~/.sshtouch config,配置多个不同的ssh key
    # gitlab
    Host gitlab.com
    HostName gitlab.com
    PreferredAuthentications publickey
    IdentityFile ~/.ssh/gitlab-rsa 
    # github
    Host github.com
    HostName github.com
    PreferredAuthentications publickey
    IdentityFile ~/.ssh/github-rsa 
      ​
    # Host和HostName填写git服务器的域名,IdentityFile指定私钥的路径
    
  5. 测试
    ssh -T -v git@[config配置的host值]
    ssh -T [email protected]
    ssh -T [email protected]
    ssh -T [email protected]
    

    Hi stefzhlg! You’ve successfully authenticated, but GitHub does not provide shell access.

你可能感兴趣的:(工具,环境,git,github)