sudo:不存在 tty 且未指定 askpass 程序

Dre*_*kes 89 ssh remote-access

尝试在远程机器sudo上运行远程二进制文件时:

ssh remotehost "sudo ./binary"
Run Code Online (Sandbox Code Playgroud)

我看到这个错误:

sudo: no tty present and no askpass program specified
Run Code Online (Sandbox Code Playgroud)

我该如何解决这个问题?

Dre*_*kes 97

一个简单的方法是指定-t

ssh -t remotehost "sudo ./binary"
Run Code Online (Sandbox Code Playgroud)

从手册页:

强制伪 tty 分配。这可用于在远程机器上执行任意基于屏幕的程序,这非常有用,例如在实现菜单服务时。多个 -t 选项强制 tty 分配,即使 ssh 没有本地 tty。

我无法确切解释为什么会这样,并且可能有更好的方法。如果是这样,我想听听:)

@psusi 在下面的评论中解释了为什么这样做。

  • 它有效是因为 `sudo` 需要一个 tty 来提示输入密码,并且当指定要运行到 `ssh` 的命令时,它默认不分配一个,因为这通常用于运行可能传输二进制数据的非交互式命令,这可以绊倒tty。 (16认同)
  • 还有 [`-tt`](https://serverfault.com/a/625695/498092),使用 heredoc 传递命令时需要 (3认同)

eli*_*hen 36

题:

我该如何解决这个问题?

sudo: no tty present and no askpass program specified
Run Code Online (Sandbox Code Playgroud)

替代答案

作为替代方案,请尝试:

sudo -S ./[yourExecutable]

这会指示 sudo 从标准输入 stdin 读取密码。

这有帮助的场景

在 chroot 环境中,这些其他答案可能无法正常工作......也许是因为:

  1. /etc/shadow 与 /etc/passwd 冲突,不允许用户输入密码。
  2. 在 chroot-ed 环境中,访问 tty1 可能有点问题,并且 ctrl-alt f2 -- to tty2 是不可行的,因为它是非 chroot-ed 环境的 tty。

例如:使用 chroot 环境(例如 Archlinux 和 arch-chroot)手动安装/修复 linux 或引导加载程序。


ken*_*orb 6

它失败了,因为sudo正在尝试提示 root 密码并且没有分配伪 tty。

/etc/sudoers 您必须以 root 身份登录或在您的(或:)中设置以下规则sudo visudo

# Members of the admin group may gain root privileges
%admin  ALL=(ALL) NOPASSWD:ALL
Run Code Online (Sandbox Code Playgroud)

然后确保您的用户属于admin组(或wheel)。