Execute Sudo Commands Without Password Prompt

Executing sudo commands always require password after the command such as :

sudo service apache2 restart

To make it run we can add this to our whitelist where we dont have to input password everytime. Follow the following steps :

sudo touch /etc/sudoers.d/custom_permissions
echo "user_name ALL = (root) NOPASSWD: /etc/apache2" >> /etc/sudoers.d/custom_permissions
# you can use vi or some other editor also
sudo chown 0440 custom_permissions

NOPASSWD tells that password should not be prompted in case of this command execution. We can also add multiple commands using

user_name ALL = (root) NOPASSWD: /etc/apache2, /etc/nginx, /path/to/command, /path/to/another/command

Now all four commmands given above are executed without any password confirmation. We can mark all sudo commands by :

user_name ALL=(ALL) NOPASSWD: ALL

Be careful while doing this. Proceed with caution. Incase of some issue contact here.

Comments