none
adding 25 (student) users to 12.04 LTS VM

    Question

  • I would like to set up 25 student accounts on a Unbuntu 12.04 LTS VM that I have provisioned via Windows Azure for teaching purposes.

    Can anyone point me toward some scripts that would automate this process and also offer some advice which command, newusers or adduser, would be preferable? I would like to set up home directories, basic passwords and the usual config/. files (from /etc/skel?). Students should have limited privileges (ie be regular users)

    If a script would be necessary, I would prefer writing the script in Python, though I can cobble together and run a bash script too.

    Thanks, while I'm quite confident as a user, the admin side for more than myself is a bit new to me. I find the command choices a bit confusing for something that would be a regular admin task.
    Saturday, October 05, 2013 6:02 PM

Answers

  • Levon9, try this Bourne shell script.

    ===========================

    #!/bin/sh

    USER_LIST=~/userlist.txt

    # Supply user information.  Format: username Firstname Lastname password
    cat > $USER_LIST << HERE
    username1 Firstname1 Lastname1 password1
    username2 Firstname2 Lastname2 password2
    HERE

    # Read one line at a time
    while read LINE

    do

            # Extract the user information from this line
            USER=$(echo $LINE | cut -d ' ' -f 1)
            NAME=$(echo $LINE | cut -d ' ' -f 2,3)
            PASSWORD=$(echo $LINE | cut -d ' ' -f 4)

            # Add the user and put him in his unique group (same name as the user as per Ubuntu default policy)
            echo
            echo "Processing user $USER ($NAME)..."
            sudo adduser --quiet --disabled-password --gecos "$NAME,,,,General user" $USER > /dev/null && echo "User $USER added"

            # Supply a password for the user
            echo -n "$PASSWORD\n$PASSWORD" | sudo passwd --quiet $USER > /dev/null && echo "Password set"

            sleep 1

    done < $USER_LIST

    echo
    rm $USER_LIST && echo "User file removed"

    ===========================

    • Marked as answer by Levon9 Saturday, October 26, 2013 12:56 PM
    Wednesday, October 09, 2013 5:00 PM

All replies

  • Hi,

    Thanks for you posting.

    I am trying to involve someone familiar with this topic to further look at this issue. There might be some time delay. Appreciate your patience.

    Thanks for your understanding and support.

    Regards.


    Vivian Wang
    TechNet Community Support

    Tuesday, October 08, 2013 5:50 AM
  • Hello and welcome to the forum.

    We don't have much information as far as scripting these users in Linux. I do know that adduser is the preferred method I have used to add users in my VM. The only documentation I had is listed here: https://help.ubuntu.com/12.04/serverguide/user-management.html#adding-deleting-users.

    I have asked Canonical if they would like to provide feedback to me about this issue. If they give me any feedback, I will pass it to you here.

    Thanks!

    Patrick [MSFT]

    Wednesday, October 09, 2013 4:41 PM
  • Levon9, try this Bourne shell script.

    ===========================

    #!/bin/sh

    USER_LIST=~/userlist.txt

    # Supply user information.  Format: username Firstname Lastname password
    cat > $USER_LIST << HERE
    username1 Firstname1 Lastname1 password1
    username2 Firstname2 Lastname2 password2
    HERE

    # Read one line at a time
    while read LINE

    do

            # Extract the user information from this line
            USER=$(echo $LINE | cut -d ' ' -f 1)
            NAME=$(echo $LINE | cut -d ' ' -f 2,3)
            PASSWORD=$(echo $LINE | cut -d ' ' -f 4)

            # Add the user and put him in his unique group (same name as the user as per Ubuntu default policy)
            echo
            echo "Processing user $USER ($NAME)..."
            sudo adduser --quiet --disabled-password --gecos "$NAME,,,,General user" $USER > /dev/null && echo "User $USER added"

            # Supply a password for the user
            echo -n "$PASSWORD\n$PASSWORD" | sudo passwd --quiet $USER > /dev/null && echo "Password set"

            sleep 1

    done < $USER_LIST

    echo
    rm $USER_LIST && echo "User file removed"

    ===========================

    • Marked as answer by Levon9 Saturday, October 26, 2013 12:56 PM
    Wednesday, October 09, 2013 5:00 PM
  • Argh .. just lost my whole reply to you when I decided to upvote your answer ..

    Anyway, what I had originally basically said thanks for the script. I sort of figured out by trial and error that adduser and delusers would work for me and put a rudimentary script together using io redirection and the read command to parse the line in my data file.

    However, the while loop and using cut to parse the lines seems neater.

    By the way, is passwd preferred over chpasswd here?

    Thanks again Peter, so many command options to add users (useradd, adduser, new user etc ...)
    Wednesday, October 09, 2013 10:23 PM
  • Thanks for the link Patrick,

    I had come across that page in my search too and have settled on using that pair of operations. I'll probably tweak the script file Peter provided and hopefully that will fit my needs.

    Wednesday, October 09, 2013 10:25 PM
  • Thanks Vivian .. there were already two helpful contributions. 

    Oddly enough, not much help on StackOverflow which is usually quite productive (I'm an active participant over there myself - though not in this specific area).

    Wednesday, October 09, 2013 10:26 PM
  • Hi Levon9,

    I wanted to take a moment to address your question:

    >By the way, is passwd preferred over chpasswd here?

    passwd vs chpasswd isn't a matter of preferred or not, but rather which is better for your situation.  Passwd is certainly the most commonly used for changing a single user password, but you might want to read the man pages listed below for more information on chpasswd (batch changes).  

    http://manpages.ubuntu.com/manpages/precise/man8/chpasswd.8.html

    http://manpages.ubuntu.com/manpages/precise/man1/passwd.1.html

    There are many ways to do things, I hope you find those links useful! 

    Regards,

    Bruce


    Thursday, October 17, 2013 8:58 PM