Wait! What?
Yes, there is a way for a user that is not even in sudo groups to run some sudo commands without password.
Imagine that for an obscure reason you need a specific user to run some of the systemctl commands for a couple of services even though those services are running under this user.
You can check the user running the process with ps aux |grep myservice
.
If you run for example:
systemctl restart myservice.service
It will ask to enter authentication and you cannot use
sudo systemctl restart myservice.service
Because “myuser” is not under sudo group.
For this case I am explaining here the solution I found in https://serverfault.com/a/1057473/517391
Edit the file /etc/sudoers
or create a new file /etc/sudoers.d/mynewfile
.
For both options create an entry like this one but adjust to your necessities:
Cmnd_Alias USER_SERVICES = /usr/bin/systemctl restart myservice1.service,/usr/bin/systemctl restart myservice2.service
myuser ALL=(ALL) NOPASSWD:USER_SERVICES
Save and close.
Then sudo or su to your user “myuser” and try to restart your services using sudo now.
Magic will happen!