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
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
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 be 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
Method 2
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]
-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.
Method 3
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
Method 4
In case newuser command do not exists on the server.
You can use take the help of passwd command.
echo "new_passwd" | passwd --stdin new_user
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0