This article will describe how to omit sudo password. This will enable scripts with sudo be automated.
1 Install sudo package
If you install Linux with setting root password, sudo package is not installed.
Switch to root user with root password, install sudo package and add your user to sudo group.
# apt install -y sudo # gpasswd -a hiroom2 sudo # Change hiroom2 to username # reboot
After reboot, switch to your user.
2 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.orig /etc/sudoers --- /etc/sudoers.orig 2017-06-19 00:41:29.421047745 +0900 +++ /etc/sudoers 2017-06-19 00:41:41.481044752 +0900 @@ -20,7 +20,7 @@ Defaults secure_path="/usr/local/sbin:/u root ALL=(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.
3 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