Mass Adding and Removing Users on Ubuntu

programming
unix
Author

TheCoatlessProfessor

Published

September 21, 2016

Introduction

I presently manage an online analytics platform for the Department of Statistics at the University Of Illinois at Urbana-Champaign as part of my work on autograding student responses in a statistical context.

One of the many joys is loading and unloading students at the end of each semester from the environment.

Background: Accounts via Kerbosos

When the installation was setup, I was really interested in trying to use the students pre-existing usernames (NetIDs) to authorize. To do so, there are a few hand wavy moves that were done to enable this using kerbosos. If I have time later, I’ll try to give a more details without risking system security.

Mass Adding and Removing Users

There are two different scripts that I use to add (onboard) and remove (offboard) users. Both scripts require a text file that has a header (e.g. “Usernames”) followed by a username (e.g. “NetID”) on each line.

For example:

Usernames
netid1
netid2
netid3

From here, the script automatically populates the users and deletes them.

Script to Populate Users

Below is the script that I use to add users to the environment.

#!/usr/bin/env bash

# $1 - Path to input csv
# $2 - Name of the group to add users to
function make_users {

input_file=$1  # Path CSV Input File
group=$2       # User group to assign to

# Check if a group exists, if not create it
id -g "$group" >/dev/null 2>&1 && echo "$group exists!" || groupadd $group

# Check if a file exists
[ ! -f "$input_file" ] && { echo "$input_file file not found"; exit 99; }

# Begin parsing the csv
{
    read -r # skips first line
    while IFS='' read -r netid || [[ -n "$netid" ]] # Assumes only 1 column containing usernames
    do
      echo "Trying to add $netid"
      if id "$netid" >/dev/null 2>&1; then # Does the user exist?
          echo "User: $netid already exists"
      else # User does not exist, create one.
          # Allow user to have their own group
          adduser "$netid" --disabled-password --gecos ""
          # Add user to the class    
          usermod -a -G "$group" "$netid"
      fi
      
    done
} < "$input_file"


}

make_users $1 $2

Sample call:

chmod u+x make_users.sh
./make_users.sh roster.txt course999

Script to Remove Users

Below is the script that I use to remove users from the environment. When I remove a user, I want everything that the user added gone so -r flag is added to userdel. Note, the script in its current form is highly destructive as it does not consider whether a user is shared between two classes.

#!/bin/bash

# $1 - Path to input csv
# $2 - Name of group being removed 
function remove_users {

input_file=$1  # Path CSV Input File
group=$2       # User group to assign to

# Check if group exists
id -g "$group" >/dev/null 2>&1 && echo "$group exists!" || echo "$group not found"; exit 99;

# Check if file exists
[ ! -f "$input_file" ] && { echo "$input_file file not found"; exit 99; }

# Begin parsing file
{
    read -r # skips first line
    while IFS='' read -r netid || [[ -n "$netid" ]] # Assumes only 1 column containing usernames
    do
    
      if id "$netid" >/dev/null 2>&1; then # Does the user exist?
          userdel -r "$netid"
          groupdel "$netid" # Delete the group associated with the netid
      else # User does not exist, issue command line warning!
          echo "WARNING: User $netid does not exist!"
      fi
      
    done
} < "$input_file"

# Remove group at the end...
groupdel "$group"

}

remove_users $1 $2

Sample call:

chmod u+x remove_users.sh
./remove_users.sh roster.txt course999