如何为 2 个不同的 gitlab 帐户设置 ssh 配置?

How to set ssh config for 2 different gitlab accounts?

我正在为 gitlab 中的 2 个不同帐户设置我的 ssh 配置。

Host gitlab.com-roulette
    HostName gitlab.com
    User norayr.ghukasian
    IdentityFile /home/norayr_ghukasyan/.ssh/id_ed25519_roulette

Host gitlab.com-devlix
    HostName gitlab.com
    User norayr.ghukasyan
    IdentityFile /home/norayr_ghukasyan/.ssh/id_ed25519_devlix

我遇到 Permission denied 错误。 对我来说奇怪的是第一个工作正常,因此我认为我的配置中存在一些我不知道的小问题。我猜想当用户或服务器尝试连接时,ssh 会自动将第一个配置与匹配的 HostName 相匹配。 我如何正确设置它以同时适用于这两个帐户?

P.S。 User 不同 - norayr.ghukasiannorayr.ghukasyan

Use difference accounts on a single GitLab instance 之后,您将像这样设置您的配置:

Host norayr.ghukasian.gitlab.com
  Hostname gitlab.com
  PreferredAuthentications publickey
  User git
  IdentityFile /home/norayr_ghukasyan/.ssh/id_ed25519_roulette

Host norayr.ghukasyan.gitlab.com
  Hostname gitlab.com
  PreferredAuthentications publickey
  User git
  IdentityFile /home/norayr_ghukasyan/.ssh/id_ed25519_devlix

然后以 norayr.ghukasian 用户身份克隆一个仓库:

git clone git@norayr.ghukasian.gitlab.com:gitlab-org/gitlab.git

使用用户名作为别名并不是绝对必要的。如果您愿意,可以使用不同的别名并将其设置在 ssh 配置的 Host 部分。

要点:

  1. 你唯一需要告诉 GitLab 你是谁的是IdentityFile
  2. 您可以通过您在 ssh 配置中为主机配置的别名指示 git/ssh 使用特定的身份文件。