Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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.

share|improve this question
    
If you teach it you'll probably know it already, but Linux is not Unix. The name comes from Linus, but it's also an acronym for [L]inux [i]s [n]ot [U]ni[x]. –  mailq Sep 10 '11 at 16:05
    
True enough. I've changed the title to reflect it. –  matthewsteele Sep 14 '11 at 16:10
add comment

6 Answers

up vote 11 down vote accepted

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.

share|improve this answer
    
Thanks. I'll probably run a tar and restore, and try learning a tool like Puppet when I'm not under a time crunch. –  matthewsteele Sep 8 '11 at 13:16
add comment

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 )
share|improve this answer
add comment

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.

share|improve this answer
add comment

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

#!/bin/sh

rm -rf /home/$USER
cp -r /etc/skel /home/$USER
chown -R $USER:$USER /home/$USER
share|improve this answer
add comment

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.

share|improve this answer
add comment

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.
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.