Linux: how to use sudo without entering a password?
What's in the box?
You're using Linux and need to run a command with elevated privileges via sudo without entering the password, and you don't know how? Then know that it's possible! This article explains how.
Under LinuxFor example, a user who is a member of the "sudo" group can execute commands requiring elevated privileges, without actually being the "root" superuser. However, the first time you elevate your privileges within a session, you should enter your password, for security reasons. If you wish to execute a sudo command in a script, this is inconvenient, as you will have to enter your password.
In some cases, you may need to remove this obligation so that the sudo command can be executed directly.
1. Edit /etc/sudoers file
Authorizations are configured in the " /etc/sudoers" on Debian or any other Linux distribution. This sudoers file can be edited with the command :
sudo visudo
This command is used to edit the sudoers file in text mode.
2. Add NOPASSWD rule
In the sudoers file, the NOPASSWD parameter is used to authorize the execution of a command without having to enter the password. Taking the example of the user " tutobox" this would give :
tutobox ALL=(ALL) NOPASSWD: ALL
However, this rule allows the tutobox user touse the "sudo" command to execute all commands without a password ! In the same spirit, the command below assigns this authorization to all users who are members of the " " group. IT " :
%informatique ALL=(ALL) NOPASSWD: ALL
It is advisable to refine this rule! In fact, we're going to allow the user toexecute a specific command without a password. Taking the example of the rm" command which is used to delete files and folders, gives the following rule:
tutobox ALL=(ALL) NOPASSWD: /bin/rm
Thus, the tutobox user will have to specify his password if he executes a command via "sudo", unless it calls the "rm" command corresponding to the "/bin/rm" binary. For a group, it works the same way.
If there are several commands you wish to authorize without a password, use this syntax :
tutobox ALL=(ALL) NOPASSWD: /bin/rm, /bin/php
We could go further and specify a complete command, with the path to the file to be deleted, for example. This is to show that you can restrict precisely.
3. Conclusion
Thanks to this article from the Computer Tutorials box, you will be able torun a sudo command without a password on your Linux server. Use sparingly and only when it's impossible to do otherwise.
Resources :