Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I want to set up a Linux machine such that when a particular user, named student, logs out, their /home directory is wiped clean and reset.

Basically I want to perform these actions when the user logs out:

userdel student
rm -r /home/student
useradd -m student
echo student | passwd --stdin student

Is there a simpler way than deleting and recreating the user?

Note: The goal here is to wipe the contents of the user's home directory and repopulate the user's home directory from /etc/skel. I guess I'm just trying to work around file ownership problems that arise when copy /etc/skel over /home/student.

Edit: What I need to do is mimic the way that useradd -m copies the stuff from /etc/skel to /home/student and changes the owner, group, and permissions. How can I do what useradd -m does without having to delete and then recreate the user?

share|improve this question
Post Locked. Please do not rollback changes made by moderators to posts. – BinaryMisfit Oct 27 '09 at 15:23
up vote 5 down vote accepted

Maybe I'm missing something, but why delete and recreate the user at all, if all you want is to clean the home directory?

Can't you just do a

rsync -a --delete /etc/skel/ /home/student/

every time a user logs out?

Maybe also kill all the user processes if any are left, but that's it.

UPDATE: To change the owner of the files, you should simply run

chown -R student:student /home/student/*

after the rsync.

I doubt the permissions have to be changed, but if so, you're going to have to do it on a file by file basis, something like

chmod +x /home/student/bin/*
share|improve this answer
This preserves the owner, group, and permissions that are present on all of the files in /etc/skel. This causes problems because root owns most of those files, and as such, when the user logs in he and his processes don't own the files. When I run useradd -m, it sets the owner, group, and permissions as needed to make logging in actually work. – eleven81 Oct 26 '09 at 16:20
See my update. Is that what you wanted? – itsadok Oct 27 '09 at 12:06
I'll have to try that chown update later today. It looks very promising! – eleven81 Oct 27 '09 at 12:55
itsadok: I got it working with the rsync line and the chown -R line (minus the last slash and asterisk) just the way I wanted. I did not have to modify any of the permissions. Thank you, so very much! – eleven81 Oct 27 '09 at 18:06
itsadok: It seems that the rsync line does not copy the hidden directories (those whose names start with a .) from /etc/skel into /home/student. Any ideas? – eleven81 Oct 27 '09 at 19:15

Tagged with "bash", so I presume you're looking at a command-line login, instead of a GUI

student@pc:~$ cat .bash_logout 
if [ "$SHLVL" = 1 ]; then
    [ -x /usr/bin/clear_console ] && /usr/bin/clear_console -q
cd ~
rm -r ~/* ~/.[a-zA-Z1-9]*
cp -r /etc/skel/* /etc/skel/.[a-zA-Z1-9]* .

First 3 lines of the above are standard(at least on my Ubuntu machine) and the rest hasn't been tested. This route has the advantage of not needing to be performed exclusively as root.

share|improve this answer
Except that it's also tagged "gdm". – Dennis Williamson Oct 26 '09 at 16:00


sudo mkdir /home/clean-homes/
sudo tar zcvf /home/clean-homes/$user.tar.gz ~user

I don't use GDM and so forget the format of the /usr/share/xsessions/*.desktop that it uses, but they're straightforward. Have yours invoke a script like

#! /bin/sh
mkdir .old
mv * .* .old
rm -rf .old &
tar zxpPf /home/clean-homes/$USER.tar.gz
exec gnome-session  # or whatever

This has the new user do all the work of deleting the old files and of recreating the contents of the home directory, on login. This isn't a solution if you want the old files to be secured from the new user: in this case you should create multiple users [why don't you?], or advise people to run your 'clean-logout' script, or - if you have people locked into logging in with only your /usr/share/xsessions/*.desktops - rewrite the above script with absolute paths and without backgrounding the rm.

Don't rely on users emptying their own directories on logout. See: ctrl+alt+backspace, pkill gnome, and a physically accessible computer's power.

share|improve this answer

Ubuntu 8.10 and later comes standard with a "Guest session" option. This switches to a guest account with all the usual programs and directories in a virgin state. Any changes the guest makes are lost when the guest logs out. At any time you can switch to the normal session by just entering your password.

share|improve this answer

You must log in to answer this question.

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