以非交互方式执行 sudo 命令

Execute sudo command non-interactively

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

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: 。 我怎样才能从答案的前面剪掉那部分?

谢谢!

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

john ALL=(ALL:ALL) ALL

或者,也许更好的方法是将该行放入名为 /etc/sudoers.d/john.

的文件中

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

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