Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I have a set of users I need to add to a new system as part of a BASH script that preps my server. How can I non-interactively add the user, create their home directory, set the group, and enter a default password?

For example, if I needed to add the following users, how would I go about that?

user: node
password: spoon
group: ultraMegaPermissions

user:serve
password: spoot
group: sortaNotMega

user:watcher
password: spoogle
group: notMega
share|improve this question
1  
Which operating system? On my Linux useradd (different than adduser, but form the same shadow package) handles command line parameters: useradd -p spoon -g ultraMegaPermissions -m node. –  manatwork Sep 19 '11 at 14:52
add comment

3 Answers

up vote 3 down vote accepted

It's an old question, but curious others can consider the command 'newusers'. This is present on both a RHEL5.5 system and a Ubuntu 12.04 system that I use, so I'd take a guess it will available in the repositories for most distributions.

From 'man newusers'

The newusers command reads a file of user name and clear-text password pairs and uses this information to update a group of existing users or to create new users. Each line is in the same format as the standard password file (see passwd(5)) with the exceptions explained below

share|improve this answer
1  
no, that would be easy and obviously silly =) –  James Andino Feb 2 '13 at 19:11
add comment

Use useradd on Linux, at least.

Use crypt(3) to generate an encrypted password, and then do the following for each:

useradd -m -g [group] -p [crypt output] [user]

-m creates a home directory. -g sets the user's initial login group. -p sets the encrypted password, as returned by crypt(3) (you should note that this option may be unsuitable as the encrypted password will be visible to users listing the processes when the user is being created).

You will probably want to use other options too (at a guess, at least -s), read man 8 useradd for those.

share|improve this answer
add comment

If you need manually input user password:

echo -n "Enter $MY_USER password: "
read -sa MY_PASS
echo ${MY_USER}:${MY_PASS}::::/home/${MY_USER}:${DEFAULT_SHELL} | newusers

And if you need it totally non-interactive, just run the last command with clear text password ($MY_PASS), which normally not a good idea

share|improve this answer
add comment

Your Answer

 
discard

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.