Skip to content

本地电脑同时配置gitlab和github的SSH #2

@H-Stephen

Description

@H-Stephen
Owner

思路与方案

  1. 能够生成两对 私钥/公钥 -- 文件命名避免重复
  2. push 时,可以区分两个账户,推送到相应的仓库 -- 为每个仓库独立设置 用户名/邮箱

操作步骤

windows环境下

1、生成两对公私密钥

默认生成的文件名是 id_rsa/id_rsa.pub,由于这里需要两套密钥,需为它们分别命名,防止被覆盖 -- 默认路径在用户根目录下的.ssh文件夹中

执行下面命令生成密钥:

ssh-keygen -t rsa -C "注册 gitlab 账户的邮箱"

提示输入名称时输入: id_rsa_gitlab

ssh-keygen -t rsa -C "注册 github 账户的邮箱"

提示输入名称时输入: id_rsa_github

2、更新用户SSH配置

打开~/.ssh/config文件(没有就新建一个),并在文件中加入以下内容

# default  
Host github.com
HostName github.com
User githubuser@xyz.com(GitHub邮箱)
IdentityFile ~/.ssh/id_rsa_github
# two 
Host gitlab.com(这里注意填写自己公司对于的gitlab host)
HostName gitlab.com
User gitlabuser@xyz.com(Gitlab邮箱)
IdentityFile ~/.ssh/id_rsa_gitlab

3、git仓库配置

把常用的账户设为全局(默认上面配置的第一个),不常用的进入项目文件夹单独设置 用户名/邮箱

# 单独设置每个repo 用户名/邮箱
git config user.name “xxxx”
git config user.email “xxxx@xx.com”
# 设置全局 用户名/邮箱 配置
git config –-global user.name “xxxx”
git config –-global user.email “xxxx@xx.com”

4、提供秘钥给服务器

1、复制 ~/.ssh/id_rsa_gitlab.pub文件内容,进入gitlab / profile / SSH Keys,将公钥内容添加至 gitlab
2、复制 ~/.ssh/id_rsa_github.pub文件内容,进入github / setting / SSH and GPG keys / New SSH key 将公钥内容添加至 github

Activity

changed the title [-]本地电脑同时配置gitlab和github的ssh[/-] [+]本地电脑同时配置gitlab和github的SSH[/+] on Sep 25, 2019
pinned this issue on Sep 25, 2019
unpinned this issue on Sep 25, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @H-Stephen

        Issue actions

          本地电脑同时配置gitlab和github的SSH · Issue #2 · H-Stephen/Blog