macos - 升级到 macOS 10.12 (Sierra) 后使用 Capistrano 部署代码时出现问题, “Permission denied (publickey).”

标签 macos ssh capistrano

所以我刚刚将我的 Mac mini(2012 年末)升级到 macOS 10.12(Sierra),一切看起来都很好,但是我在使用 Capistrano 部署代码时遇到了一个奇怪的问题.我收到以下错误:

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 jake@example.com
DEBUG [hkdgad21] Command: /usr/bin/env mkdir -p /tmp/somecode/
jake@example.com'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 jake@example.com
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 git@github.com:SomeUser/SomeCode.git as jake@example.com
DEBUG [abcdef01] Command: ( GIT_ASKPASS=/bin/echo GIT_SSH=/tmp/somecode/git-ssh.sh /usr/bin/env git ls-remote --heads git@github.com: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 jake@example.com: 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 jake@example.com: git exit status: 128
git stdout: Nothing written
git stderr: Permission denied (publickey).
fatal: Could not read from remote repository.

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

最佳答案

似乎是 SSH key 没有像以前在 Mac OS X 10.11 (El Capitan) 中那样自动添加的问题。这是来自 macOS Sierra 的预期行为还是连接到 OpenSSH 的东西?

方法 1:将所有已知 key 添加到 SSH 代理。

所以我找到的一个解决方案是运行 ssh-add -A选项 — 使用存储在钥匙串(keychain)中的任何密码短语将所有已知身份添加到 SSH 代理 — 如下所示:

ssh-add -A

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

并将这一行添加到底部:
ssh-add -A 2>/dev/null;

现在,当您打开一个新的终端窗口时,一切都会好起来的!

方法 2:仅将 key 链中的 SSH key 添加到代理。

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

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

问题是 ssh-add -A即使没有必要这样做,也会随意将您拥有的每一个 SSH key /身份添加到代理;例如在 Vagrant 盒子的情况下。

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

首先,如果您向代理添加的 SSH key /身份比您需要的多——如 ssh-add -l 所示然后像这样从代理中清除它们:
ssh-add -D

完成后,将 SSH 代理作为后台进程启动,如下所示:
eval "$(ssh-agent -s)"

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

输入你的密码,点击回车,你应该很高兴。

但在其他情况下,只需运行它就足以添加 key /身份:
ssh-add -K

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

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

并将这一行添加到底部;评论或删除 -A如果你有这个版本:
ssh-add -K

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

更新 1:基于 davidalger’s answer我发现了一个更好的全局解决方案,它适用于系统上的所有用户。只需通过 sudo 打开位于此处的全局 SSH 配置:
sudo nano /etc/ssh/ssh_config

并将这一行添加到文件的底部:
AddKeysToAgent yes

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

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

从 macOS Sierra 10.12.2 开始,Apple(我假设)添加了 UseKeychain SSH 配置的配置选项。检查手册页(通过 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''.

归结为 Apple 将解决方案视为添加 ssh-add -A给您的.bash_profile as explained in this Open Radar ticket或添加 UseKeychain作为每个用户 ~/.ssh/config 中的选项之一.

关于macos - 升级到 macOS 10.12 (Sierra) 后使用 Capistrano 部署代码时出现问题, “Permission denied (publickey).”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39715135/

相关文章:

python - 使用 Python mysql.connector 远程连接 MySQL

linux - 转发连接到另一个?

bash - 如何获取通过ssh远程创建的进程的时间?

ruby-on-rails - 如何使用 capistrano 处理一次性部署任务?

macos - 如何让音频设备 UID 传入 NSSound 的 setPlaybackDeviceIdentifier :

c++ - 在 mac 中创建 dylib 在 python 中导入

ruby-on-rails - Capistrano puma :restart not working, 但 puma:start 是

ruby-on-rails - "bundle exec thin start -C config/thin.yml"没有启动瘦

macos - 在 Mac OS X 上模拟用户

xcode - cocoa: 如何让webView背景透明?