Auto Reset User Account to Original State in Ubuntu 24.04

Last updated: May 13, 2024 — Leave a comment

This tutorial shows how to automatically reset a user account in Ubuntu after every reboot. All user files, extensions, personal app data, browsing history will be completely removed, so it just looks like it was when you created it.

It’s useful for computers for public use. Resetting user account, makes it always logs into Ubuntu with everything as default, without any leftover from last boot.

All personal app data, user files, and personal settings are stored in user’s home directory. So, generally delete the home directory and recreate it will do the job resetting the user account to original state.

To do it manually, you can simply run the 2 commands below one by one using another administrator account (replace USERNAME accordingly):

sudo rm -rf /home/USERNAME
sudo mkhomedir_helper USERNAME

And, the steps below will tell you how to automate the process on every boot.

NOTE: You need to perform following steps with an administrator account with sudo permission!

Step 1: Remove User Directory Automatically on Boot

For Debian, Ubuntu, Fedora, and many Linux that use systemd, there’s a systemd-tmpfiles service that can automatically creates, deletes, and cleans up volatile and temporary files and directories.

1. First, open up a terminal window by pressing Ctrl+Alt+T on keyboard in Ubuntu. When it opens, run command to create a config file and edit the Gnome default text editor:

sudo gnome-text-editor /etc/tmpfiles.d/remove_guest_home.conf

Replace gnome-text-editor with your system text editor accordingly, or use nano instead that works in most cases.

2. When file opens, add the lines below and save it (for nano, press Ctrl+S to save, and Ctrl+X to exit).

#    Path             Mode UID  GID  Age Argument
R    /home/guest       -    -    -    -    -

Replace “guest” with the username you want to reset! In case you don’t remember it, run ls /home command to list.

After that, your system will delete the “/home/guest” directory on every boot to clear the user data. Unless you disabled the systemd-tmpfiles systemd services (systemd-tmpfiles-setup.service, systemd-tmpfiles-clean.service).

Step 2: Create a script for re-creating user home directory

After that, run the command below in a terminal window to create (and edit) a script:

sudo editor /usr/local/sbin/create_guest_home.sh

Then, write the lines below into it:

#!/bin/bash
mkhomedir_helper guest

Replace guest with the username you want to recreate its home directory. Then, press Ctrl+S to save the file, and Ctrl+X to exit.

Finally, run command to make the script executable:

sudo chmod u+x /usr/local/sbin/create_guest_home.sh

Step 3: Create a Service to Auto-Run the script

After created the script, you can now create a systemd service to make the script run automatically to re-create a clean user home directory.

1. First, run command in a terminal window to create (& edit) a service file:

sudo editor /etc/systemd/system/create-guesthome.service

2. Then, write following lines into it:

[Unit]
Description=Create home directory for Guest account
After=systemd-tmpfiles-setup.service
After=systemd-tmpfiles-setup-dev.service
After=systemd-tmpfiles-clean.service

[Service]
Type=oneshot
ExecStart=/usr/local/sbin/create_guest_home.sh

[Install]
WantedBy=multi-user.target

3. Next, press Ctrl+S to save the service file and Ctrl+X to exit. Finally, run command to enable the service.

systemctl enable create-guesthome.service

Now, you can login with that user account (guest in my case), make some changes (e.g., browse web pages, create/delete files, configure desktop layout), and finally reboot computer to verify.

In addition, if you want to reset user account, but leave some files persistent in every boot, try create the corresponding files in /etc/skel directory. All the files in that directory will be automatically copied to user home directory when generated.

Undo:

To undo all the changes, simply open up a terminal window.

Then, disable the service by running command:

systemctl disable create-guesthome.service

Also, remove the service file if you want:

sudo rm /etc/systemd/system/create-guesthome.service

And, delete the systemd-tmpfiles config file that automatically clear user home directory, as well as the script to re-create a clean home:

sudo rm /etc/tmpfiles.d/remove_guest_home.conf /usr/local/sbin/create_guest_home.sh

Twitter

I'm a freelance blogger who started using Ubuntu in 2007 and wishes to share my experiences and some useful tips with Ubuntu beginners and lovers. Please comment to let me know if the tutorial is outdated! And, notify me if you find any typo/grammar/language mistakes. English is not my native language. Contact me via [email protected] Buy me a coffee: https://ko-fi.com/ubuntuhandbook1 |

No Comments

Be the first to start the conversation.

Leave a Reply

Text formatting is available via select HTML.

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> 

*