I was writing a script which needs a normal user (part of sudo group) to stop and start one Ubuntu Service using service command. But problem was that if I need to run service command using sudo, I had to type the password of that normal user. This was killing the automation of the script. The way was to edit the /etc/sudoers configuration file.
I am showing the steps for a normal user stgsync
Step 1: Add the user to sudo group
usermod -a -G sudo stgsync
Step2: Now edit the /etc/sudoers file using the visudo command
Step3: Add the following line
%stgsync ALL=(ALL) NOPASSWD: /usr/sbin/service
This line will allow stgsync user to run service command without entering password.
For the details of this sudoers module and writing sudoers policy statements visit this page:
Step4: Save and exit from visudo
That’s it , now the stgsync user can run service command without password:
sudo service mysql stop
will not ask for password