sudoing ssh 的问题 - `sudo ssh ...` 失败

Efr*_*ren 3 ssh sudo git key-authentication

我正在尝试使用 git salt ssh 访问(使用 root 运行)。错误总是:

权限被拒绝(公钥)。

我设法重现了这个问题,模拟了 salt 可能在做什么,通过在root用户上运行 ssh 命令,然后使用sudo(仍然在 root 帐户上)运行相同的命令,得到相同的错误。

这成功了:

root@server:/src# ssh -T git@bitbucket.org

以 XXXX 身份登录。

这失败了:

root@server:/src# sudo ssh -T git@bitbucket.org

权限被拒绝(公钥)。

权限显然是正确的:

ls -la ~/.ssh
total 32
drwx------  2 root root 4096 Jun  2 12:18 .
drwx------ 12 root root 4096 Jun  2 12:10 ..
-rw-------  1 root root  550 Jun  1 16:31 authorized_keys
-r--------  1 root root   83 Jun  2 12:18 config
-rw-------  1 root root  134 Jun  1 18:18 environment
-rw-------  1 root root 1679 May 26  2015 id_rsa
-rw-r--r--  1 root root  393 Aug  3  2014 id_rsa.pub
-rw-r--r--  1 root root 3984 Jun  2 10:19 known_hosts
Run Code Online (Sandbox Code Playgroud)

添加-v到失败的命令显示一切都很好,直到失败才出现错误:

...
debug1: Authentications that can continue: publickey
debug1: Next authentication method: publickey
debug1: Offering RSA public key: /root/.ssh/id_rsa
debug1: Server accepts key: pkalg ssh-rsa blen 279
debug1: key_parse_private2: missing begin marker
debug1: read PEM private key done: type RSA
debug1: Authentications that can continue: publickey
debug1: No more authentication methods to try.
Permission denied (publickey).
Run Code Online (Sandbox Code Playgroud)

我搜索并只找到了与权限相关的内容,但没有解释使用 root 运行时 sudo 失败。

小智 5

就像@Serge 在评论中指出的那样,这一行

debug1: Offering RSA public key: /root/.ssh/id_rsa
Run Code Online (Sandbox Code Playgroud)

在您的 ssh -v 输出中告诉您 ssh 尝试使用根目录 (/root) 而不是您自己的用户目录 (/home/yourusername) 中的公钥进行身份验证。

这给您留下了三个选择。你可以

  • 使用-i选项运行 ssh以明确指定 ssh 将使用的密钥(例如ssh -i /home/yourusername/.ssh/id_rsa ...),
  • 为 root 创建一个新的 ssh 密钥并将其添加到远程或
  • 将您自己的 .ssh 目录复制或链接到 /root

不过,您可能需要重新考虑您的设置。SSH 不需要您机器上的 root 权限,并且以 root 身份运行它也不会在远程端获得任何东西。