git配置多个ssh的几个问题

微信小程序最近一次的更新(2018.06.12)在开发者工具中新增了TGit工具栏入口,于是决定将在开发的小程序代码托管到腾讯云的TGit上。由于已经配置了github的SSH,需要为git配置多个SSH满足二者同时工作的要求。过程很简单,基本过程如下:

一 配置过程

1 生成github的ssh-key

1
$ ssh-keygen -t rsa -C "email@github.com” -f ~/.ssh/id-rsa

2 生成TGit的ssh-key

1
$ ssh-keygen -t rsa -C "email@git.cloud.tencent.com” -f ~/.ssh/id-rsa-tencent
  1. 如果不加 -f ~/.ssh/id-rsa,会弹出Enter file in which to save the key (/.ssh/id_rsa)需要用户将生产的ssh-key保存在某个文件中,默认为当前操作目录下的id_ras中,需要用户另行将产生id_ras以及id_ras.pub复制到git默认访问的位置:Mac为~/.ssh/下,Windows为C:\Users\your user name\.ssh下;
  2. 两次ssh-key使用不同名字加以区分,例如TGit存储在id_rsa_tencent中;
  3. TGit的邮箱为创建的id名,可在腾讯云下查找到;
  4. 输入以上命令后会提示Enter passphrase (empty for no passphrase):让你输入访问密码,建议此处省略,否则每次实用git都会输入密码;

3 将生成的公钥配置到git服务器上

将对应的id_rsa.pub中的信息复制到github与TGit上SSH中,如下图:

4 修改配置文件

1
2
3
4
5
6
7
8
9
10
11
# 配置github.com
Host github.com
HostName github.com
IdentityFile ~/.ssh/id_rsa
PreferredAuthentications publickey

# 配置git.cloud.tencent.com
Host git.cloud.tencent.com
HostName git.cloud.tencent.com
IdentityFile ~/.ssh/id_rsa_tencent
PreferredAuthentications publickey

5 测试是否联通

1
2
3
4
$ ssh -T git@github.com

# 输出
Welcome to GitHub, xxxxx! 即成功。

二 踩到的坑

1 TGit中https与ssh方式域名不一致

注意:
TGit中https方式与ssh方式域名不一致。

这就导致了在测试是否联通的时候报如下几个错:

  1. 测试$ ssh -t git@git.qcloud.com报错`git@git.qcloud.com: Permission denied (publickey). `即没有访问git.qcloud.com的公钥;
  2. 测试$ ssh -t git@git.qcloud.tencent.com报错ssh: Could not resolve hostname git.qcloud.tencent.com: nodename nor servname provided, or not known.即无法访问到这个域名。

2 shell request failed on channel 0 问题

在测试正确的TGit过程中,即命令$ ssh -t git@git.cloud.tencent.com,会报错shell request failed on channel 0,在网上查了一些相关的解释,以及通过调试模式得到相关log为

1
2
3
4
5
6
debug1: channel 0: new [client-session]
debug1: Entering interactive session.
debug1: pledge: network
debug1: Sending environment.
debug1: Sending env LANG = zh_CN.UTF-8
shell request failed on channel 0

ssh已经建立链接,进入交互回话,但是 channel 0 无应答。
分析相关原因为:

  1. 腾讯TGit有相关限制;
  2. 我没有将TGit的user-name与user-email 配置为全局