【问题标题】:Execute sudo command non-interactively以非交互方式执行 sudo 命令
【发布时间】:2020-09-11 14:38:41
【问题描述】:

我想在不交互的情况下执行以下命令:

sudo grep -e "test" /etc/sudoers

我试过以下方法:

tester@compute:~$ echo 'clouduser' | sudo -S grep -e "test" /etc/sudoers
[sudo] password for tester: test ALL=(ALL) NOPASSWD: ALL

问题是我在响应前面得到了[sudo] password for tester: 。 我怎样才能从答案的前面剪掉那部分?

谢谢!

【问题讨论】:

    标签: bash command non-interactive


    【解决方案1】:

    我会回答我的问题 - 也许其他人会需要它:

    (echo 'clouduser' | sudo -Si >/dev/null 2>&1); sudo grep -e test /etc/sudoers
    

    【讨论】:

      【解决方案2】:

      将以下行添加到您的 /etc/sudoers 文件中以打开无密码 sudo。在这种情况下,我使用john 作为登录帐户。更改为您自己的帐户 ID。

      john ALL=(ALL:ALL) ALL
      

      或者,也许更好的办法是将该行放入名为 /etc/sudoers.d/john 的文件中。

      【讨论】:

      • 感谢您的回复。我之前需要在没有任何人工干预的情况下进行该操作。我同时找到了答案并发布了答案。
      猜你喜欢
      • 2015-12-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多