生成多个git ssh秘钥



1.问题阐述

当有多个git账号的时候,比如一个github,用于自己进行一些开发活动,再来一个gitlab,一般是公司内部的git。这两者你的邮箱如果不同的话,就会涉及到一个问题,生成第二个gitkey的时候会覆盖第一个的key,导致必然有一个用不了。

解决办法

生成第一个ssh key(这里用于github,用的私人邮箱)

ssh-keygen -t rsa -C "[email protected]"  -f ~/.ssh/id_rsa_github

生成第二个ssh key(这里用于gitlab,用的是公司邮箱)

ssh-keygen -t rsa -C "yourmail@你司.com"  -f ~/.ssh/id_rsa_gitlab 

打开ssh-agent
这里如果你用的github官方的bashssh-agent -s,如果是其他的,比如msysgit,eval $(ssh-agent -s)

添加私钥

  ssh-add ~/.ssh/id_rsa_github

  ssh-add ~/.ssh/id_rsa_gitlab

如果执行ssh-add时提示"Could not open a connection to your authentication agent",可以现执行命令:

ssh-agent bash

# 可以通过 ssh-add -l 来确私钥列表

$ ssh-add -l

# 可以通过 ssh-add -D 来清空私钥列表

$ ssh-add -D


创建并修改config文件

vim  config

# gitlab

    Host git.iboxpay.com

       HostName git.你司.com  //这里填你们公司的git网址即可

        PreferredAuthentications publickey

        IdentityFile ~/.ssh/id_rsa_gitlab

        User yourname

    

    # github

   Host github.com

        HostName github.com

        PreferredAuthentications publickey

        IdentityFile ~/.ssh/id_rsa_github

        User yourname2

githubgitlab上添加公钥即可,这里不再多说。





猜你喜欢

转载自blog.csdn.net/weiguang111/article/details/79504437