需要场景:本地仓库添加了公司的gitlab或者github账号,你还想添加自己的github账号
  • 生成一个新账号的ssh-key
     ssh-key默认保存文件为id_rsa,新的ssh-key不能和默认的相同
  •        
           
    1. ssh-keygen -t rsa -b 4096 -C "your_email@example.com" -f ~/.ssh/xxx_rsa
  • 新密钥添加到SSH agent中
     默认只会读取id_rsa,为了让SSH识别新的私钥,需将其添加到SSH agent中
  •         
            
    1. ssh-add ~/.ssh/xxx_rsa
    如果出现 Could not open a connection to your authentication agent的错误,就试着用以下命令
             
             
    1. ssh-agent bash
    2. ssh-add ~/.ssh/xxx_rsa
  • 修改config文件
     在~/.ssh目录下找到config文件,如果没有就创建
  •        
           
    1. touch config # 创建config文件
    然后修改配置如下
# 该文件用于配置私钥对应的服务器
# gitlab
Host gitlab.com
    HostName gitlab.com
    PreferredAuthentications publickey
    IdentityFile ~/.ssh/id_rsa
# github
Host github.com
    HostName github.com
    PreferredAuthentications publickey
    IdentityFile ~/.ssh/github_rsa
  • 添加ssh-key到github后台
Logo

瓜分20万奖金 获得内推名额 丰厚实物奖励 易参与易上手

更多推荐