【问题标题】:Sudo over SSH mixes up password tty and stdinSudo over SSH 混淆了密码 tty 和 stdin
【发布时间】:2016-03-01 14:16:35
【问题描述】:

设置:

  • 带有 SQL 脚本 script.sql (Postgres) 的本地 *nix 机器。
  • 带有 Postgres 的远程计算机 remote (Debian 7)。
    • 我可以 SSH 登录为 some_user,他是一个 sudoer。
    • 使用 Postgres 的任何操作都需要以 postgres 用户身份完成。
    • 服务器只监听localhost:5432

如何在remote 上执行script.sql 而不先将其复制到那里?

这很好用:

ssh -t some_user@remote 'sudo -u postgres psql -c "COMMANDS FOO BAR"'

-t 标志意味着sudo 将在本地终端上正确询问some_user 的密码。

还有一件事,就是能够将script.sql 传送到psql。这不起作用:

ssh -t some_user@remote 'sudo -u postgres psql' < script.sql

失败并显示消息:

Pseudo-terminal will not be allocated because stdin is not a terminal.
sudo: no tty present and no askpass program specified

编辑:简化示例

Postgres 和psql 似乎在这个问题上没有多大作用。下面的代码有同样的问题:

ssh some_user@remote xargs sudo ls < input_file

问题似乎是:我们需要发送 2 个输入到 sudo,使用 tty 的密码和 stdin 传递给 ls

编辑:更简单

ssh localhost xargs sudo ls < input_file
sudo: no tty present and no askpass program specified

添加-t 不起作用:

$ ssh -t localhost xargs sudo ls < input_file
Pseudo-terminal will not be allocated because stdin is not a terminal.
sudo: no tty present and no askpass program specified

添加另一个-t 也不起作用:

$ ssh -t -t localhost xargs sudo ls < input_file
<content of input_file>
<waiting on a prompt>

【问题讨论】:

  • 抱歉,无法通过简化示例重现您的错误。这仅在您添加 -t 开关时才会失败。不要用棍子打自己,并声称自己很痛苦。我唯一能想到的就是添加 -t -t 看看问题是否消失。
  • 添加了更简化的示例。 -t -t 不起作用。
  • 对不起,即使是最简单的例子,仍然无法重现您的问题。我是一名PG,我想我从这个角度回答了你的问题。我希望一个壳家伙在这里插话并弄清楚你的配置。
  • 非常感谢,谢谢!

标签: ssh sudo


【解决方案1】:
ssh -T some_user@remote "sudo -u postgres psql -f-" < script.sql

"-f-" 将从 STDIN 读取脚本。只需将文件重定向到那里,就可以了。

不要为 ssh 使用 -t 选项,您不需要完整的终端。

【讨论】:

  • 不起作用,返回错误sudo: no tty present and no askpass program specified
  • 如果你绝对必须使用 -t 选项(不知道为什么),那么至少在没有伪终端的情况下调用它。答案已编辑。
  • 您的答案目前结合了-t-T:“强制分配伪tty”和“禁用伪tty 分配”。
  • @vektor 好吧,这很愚蠢,只意味着-T。已编辑
  • 抱歉,还是不行。我添加了一个简化示例供大家尝试。
【解决方案2】:
ssh -T ${user}@${ip} sudo DEBIAN_FRONTEND=noninteractive postgres psql -f- < test.sql

使用 DEBIAN_FRONTEND=noninteractive 解析 no tty present 或等效的分发版。

【讨论】:

  • 试过你的方法,我仍然收到sudo: no tty present and no askpass program specified
猜你喜欢
  • 1970-01-01
  • 2016-01-12
  • 1970-01-01
  • 1970-01-01
  • 2014-05-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-09-18
相关资源
最近更新 更多