Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm currently running a VPS on CentOS. I have an application running in the background and it will run a script under sudo to either disable or enable any account.

So what I'm trying to create is a script that creates an account, adds a password and adds the users under "etc/sudoers".

The script is being run as root so there shouldn't be any permission errors. How would I come to create this script?

share|improve this question

I've been looking for the same thing, the best I've come up with thus far is to add a unix group in the /etc/sudoers file like this:

# Members of the sftpadmin group may gain root privileges
%sftpadmin          ALL=(ALL)       ALL

And then when adding a user to the system add them to this group using useradd:

useradd ... -g sftpadmin ... newuser

Also the /etc/sudoers file includes a group for this very purpose called wheel. It's commented out but you could uncomment it and then start adding users that you want to give full access to into the wheel group. The /etc/sudoers file includes 2 examples, one that will challenge people with their password and one that won't.

## Allows people in group wheel to run all commands
# %wheel    ALL=(ALL)   ALL

## Same thing without a password
# %wheel    ALL=(ALL)   NOPASSWD: ALL

Also as an alternative take a look at this stackoverflow post: http://stackoverflow.com/questions/323957/how-do-i-edit-etc-sudoers-from-a-script. It discusses how to make a custom script that can do automatically what you'd do when running visudo and manually edit the /etc/sudoers file.

share|improve this answer
Using a group is definitely the right approach. It's not just a workaround for not having to edit sudoers, it makes maintenance easier overall. – Gilles Jun 25 '11 at 12:41

You can use the useradd command to create a user but you shouldn't really automate adding the user to the sudoers file. This file is normally edited via the visudo command. Editing it directly, especially in an automated fashion may results in errors rendering the file corrupt. Certainly not a situation you will wish to deal with.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.