如何在Bitbucket和Git中设置多个SSH配置

huangapple go评论68阅读模式
英文:

How to setup múltiple SSH configurations with Bitbucket and Git

问题

我正在使用两个不同的Bitbucket帐户来处理不同的项目。我的SSH配置文件如下:

# 帐户1
Host bitbucket.org
    User user1
    IdentityFile ~/.ssh/id_rsa_1

# 帐户2
Host bitbucket.org
    User user2
    IdentityFile ~/.ssh/id_rsa_2

如果我对帐户2执行git push(或pull或其他操作),我会收到错误消息,因为它尝试使用帐户1的配置。

我还尝试过以下配置:

# 帐户1
Host account1
    HostName bitbucket.org
    User user1
    IdentityFile ~/.ssh/id_rsa_1

# 帐户2
Host account2
    HostName bitbucket.org
    User user2
    IdentityFile ~/.ssh/id_rsa_2

但我仍然收到相同的错误。如何配置它,以便它会尝试所有配置直到找到正确的配置呢?

英文:

I'm working with different projects using two different Bitbucket accounts. My SSH config file looks like this:

# account 1
Host bitbucket.org
    User user1
    IdentityFile ~/.ssh/id_rsa_1

# account 2
Host bitbucket.org
    User user2
    IdentityFile ~/.ssh/id_rsa_2

If I git push (or pull or whatever) to/from account 2, I get an error because it tries to connect using account 1 config.

I also tried this:

# account 1
Host account1
    HostName bitbucket.org
    User user1
    IdentityFile ~/.ssh/id_rsa_1

# account 2
Host account2
    HostName bitbucket.org
    User user2
    IdentityFile ~/.ssh/id_rsa_2

But I get the same error. How can I configure it so it tries with all the configurations until find the right one?

答案1

得分: 0

使用SSH配置可能无法达到您的期望,当连接到同一主机时。最好的方法是完全绕过它。更好的方法是简单地告诉Git您希望如何连接,然后让它使用GIT_SSH_COMMAND环境变量或core.sshCommand配置设置来为您的存储库提供SSH命令行。完整示例可以在此答案中找到。

https://superuser.com/a/912281/98717

您还应该仔细检查您的SSH设置。通常,git存储库由git用户访问。

以下是与此答案链接的答案文本,截止到本答案发布时的内容。原用户获得信用。

环境变量 GIT_SSH_COMMAND

从Git版本2.3.0开始,您可以使用环境变量GIT_SSH_COMMAND,如下所示:

GIT_SSH_COMMAND="ssh -i ~/.ssh/id_rsa_example" git clone example

请注意,-i有时可能会被您的配置文件覆盖,如果是这样,您应该给SSH提供一个空的配置文件,如下所示:

GIT_SSH_COMMAND="ssh -i ~/.ssh/id_rsa_example -F /dev/null" git clone git@github.com:example/example.git

配置 core.sshCommand

自Git版本2.10.0开始,您可以为每个存储库或全局配置此设置,使用core.sshCommand设置。不再需要使用环境变量。以下是如何同时克隆存储库并设置此配置的方法:

git clone -c "core.sshCommand=ssh -i ~/.ssh/id_rsa_example -F /dev/null" git@github.com:example/example.git
cd example/
git pull
git push

如果存储库已经存在,请运行:

git config core.sshCommand "ssh -i ~/.ssh/id_rsa_example -F /dev/null"

配置保存在.git/config中。

英文:

Using SSH config may not accomplish what you expect it to when connecting to the same host. You're better off bypassing that altogether. The better approach is to simply tell Git how you wish to connect and let it handle everything for you using the GIT_SSH_COMMAND environment variable or the core.sshCommand config setting to supply the SSH command line for your repo to use. Full examples are found in this answer.

https://superuser.com/a/912281/98717

You should also double check your SSH settings. Generally, git repos are accessed by the git user.

Here's the text of the answer linked above as it exists at the time of this answer. Credit to the original user.

Environment variable GIT_SSH_COMMAND

From Git version 2.3.0, you can use the environment variable GIT_SSH_COMMAND like this:

GIT_SSH_COMMAND="ssh -i ~/.ssh/id_rsa_example" git clone example

Note that -i can sometimes be overridden by your config file, in which case, you should give SSH an empty config file, like this:

GIT_SSH_COMMAND="ssh -i ~/.ssh/id_rsa_example -F /dev/null" git clone git@github.com:example/example.git

Configuration core.sshCommand

Since Git version 2.10.0, you can configure this per repo or globally, using the core.sshCommand setting. There's no more need to use the environment variable. Here's how you clone a repo and set this configuration at the same time:

git clone -c "core.sshCommand=ssh -i ~/.ssh/id_rsa_example -F /dev/null" git@github.com:example/example.git
cd example/
git pull
git push

If the repo already exists, run:

git config core.sshCommand "ssh -i ~/.ssh/id_rsa_example -F /dev/null"

The configuration is saved in .git/config

答案2

得分: 0

以下是翻译好的部分:

第二种 ~/.ssh/config 方法 - 即你拥有 Host account1Host account2 的方法 - 通常在设置好之后最容易使用,但我在你的帖子中没有看到关于实际 使用 这些新名称的内容。(请注意,你的第一种方法不会起作用:因为 Host 值相同,SSH 客户端无法确定在特定情况下应该使用哪一个。)

对于已经存在的存储库,git remote set-url origin account1:owner/repogit remote set-url origin account2:owner/repo 将更新远程仓库以使用 ~/.ssh/config 中定义的参数。

对于尚未克隆的存储库,而不是使用 git clone git@bitbucket.org:owner/repo,请使用 git clone account1:owner/repogit clone account2:owner/repo

一旦你已经根据需要设置了远程 URL,然后 git fetchgit pullgit push 和任何其他网络操作都将使用你在 ~/.ssh/config 中定义的参数。你不需要更改命令,如果你需要更改一个帐户的设置(例如定义新的密钥或端口),那么你可以在 ~/.ssh/config 中为所有受影响的存储库进行一次更改。

英文:

The second ~/.ssh/config approach - the one where you have Host account1 and Host account2 - is usually easiest to work with once it's set up, but I didn't see anything in your post about actually using the new names in those repos. (Note that your first approach won't work: since the Host values are identical, it's impossible for the SSH client to know which one it should use in a given situation.)

For repos you already have, git remote set-url origin account1:owner/repo or git remote set-url origin account2:owner/repo will update the remotes to use the parameters defined in ~/.ssh/config.

For repos you haven't cloned yet, instead of git clone git@bitbucket.org:owner/repo, use either git clone account1:owner/repo or git clone account2:owner/repo.

Once you've set the remote URLs as appropriate, then git fetch, git pull, git push, and any other network operations will use the parameters you've defined in ~/.ssh/config. You won't have to change the commands, and if you need to change anything about the setup for one account (such as defining a new key or port) then you can do it once, for all affected repos, in ~/.ssh/config.

huangapple
  • 本文由 发表于 2023年7月6日 16:21:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/76626858.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定