9

I'm helping to teach two Unix courses next week. Users will be granted an account on a RHEL 5 machine, during which time they'll add files to their /home folder, update their .bashrc and other dotfiles, and perform other general messiness that needs to be cleaned up.

Students in the second session will be reusing the user accounts of the folks in the first session. I'd like to automate the cleanup of their accounts, so new users can start fresh.

I'm sure I could write a shell script that runs a su -u $USER_ID and lays down an "original" set of good files in their homedir, and removes anything else it finds. Are there other tools that will help me with this clean/reset operation? I don't have any experience with Puppet, Chef, or other tools. Would they be helpful to do something like this?

To provide a scope of the problem, there's about 30 user accounts, I know all the userid/passwords, and they're all created on the same RHEL box.

matthewsteele
  • 193
  • 1
  • 1
  • 5

7 Answers7

12

There are many ways that would help:

  • remove the complete home directory and copy all files from /etc/skel back to the homedir. Change permissions afterwards.
  • put the system in a virtual machine, make a snapshot and revert to the snapshot after lesson 1
  • find something like a kiosk mode in RHEL. Ubuntu has something like that, which automatically restores the home during logoff
  • put the home on a btrfs filesystem, make a snapshot and revert after lesson 1
  • tar the home dir before lesson, delete home afterwards, restore from tar
  • ...

And learning other tools like Puppet/Chef is a little bit too much if you want results next week.

mailq
  • 17,023
  • 2
  • 37
  • 69
7

Lets say all your students had a UID between 1000 and 65000

A quick one-liner like this would work. Awk will print out an rsync command and chown command for every user in the range. Sending the output of the awk into bash will run the all the commands to reset directories and permissions.

# see what will happen.
awk 'BEGIN{FS=":"} $3 >= 1000 && $3 <=65000 { print "rsync --delete -v -r /etc/skel/ " $6 "/ ; chown -R " $1 ":" $1 " " $6;}' /etc/passwd

# actually run the commands to reset all users matched by awk.
bash <( awk 'BEGIN{FS=":"} $3 >= 1000 && $3 <=65000 { print "rsync --delete -v -r /etc/skel/ " $6 "/ ; chown -R " $1 ":" $1 " " $6;}' /etc/passwd )
Zoredache
  • 130,897
  • 41
  • 276
  • 420
2

If you're using gdm for your login manager you can add something like this file: /etc/gdm/PostSession/Default

#!/bin/sh

if [[ "$USER" != "" ]]; then
   rm -rf /home/$USER
   cp -r /etc/skel /home/$USER
   chown -R $USER:$USER /home/$USER
fi
Andrew Case
  • 3,489
  • 3
  • 23
  • 39
1

The "original" set of account files are usually located under /etc/skel in unix systems.

Apart from this, I don't know any tool to automate the cleanup. I'd probably end up writing some simple bash script.

user683887
  • 111
  • 2
1

Try this

#!/bin/bash

BASEDIR=/home

# error codes
E_OK=0
E_NOK=1

function handle_error
{

CODE=$1
ACTION=$2
INV=$3

# INV means to invert the handling logic

if [ -z $INV ]; then
  if [ $CODE -ne 0 ]; then
    echo "error: $ACTION"
    exit $E_NOK
  fi
else
 if [ $CODE -eq 0 ]; then
    echo "error: $ACTION"
    exit $E_NOK
  fi
fi

  return $E_OK
}


function print_usage()
{

  echo "usage: reset-homedir.sh USERNAME"

}
# target user and target dir
TUSER=$1
TDIR=$BASEDIR/$TUSER

if [ -z $TUSER ]; then
  print_usage
  exit 0
fi

getent passwd $TUSER >& /dev/null
RC=$?
handle_error $RC "user $TUSER does not exist"

TGROUP="`id -gn $1`"

if [ ! -d $TDIR ]; then
  echo "error: target directory $TDIR does not exist"
  exit 1
fi

# you don't want to delete user mounted stuff do you?
MOUNTS="`mount |grep $TDIR`" >& /dev/null
RC=$?
handle_error $RC "there are mounted filesystems below $TDIR" TRUE


ps -u $TUSER >& /dev/null
RC=$?
handle_error $RC "user $TUSER is logged in" TRUE

echo
echo "$TDIR will be reset to the default state - ALL DATA WILL BE LOST"
echo
echo "-- press ENTER to continue or CTRL+C to abort --"
read dummy

# we did our best to check for unwanted situations
rm -rf $TDIR
# creates a new one
cp -R /etc/skel $TDIR
chown -R $TUSER:"$TGROUP" $TDIR

echo Done.
memogh
  • 11
  • 1
0

There may be some nice tool that will help, but I would probably just (a) put the desired stuff in a git repo, and (b) write a script which iterates over the users and rm's the desired stuff and pulls from the git repo.

Kevin Beam
  • 101
  • 1
-2

This works for Ubuntu gnome: pico /usr/local/bin/cleanup.sh

rm -rf /home/user/Downloads/*

do the same if you want to remove Documents

exit chmod 511 /usr/local/bin/cleanup.sh

pico /etc/gdm/PostSession/Default

!/bin/bash

/usr/local/bin/cleanup.sh exit 0

kongwu
  • 1