Re: User creation script

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 4/25/07, Ashley M. Kirchner <ashley@xxxxxxxxxx> wrote:

    Anyone have pointers (or examples) of a script that can be used to
create new users and set their password at the same time?  I need
something I can call like so:

    script USERNAME PASSWORD

    ...and have it return 1 or 0 for failure or success.

    This is going to be used by another application with the arguments
USERNAME and PASSWORD passed to it.  And this will also be running
through sudo (from some protected area.)


--
fedora-list mailing list
fedora-list@xxxxxxxxxx
To unsubscribe: https://www.redhat.com/mailman/listinfo/fedora-list


Here is one I did up to create a bunch of accounts when running a
class. It creates user1, user2, user3, user4, etc.. with corresponding
password being the same as the username.

for (( i=1; i<21; i++)); do /usr/sbin/useradd user$i && echo "user$i"
| passwd --stdin user$i || echo "unable to add user$i"; done

I have this in a script but it is written such that you can do it at
the command line.  You could clean it up to behave a bit more like you
want it. I have a second script as follows to remove the accounts and
their home directory when done:

for (( i=1; i<21; i++)); do /usr/sbin/userdel -r user$i; done

and of course you could change the for loop counter to suite your needs.

If you want to use a list of names from a file (each entry separated
by a space or newline) you could write the script as follows:

for i in $(cat users); do /usr/sbin/adduser $i && echo "$i" | passwd
--stdin $i;done

Where users is the text file containing the list of users you want to add.

Or you could simply supply the names on the command line as follows:

for i in john mary paul; do /usr/sbin/useradd $i && echo "$i" | passwd
--stdin $i;done

In both these examples the password is the same as the username.  If
you wanted to assign a password from a file along with the username,
then you could either incorporate the hash command from perl (not
comfortable enough to help you on that without some digging), or
create a file with usernames (called users.txt in this script) and a
second with passwords (called pass.txt i this script and it associates
first username in users.txt to first password in pass.txt, etc) and
use the following script:

declare -ar USERNAMES=($(cat users.txt))
declare -ar PASSWORDS=($(cat pass.txt))
declare -ir NUM_OF_USERS=${#usernames[@]}

for (( i=0; i<$NUM_OF_USERS; i++ ));do  /usr/sbin/useradd
${USERNAMES[$i]} && echo "${PASSWORDS[$i]}" | passwd --stdin
${USERNAMES[$i]} && echo Successfully added user ${USERNAMES[$i]} ||
echo "unable to add ${USERNAMES[$i]}"; done



There are other ways you could do this (use one file with username,
password on each line and then parse out each line).

I tried to keep it as simple as possible (both because I don't have
time to code a more complex script right now and it's not that bad an
idea to keep usernames and passwords separate - if you wanted more
security you could convert the passwords to their proper crypted
values and put that in a file and then have the script use those
values instead, possibly inserting them into the shadow file via the
script. Of course I'm assuming you would assign a default password for
each user which you would require them to change anyhow.

Hope this helps.

Jacques B


[Index of Archives]     [Current Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Yosemite News]     [Yosemite Photos]     [KDE Users]     [Fedora Tools]     [Fedora Docs]

  Powered by Linux