Execute sudo command non-interactively

1.9k Views Asked by At

I would like to execute the following command without interaction:

sudo grep -e "test" /etc/sudoers

I have tried the following method:

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

The problem is that I am getting the [sudo] password for tester: in front of the response. How I can cut that part from the front of the answer?

Thanks!

2

There are 2 best solutions below

0
On BEST ANSWER

I will answer to my question - maybe someone else will need it:

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

Add the following line to your /etc/sudoers file in order to turn on password-less sudo. In this case, I use john as the login account. Change to your own account id.

john ALL=(ALL:ALL) ALL

Alternatively, and perhaps better is to put that line into a file called /etc/sudoers.d/john.