升级到 macOS 10.12 (Sierra) 后,使用 Capistrano 部署代码时出现问题,“权限被拒绝(公钥)”。

2023-12-23

所以我刚刚将我的 Mac mini(2012 年末)升级到 macOS 10.12 (Sierra),一切看起来都很好,但我在部署代码时遇到了一个奇怪的问题卡皮斯特拉诺 http://capistranorb.com/。我收到以下错误:

Permission denied (publickey).

以前在 Mac OS X 10.11 (El Capitan) 或之前的任何版本中从未遇到过此问题。为什么现在突然发生这种事?失败的 Capistrano 部署的完整输出如下:

jakes_mac:SomeCode jake$ cap staging deploy
INFO [hkdgad21] Running /usr/bin/env mkdir -p /tmp/somecode/ as [email protected] /cdn-cgi/l/email-protection
DEBUG [hkdgad21] Command: /usr/bin/env mkdir -p /tmp/somecode/
[email protected] /cdn-cgi/l/email-protection's password:
INFO [hkdgad21] Finished in 5.166 seconds with exit status 0 (successful).
DEBUG Uploading /tmp/somecode/git-ssh.sh 0.0%
INFO Uploading /tmp/somecode/git-ssh.sh 100.0%
INFO [xyz20312] Running /usr/bin/env chmod +x /tmp/somecode/git-ssh.sh as [email protected] /cdn-cgi/l/email-protection
DEBUG [xyz20312] Command: /usr/bin/env chmod +x /tmp/somecode/git-ssh.sh
INFO [xyz20312] Finished in 0.240 seconds with exit status 0 (successful).
INFO [abcdef01] Running /usr/bin/env git ls-remote --heads [email protected] /cdn-cgi/l/email-protection:SomeUser/SomeCode.git as [email protected] /cdn-cgi/l/email-protection
DEBUG [abcdef01] Command: ( GIT_ASKPASS=/bin/echo GIT_SSH=/tmp/somecode/git-ssh.sh /usr/bin/env git ls-remote --heads [email protected] /cdn-cgi/l/email-protection:SomeUser/SomeCode.git )
DEBUG [abcdef01]    Permission denied (publickey).
DEBUG [abcdef01]    fatal: Could not read from remote repository.
DEBUG [abcdef01]
DEBUG [abcdef01]    Please make sure you have the correct access rights
DEBUG [abcdef01]    and the repository exists.
(Backtrace restricted to imported tasks)
cap aborted!
SSHKit::Runner::ExecuteError: Exception while executing as [email protected] /cdn-cgi/l/email-protection: git exit status: 128
git stdout: Nothing written
git stderr: Permission denied (publickey).
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

SSHKit::Command::Failed: git exit status: 128
git stdout: Nothing written
git stderr: Permission denied (publickey).
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

Tasks: TOP => git:check
(See full trace by running task with --trace)
The deploy has failed with an error: Exception while executing as [email protected] /cdn-cgi/l/email-protection: git exit status: 128
git stdout: Nothing written
git stderr: Permission denied (publickey).
fatal: Could not read from remote repository.

请确保您拥有正确的访问权限 并且存储库存在。


似乎这是 SSH 密钥无法像 Mac OS X 10.11 (El Capitan) 中那样自动添加的问题。这是 macOS Sierra 的预期行为还是与 OpenSSH 连接的其他行为?

方法一:添加众所周知SSH 代理的密钥。

所以我找到的一个解决方案是运行ssh-add https://developer.apple.com/legacy/library/documentation/Darwin/Reference/ManPages/man1/ssh-add.1.html-A选项 - 使用钥匙串中存储的任何密码短语将所有已知身份添加到 SSH 代理 - 如下所示:

ssh-add -A

现在这可以工作,但它不会在重新启动后持续存在。因此,如果您不想再担心这个问题,只需打开您的用户~/.bash_profile像这样的文件:

nano ~/.bash_profile

并将此行添加到底部:

ssh-add -A 2>/dev/null;

现在,当您打开新的终端窗口时,一切都应该很好!

方法二:添加仅钥匙串中的 SSH 密钥给代理。

所以虽然ssh-add -A选项应该适用于大多数基本情况,我最近遇到了一个问题,我在更常见的机器上设置了 6-7 个 Vagrant 盒子(使用 SSH 密钥/身份进行访问)id_rsa.pub到位。

长话短说,由于基于 SSH 密钥/身份的尝试失败太多,我最终被锁定在远程服务器之外,因为服务器访问是基于密码的,而 SSH 密钥/身份就是 SSH 密钥/身份。所以 SSH 代理尝试了all我的 SSH 密钥失败了,我什至无法进入密码提示。

问题是ssh-add -A即使没有必要,也会任意地将您拥有的每个 SSH 密钥/身份添加到代理中;例如 Vagrant 盒子的情况。

经过多次测试后我的解决方案如下。

首先,如果您添加到代理的 SSH 密钥/身份多于您的需要 — 如所示ssh-add -l然后将它们从代理中全部清除,如下所示:

ssh-add -D

完成后,然后将 SSH 代理作为后台进程启动,如下所示:

eval "$(ssh-agent -s)"

现在,事情变得很奇怪,我不太清楚为什么。在某些情况下,您可以专门添加~/.ssh/id_rsa.pub代理的密钥/身份如下:

ssh-add ~/.ssh/id_rsa.pub

Type in your passphrase, hit Return and you should be good to go.

但在其他情况下,只需运行此命令就足以添加密钥/身份:

ssh-add -K

如果一切正常,请输入ssh-add -l您应该会看到列出一个单独的 SSH 密钥/身份。

都好?现在打开你的.bash_profile:

nano ~/.bash_profile

并将此行添加到底部;评论或删除-A版本如果你有的话:

ssh-add -K

这将允许在每次启动/重新启动时将 SSH 密钥/身份重新加载到 SSH 代理。


更新1:基于戴维达尔格的回答 https://stackoverflow.com/a/39904591/117259我发现了一个更好的全局解决方案,可以适用于系统上的所有用户。只需打开位于此处的全局 SSH 配置即可sudo:

sudo nano /etc/ssh/ssh_config

并将此行添加到文件底部:

AddKeysToAgent yes

这样做了——在移除之后.bash_profile修复,一切都很好。


更新 2:Apple 现在添加了UseKeychain打开 SSH 配置选项的选项并考虑ssh-add -A也是一个解决方案。

从 macOS Sierra 10.12.2 开始,Apple(我认为)添加了一个UseKeychainSSH 配置的配置选项。检查手册页(通过man ssh_config) 显示以下信息:

UseKeychain
        On macOS, specifies whether the system should search for
        passphrases in the user's keychain when attempting to use a par-
        ticular key. When the passphrase is provided by the user, this
        option also specifies whether the passphrase should be stored
        into the keychain once it has been verified to be correct.  The
        argument must be ``yes'' or ``no''.  The default is ``no''.

归根结底,苹果认为解决方案要么添加ssh-add -A给你的.bash_profile 正如此 Open Radar 票中所述 https://openradar.appspot.com/27348363或添加UseKeychain作为每个用户的选项之一~/.ssh/config.

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

升级到 macOS 10.12 (Sierra) 后,使用 Capistrano 部署代码时出现问题,“权限被拒绝(公钥)”。 的相关文章

随机推荐