Automated Home Directory Reset: Scripting Solutions for Linux User Account Cleanup in RHEL 5


2 views

When managing shared Linux systems for training purposes, maintaining clean user environments between sessions is crucial. In this scenario with RHEL 5, we need to reliably reset 30 user home directories to a known-good state, removing all user-generated files while restoring default configurations.

The most straightforward approach combines shell scripting with RHEL's built-in tools. Here's why this works best:

  • No additional infrastructure required (unlike Puppet/Chef)
  • Complete control over the reset process
  • Easily scheduled via cron between sessions

Here's a robust implementation that handles both cleanup and restoration:

#!/bin/bash
# Define our template directory containing default files
TEMPLATE_DIR="/etc/skel_enhanced"

for USER in $(cat /opt/course/users.list); do
    HOME_DIR=$(getent passwd $USER | cut -d: -f6)
    
    # Safety checks
    if [ ! -d "$HOME_DIR" ]; then
        echo "Warning: Home directory for $USER doesn't exist"
        continue
    fi
    
    # Complete cleanup
    find "$HOME_DIR" -mindepth 1 -delete
    
    # Restore default files
    cp -r "$TEMPLATE_DIR"/. "$HOME_DIR"/
    
    # Fix permissions
    chown -R $USER:$(id -gn $USER) "$HOME_DIR"
    chmod 750 "$HOME_DIR"
done

Template Directory Setup

Enhance /etc/skel with additional training-specific defaults:

mkdir -p /etc/skel_enhanced
cp -r /etc/skel/. /etc/skel_enhanced/
cat > /etc/skel_enhanced/.bashrc_course <<'EOL'
# Course-specific aliases
alias ll='ls -lh --color=auto'
alias ..='cd ..'
EOL

Validation Steps

Add these checks to your script for reliability:

verify_user() {
    if ! id "$1" >/dev/null 2>&1; then
        echo "Error: User $1 does not exist"
        return 1
    fi
    return 0
}

validate_permissions() {
    if [ $(stat -c %a "$1") -ne 750 ]; then
        return 1
    fi
    return 0
}

For those considering configuration management tools:

  • Puppet: Overkill for 30 local users, but would work with file resources
  • Ansible: More suitable than Puppet, could use the 'file' module
  • Delphix/OS Snapshots: Heavyweight solution requiring storage

The shell script approach provides the best balance of simplicity and control for this RHEL 5 scenario. Schedule it to run via cron immediately after each session ends:

0 16 * * 1-5 /usr/local/bin/reset_homedirs.sh > /var/log/homedir_reset.log 2>&1

Remember to test thoroughly with a non-critical user account before deploying system-wide.


When managing multi-user Linux systems for training purposes, resetting home directories between sessions is a common administrative task. The primary requirements are:

  • Complete removal of all user-created files and directories
  • Restoration of default configuration files (.bashrc, .profile, etc.)
  • Permission reset to ensure proper access controls
  • Minimal downtime between user sessions

For RHEL 5 environments, I recommend a three-stage approach combining shell scripting with basic system utilities:

#!/bin/bash
# Stage 1: File cleanup
for USER in $(cat userlist.txt); do
    # Remove all contents except protected files
    find /home/$USER -mindepth 1 -maxdepth 1 \
    ! -name ".protected*" -exec rm -rf {} +
    
    # Stage 2: Template restoration
    rsync -a /etc/skel/ /home/$USER/
    
    # Stage 3: Permission reset
    chown -R $USER:$USER /home/$USER
    chmod 700 /home/$USER
done

The /etc/skel directory serves as the gold standard for default configurations. Enhance it with:

# Recommended skel contents
.bashrc        # With course-specific aliases
.profile       # Minimal PATH adjustments
.vimrc         # Basic editor settings
README.txt     # Usage instructions

For more granular control, implement differential backups with tar:

# Create baseline snapshot
tar -czvf /var/backups/user_template.tgz -C /etc/skel .

# Restoration command
tar -xzvf /var/backups/user_template.tgz -C /home/$USER

Schedule cleanups via cron for hands-off operation:

# /etc/crontab entry
0 12,18 * * * root /usr/local/bin/reset_homedirs.sh
  • Always verify paths before recursive operations
  • Run scripts with limited privileges when possible
  • Maintain backup snapshots before mass deletions
  • Implement dry-run modes for testing