具有多个gitlab用户帐户的多个密钥的SSH配置 [英] SSH config with multiple keys for multiple gitlab user accounts
问题描述
我在服务器上运行Gitlab CE,并使用几个不同的用户帐户按兴趣对我的存储库进行分组.问题出在SSH.
I run Gitlab CE on my server and use several different user accounts to group my repos by interest. The problem is with SSH.
我为github找到了以下解决方案:
I found the following solution for github:
https://gist.github.com/jexchan/2351996
在本指南中,仅在配置中使用具有相同主机名的不同主机.要实现我想要达到的目标,这几乎不需要付出任何努力.但是此解决方案不适用于Gitlab,或者至少不适用于我.
In this guide one just uses different hosts with the same hostname in the config. Which is little effort to achieve what I'd like to achieve. But this solution does not work with Gitlab or at least not for me.
此解决方案遍及整个网络.一个不那么常用但对我有用的东西就是这个:
This solution is all over the web. One that is less used but is working for me, is this one:
https://gist.github.com/gubatron/d96594d982c5043be6d4
在第二个中,一个在ssh配置中将子域名分配为主机,并使用相同的主机名,并在git配置中使用相同的子域. 小例子:
In the second, one assigns subdomain names as hosts in the ssh config with the same hostnames and uses the same subdomains in the git config. Little example:
SSH配置:
Host user1.git.mydomain.at
HostName git.mydomain.at
IdentityFile ~/.ssh/id_rsa_user1
Host user2.git.mydomain.at
HostName git.mydomain.at
IdentityFile ~/.ssh/id_rsa_user2
git:
git remote set-url origin admin@user1.git.mydomain.at:user1/foo.git
git remote set-url origin admin@user2.git.mydomain.at:user2/foo.git
可以看到,我必须手动更改每个回购URL.我想避免这种情况,并希望使用第一种解决方案.
One can see, that I have to change every repo url manually. I would like to avoid this, and would prefer the first solution.
我想念一些重要的东西吗?
Am I missing something important?
推荐答案
您需要两者.
我假设:
-
用户名(不是电子邮件)分别是 user1 和 user2
您已上传了足够的公共(即.pub
)密钥,其各自的私钥分别为 user1_rsa 和 user2_rsa
you have uploaded adequate public (i.e. .pub
) keys, for which the respective private keys are user1_rsa and user2_rsa
服务器网址的格式为server.com(即用 bitbucket.org , github.com 替代 server.com >等),则~/.ssh/config
应包含:
the server url is in the form server.com (i.e. substitute server.com by bitbucket.org, github.com, etc.) then ~/.ssh/config
should contain:
Host server.com-user1
HostName server.com
User git
IdentityFile ~/.ssh/user1_rsa
Host server.com-user2
HostName server.com
User git
IdentityFile ~/.ssh/user2_rsa
II..在您的git目录中,假设您添加了一个名为origin
的源,并且位于一个名为domain
的域中,并且名为一个project
的项目,那么git remote -v
应该配置为返回如下内容:
II. In your git directory, assuming that you have added an origin named origin
and are in a domain named domain
and a project named project
, then git remote -v
should be configured to return something like this:
origin git@server.com-user1:domain/project.git (fetch)
origin git@server.com-user1:domain/project.git (push)
- 首先可以通过使用
git remote add origin git@bitbucket.org-user1:domain/project.git
添加远程目录来获得此输出.关键是额外的-user1
,带有破折号,在两个文件中都匹配,并且与您的用户名匹配. - 您还可以在事实之后编辑
.git/config
并手动添加-user1
. - You could have obtained this output in the first place by adding the remote directory using
git remote add origin git@bitbucket.org-user1:domain/project.git
. The key is the extra-user1
, with a dash, matching in both files, and matching your username. - You can also edit
.git/config
after the fact and add-user1
by hand.
这篇关于具有多个gitlab用户帐户的多个密钥的SSH配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!