Skip to content

Commit

Permalink
Update use-gitlab-github-together.md
Browse files Browse the repository at this point in the history
- `config`的`Host`参数不支持星号(`*`) +  主机名混用的通配符模式,这样直接无法匹配,导致使用默认的私钥认证
- 例子尽量使用`example.com`,不要`xxx.com`...
  • Loading branch information
rosuH authored Nov 24, 2018
1 parent 88e7ce3 commit 2a25ff8
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions use-gitlab-github-together.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
git config --global user.name 'xirong' && git config --global user.email '[email protected]'

# 团队项目配置,每次新创建一个项目,需要执行下
git config --local user.name 'xirong.liu' && git config --local user.email 'xirong.liu@corp.xxx.com'
git config --local user.name 'xirong.liu' && git config --local user.email 'xirong.liu@corp.example.com'
```

### 2. 生成 ssh key 上传到 Github/Gitlab
Expand All @@ -63,7 +63,7 @@ ssh key 默认生成后保存在 `~/.ssh/`目录下 ,默认为 `id_rsa 和 id_

``` bash
# 生成公钥、密钥的同时指定文件名,Gitlab使用
ssh-keygen -t rsa -f ~/.ssh/id_rsa.gitlab -C "xirong.liu@corp.xxx.com"
ssh-keygen -t rsa -f ~/.ssh/id_rsa.gitlab -C "xirong.liu@corp.example.com"

# 生成默认,Github使用
ssh-keygen -t rsa -C "[email protected]"
Expand All @@ -75,12 +75,18 @@ ssh-keygen -t rsa -C "[email protected]"
`~/.ssh`目录下,如果不存在,则新建 `touch ~/.ssh/config`文件 ,文件内容添加如下:

``` bash
Host *.corp.xxx.com
Host corp.example.com
HostName git.corp.example.com
IdentityFile ~/.ssh/id_rsa.gitlab
User xirong.liu
```

配置完成后,符合 `*.corp.xxx.com`后缀的 Git 仓库,均采取` ~/.ssh/id_rsa.gitlab` 密钥进行验证,其它的采取默认的。
- `Host`参数是命令行给出的主机名,比如`ssh -T [email protected]`,那么此时的主机(`Host`)就是`corp.example.com`
- 只有`Host`匹配之后,`SSH``[email protected]`转换成`git.corp.example.com`
- `Host`不支持`*`和主机名混用,即`*.example.com`;单独`*`表示匹配所有主机,也就是默认规则
- `HostName`应该是必须填写的内容,根据你使用的命令行内容来填写

配置完成后,符合`git.corp.example.com`的 Git 仓库,均采取` ~/.ssh/id_rsa.gitlab` 密钥进行验证,其它的采取默认的。

### 4. 上传public key 到 Github/Gitlab

Expand Down Expand Up @@ -120,4 +126,4 @@ total 40
-rw------- 1 xirong staff 1679 Jun 22 11:42 id_rsa_gitcafe
-rw-r--r-- 1 xirong staff 407 Jun 22 11:42 id_rsa_gitcafe.pub
-rw-r--r-- 1 xirong staff 9139 Jul 29 15:08 known_hosts
```
```

0 comments on commit 2a25ff8

Please sign in to comment.