Score:0

Scripting user account creation and group assignment

cn flag

I have to create a large number of users for a large number of Ubuntu VMs and wrote the following script as a result. This (working) bash script creates multiple sudo users from the USERS array and password field. I have been researching in other threads and across the adduser manual whether or not my process of creating a user, changing the password, and then adding the user to the sudo group is excessively verbose or not. I am aware that there are some other issues such as not validating usernames against the NAME_REGEX, and that at initialization all users will have the same password, but for my purposes this is currently not a concern. Simply put, am I doing this the most efficient way?

#! /bin/bash

USERS=(<users removed>)
password=<password removed>

if [ $EUID -ne 0 ]; then
    echo  "Please run this script with sudo or as root."
    exit 1
fi

for user in ${USERS[*],,}; do
    id -u $user &>/dev/null
    if [ $? -eq 1 ]; then
        adduser --quiet --gecos "" --disabled-password $user
        chpasswd <<< "$user:$password"
        usermod -aG sudo $user
        echo "created sudo user $user successfully..."
    else
        echo "$user already exists, skipping..."

    fi
done
cn flag
Have you seen the `newusers` **command**? Here on how to use that: https://www.tecmint.com/create-multiple-user-accounts-in-linux/ All you need is a file with `pw_name:pw_passwd:pw_uid:pw_gid:pw_gecos:pw_dir:pw_shell` on 1 line for each user.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.