ElementaryOS 0.4: sudo without password

This article will describe how to omit sudo password. This will enable scripts with sudo be automated.

1 Omit sudo password

Run visudo and edit /etc/sudoers. This needs sudo password yet.

$ sudo visudo
[sudo] password for hiroom2:

Add "NOPASSWD:" at sudo group.

$ sudo diff -uprN /etc/sudoers{.org,}
--- /etc/sudoers.org    2016-07-06 21:36:02.851839781 +0900
+++ /etc/sudoers        2016-07-06 21:36:17.335682680 +0900
@@ -23,7 +23,7 @@ root  ALL=(ALL:ALL) ALL
 %admin ALL=(ALL) ALL

 # Allow members of group sudo to execute any command
-%sudo  ALL=(ALL:ALL) ALL
+%sudo  ALL=(ALL:ALL) NOPASSWD:ALL

 # See sudoers(5) for more information on "#include" directives:

Then it does not need sudo password.

2 Redirect with sudo

This is not how to omit sudo password. But scripts with sudo often needs the redirect with sudo.

For below example, command can run as privilege user but the redirect cannot be executed as privilege user.

$ sudo command > /etc/file

Using "sudo su -c" enable running the redirect as privilege user.

$ sudo su -c 'command > /etc/file'

Or tee command can be excuted as privilege user.

$ sudo command | sudo tee /etc/file