【问题标题】:issue with 'IdentityFile' in ssh config filessh 配置文件中的“IdentityFile”问题
【发布时间】:2022-01-28 00:27:43
【问题描述】:

当我尝试从 gitlab 克隆项目时遇到问题。我有这个错误信息:

clonage dans 'demossh'...
git@gitlab.myserver.com: Permission denied (publickey).
fatal: Impossible de lire le dépôt distant.

Veuillez vérifier que vous avez les droits d'accès
et que le dépôt existe.

我使用这个命令行进行克隆:git clone git@gitlab.myserver.com:demossh.git

但如果我改用这个命令,一切正常: GIT_SSH_COMMAND="ssh -i ~/.ssh/privateKey" git clone git@gitlab.myserver.com:demossh.git

这里是我的~/.ssh/config 文件:

host *
  # UseRoaming no
  IgnoreUnknown UseKeychain,AddKeysToAgent
  UseKeychain yes
  AddKeysToAgent yes
  ControlPath ~/.ssh/controlmasters/%C
  ControlMaster auto
  ControlPersist 600
  Preferredauthentications publickey
  AddressFamily inet
  Protocol 2
  Compression yes
  IdentitiesOnly yes


Host gitlab
  HostName gitlab.myserver.com
  User git
  port 443
  IdentityFile ~/.ssh/privateKey

我在另一台计算机上使用相同的配置和相同的公钥/私钥,它工作得很好。

有人可以帮我解决这个问题吗?我不明白如何找出问题所在。

回想一下,它是一个自托管的 gitlab 服务器。

EDIT1:回复@torek answer

ssh -Tvv git@gitlab.myserver.com 给我这个输出:

OpenSSH_7.8p1, LibreSSL 2.6.2
debug1: Reading configuration data /Users/unouss/.ssh/config
debug1: /Users/unouss/.ssh/config line 2: Applying options for *
debug1: /Users/unouss/.ssh/config line 32: Applying options for ulille
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 20: Applying options for *
debug1: /etc/ssh/ssh_config line 102: Applying options for *
debug1: auto-mux: Trying existing master
debug1: Control socket "/Users/unouss/.ssh/controlmasters/f2a4e6b6312f02723d17bf22edff8059aeacd42d" does not exist
debug1: Connecting to gitlab.myserver.com port 443.
debug1: Connection established.
debug1: identity file /Users/unouss/.ssh/privateKey type 3
debug1: identity file /Users/unouss/.ssh/privateKey-cert type -1
debug1: Local version string SSH-2.0-OpenSSH_7.8
ssh_exchange_identification: Connection closed by remote host

ssh -Tvv -i ~/.ssh/privateKey git@gitlab.myserver.com这个输出:

OpenSSH_7.8p1, LibreSSL 2.6.2
debug1: Reading configuration data /Users/unouss/.ssh/config
debug1: /Users/unouss/.ssh/config line 2: Applying options for *
debug1: /Users/unouss/.ssh/config line 32: Applying options for ulille
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 20: Applying options for *
debug1: /etc/ssh/ssh_config line 102: Applying options for *
debug1: auto-mux: Trying existing master
debug1: Control socket "/Users/unouss/.ssh/controlmasters/f2a4e6b6312f02723d17bf22edff8059aeacd42d" does not exist
debug1: Connecting to gitlab.myserver.com port 443.
debug1: Connection established.
debug1: identity file /Users/unouss/.ssh/privateKey type 3
debug1: identity file /Users/unouss/.ssh/privateKey-cert type -1
debug1: identity file /Users/unouss/.ssh/privateKey type 3
debug1: identity file /Users/unouss/.ssh/privateKey-cert type -1
debug1: Local version string SSH-2.0-OpenSSH_7.8
ssh_exchange_identification: Connection closed by remote host

这里是差异:

13a14,15
> debug1: identity file /Users/unouss/.ssh/privateKey type 3
> debug1: identity file /Users/unouss/.ssh/privateKey-cert type -1

PS:对不起,我的英语不好。

【问题讨论】:

  • 在测试时消除 Git:运行 ssh -Tvv git@gitlab.myserver.comssh -Tvv -i ~/.ssh/privateKey git@gitlab.myserver.com 并查看调试输出有什么不同。
  • 它们过于冗长,无法放入 cmets。只需比较它们,看看有什么不同 - 为什么一个有效而一个无效 - 并从那里开始研究一个无效的问题。
  • 我编辑了我的第一篇文章并添加了一些与您的答案相关的输出
  • 请注意,我的 cmets 不是答案,它们只是获取更多调试信息的说明。奇怪的是,它看起来 neither ssh 命令不起作用(尽管这可能只是我对 gitlab 缺乏了解)。此外,您的Host gitlab 不适用于ssh git@gitlab.myserver.com,而仅适用于ssh git@gitlab,并且 something 它似乎确实适用,因为两者之间的区别在于/Users/unouss/.ssh/privateKey(一个ECDSA 密钥)被加载两次,正如人们对 -i 参数所期望的那样。
  • 我们还看到 debug1: /Users/unouss/.ssh/config line 2: Applying options for *debug1: /Users/unouss/.ssh/config line 32: Applying options for ulille 这意味着您的第 2 行和第 32 行正在被应用,以及关于 /etc/ssh 的两个 cmets 在第 20 行和第 102 行为所有主机应用选项。所以这些地方很有趣,但真正的问题是这两个 ssh 是否正常工作。如果是这样,那就意味着问题是Git默认启动的ssh(没有设置GIT_SSH_COMMAND)不是你正常的ssh。

标签: git macos ssh gitlab


【解决方案1】:

您正在连接到“gitlab.myserver.com”,但在~/.ssh/config 中定义了“gitlab”条目。它没有被捡起来。这就是当您将私钥的路径指定为参数时连接成功的原因。

部分名称必须与您要连接的名称相匹配。如果你需要这两个名字,你可以使用一个列表:

- Host gitlab
+ Host gitlab gitlab.myserver.com
    HostName gitlab.myserver.com
    User git
    port 443
    IdentityFile ~/.ssh/privateKey

您确定端口 443 吗? gitlab ssh 端口是否从 gitlab 端设置为 443?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-10-30
    • 2018-01-22
    • 2016-05-28
    • 1970-01-01
    相关资源
    最近更新 更多