0

Recently, I have just started to learn linux and I am currently trying to make a shell script that can automate the task of creating user accounts, assigning them passwords and then assigning them to groups.

I have got so far as being able to create the user account and creating a random password for them by using file descriptors, but have no idea of how I can automatically assign them to a group.

If it matters I am using the Raspberry Pi.

The code I have so far is:

exec 3< users.txt
exec 4< passwords.txt

while read iuser <&3 && read ipasswd <&4 ; do
    adduser $iuser
    echo $ipasswd | passwd --stdin $iuser
done
4
  • How do you know which group they're supposed to be a member of?
    – Sobrique
    Commented May 5, 2015 at 8:29
  • @sobrique - My Aim of this is to have a default group that all users are assigned to and then admins have the ability to change the group of the user. I have no code to do with it due to having no idea how to add it in to the code Commented May 5, 2015 at 8:31
  • 3
    I can not understand well your question but you can use adduser -G groupname $iuser
    – taliezin
    Commented May 5, 2015 at 8:40
  • How often do you need to do such a thing? Seems a lot of work to create the script and then later the input files if you need to run it once. Perhaps you are better of by creating a central user repository that is used on all the Raspberry computers that you have.
    – Marco
    Commented May 5, 2015 at 9:28

1 Answer 1

1

Based on comments - you probably want to read the manpage for adduser.

In particular :

-G, --groups GROUP1[,GROUP2,...[,GROUPN]]] A list of supplementary groups which the user is also a member of. Each group is separated from the next by a comma, with no intervening whitespace. The groups are subject to the same restrictions as the group given with the -g option. The default is for the user to belong only to the initial group.

So in your script you can:

DEFAULT_GROUP=users
exec 3< users.txt
exec 4< passwords.txt

while read iuser <&3 && read ipasswd <&4 ; do
    adduser -G $DEFAULT_GROUP $iuser
    echo $ipasswd | passwd --stdin $iuser
done

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .