There comes a time when you want to execute a command with root privileges but you don’t want to enter root password again and again. May be you want to allow a user to be able to execute one particular command but can’t share root password or some command needs to be executed on boot but requires root..sounds familiar? Keep reading.
The kind of approach we will be using is very often frowned upon. Mainly because it beats the purpose of sudo i.e. allowing only privileged users to execute certain commands. It is also looked upon as security breach, but you are fine if you know what you are doing.
You can run sudo command without password on linux by modifying /etc/sudoers file which maintains – which user can execute what. Before editing this file, lets take a backup – just in case – it gets messed up.
/etc/sudoers file can be edited in any editor of your choice but it is highly recommended to use visudo command. If there is a syntax error in sudoers file then you will be locked out of shell and we don’t want that. While visudo command checks for correct syntax and restricts user from saving a corrupt sudoers file. Enter following command to edit sudoers file:
For example, if we want user “ayush” to be able to execute, say, “visudo” without asking for password then we will have to append following line in sudoers file in the end.
ayush ALL = NOPASSWD: /usr/sbin/visudo
This needs to be added at the very last of sudoers file because if there are matching entries then last line will override all previous occurrences.
This is how my sudoers looks after editing:
Now, user “ayush” won’t be prompted to enter password while executing “sudo visudo”.
You can replace your own username and command(s) you want to execute without entering root password:
[username] ALL = NOPASSWD: [command]
This can be extended for multiple commands by comma separating them.