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.

2
  • 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, 2011 at 16:05
  • True enough. I've changed the title to reflect it. Sep 14, 2011 at 16:10

8 Answers 8

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.

1
  • Thanks. I'll probably run a tar and restore, and try learning a tool like Puppet when I'm not under a time crunch. Sep 8, 2011 at 13:16
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 )
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
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.

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.
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.

0

It's and old question, but there's one rather straightforward answer that I don't see mentioned in comments and answers here, so adding it now: you can also simply delete and re-add the user.

There are a few simple methods for that:

  • You can use a shutdown script to always delete the user account, and a startup script to always delete (in case shutdown was brutal) and re-add the user account.
  • You can use crontab.

This way, you have a guest user account that should always be fresh on startup.

You can also achieve the same on logout (but in that case you'd do the delete + readd on logout in one step, so the account can be seen and used in the account selector, if you use one).

Note that this isn't necessarily a 100% full-proof approach, but it's straightforward and works fine. I've set that up in a number of school labs a few times, where they didn't have IT staff and a proper orchestration solution. This allows a rather safe setup for a "student" account and without sharing admin privileges.

New contributor
haylem is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
-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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .