Is it possible to add users to the sudoers file through a shell script?
I’ve been looking around, still can’t find anything.
Is it possible to add users to the sudoers file through a shell script?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You could simply
echo(with elevated privileges, of course) directly to the/etc/sudoersfile:(note the tab character between the username and the first
ALL)Or, for a script:
Then save to
somefile.sh,chmod a+rxit, and runsudo ./somefile.shfrom a terminal window.To add multiple users, change the script to this;
Then, run the script like this (assuming you saved it as
addsudousers.sh):that is, space-separated.
To read the names from a file:
listofusers.txtshould also be space-separated.Edit: Jappie Kirk rightly points out that you can’t directly call
sudo echo ... >> /etc/sudoersbecause the>>redirection is handled by the shell, which has by that point dropped the superuser privileges. However, if you run a script that containsecho ... >> /etc/sudoersand the script itself has superuser privileges, everything should work just fine.