Securing your Linux lab with resettable user accounts

The Configuration File

Each machine requires a configuration file. On my systems, I named it /etc/reboot-restore.conf, a reasonable name and place to put the file. This file specifies the users with home directories that will be restored when the system reboots.

Each line of the file has the following settings (separated by semicolons): the user name, the encrypted password, and the path to the image file (Listing 1).

Listing 1



Part of restoring the user is restoring the password, which the user could change during a session. usermod, which changes the password, only accepts a password encrypted by the crypt function. Rather than placing the passwords in the configuration file in plain text and letting the restore script do the encryption, I decided to put encrypted passwords in the configuration file for a minimum level of security. To encrypt a password, use the Perl command:

perl -e 'print crypt("password", "salt"),"\n";'

The first argument to crypt is the plaintext password. The second argument is a "salt," which comprises characters used as random input for the encryption algorithm. Only the first two letters of the salt are used, and they will be the first two letters of the encrypted password. The following lines show how I encrypted the password counting for user math and the password writing for user english with the use of arbitrary salts:

$ perl -e 'print crypt("counting","qc"), "\n"'
$ perl -e 'print crypt("writing", "gr"), "\n"'

Blank lines in the configuration are ignored; anything following a # is also ignored. Listing 1 shows the configuration file I used for testing. The directory /usr/local/reboot-restore holds the compressed image file.

The Restoration Program

The configuration file in Listing 1 is read by the program in Listing 2, which does the actual restoration.

Listing 2


The program gets the name of the configuration file from its first command-line argument (line 5) then reads the contents of the /etc/passwd file (lines 16-21). Then it reads the configuration file. For each line that is in the proper format (line 54-56), the program checks to see whether it is for a valid user (lines 59). The program then downloads the image file if it's not local (lines 64-72) and stores it in a temporary file. After that, the script calls the restore_user subroutine, passing along the user's /etc/passwd information as a parameter.

That subroutine separates the user information (lines 104-106), figures out the image file's compression type (lines 107-115) then calls commands in lines 118-123 to

  • remove the user's home directory,
  • decompress the image file (thus re-creating the home directory),
  • change the ownership of the directory and its files, and
  • restore the user's password.

The change of ownership is necessary in case the machines are not all exactly identical; if machine A has user math as user number 501 and machine B has user math as user number 502, the image's ownership flags must change to suit the machine.

If the image file was downloaded, the restore subroutine gets rid of the temporary file (lines 83-86). After all the lines in the configuration are processed, the script ends, returning 0 (no errors), if every line processed was OK, returning 1 otherwise.

This script should be placed in a reasonable system-level location on each machine that needs the restore capability. In this example, I created the directory /usr/local/reboot-restore and put the script in that directory on each machine. At this point, make sure the script permissions are set to executable.

Invoking the Script

Finally, you need to make sure the script is executed whenever the machine is rebooted. The easiest way to do this is to invoke it from the /etc/rc.d/rc.local file. This script is executed as the last step in the startup process. For this example, I added these lines to the file:

/usr/local/reboot-restore/ \

When the system starts, rc.local will invoke the script and restore the original user environment.

The Author

J. David Eisenberg teaches Computer and Information Technology classes at Evergreen Valley College in San Jose, CA. He also does feral cat trapping on campus. He lives with his two cats, Marco Polo and Big Tony.

Buy Linux Magazine

Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

  • Qt4-fsarchiver

    Qt4-fsarchiver lets you back up files, complete partitions, and create disk images with a single mouse click.

  • Mondo and Mindi

    Mondo shows that backups don’t need to be time consuming or difficult. This amazingly simple tool backs up complete hard disks or individual directories.

  • System Recovery with tar

    Use a tarball to restore your system in next to no time – without a complete re-install.

  • Partimage

    Protect your system and preserve your configuration with this powerful backup tool.

  • Partition Backup

    A partition backup offers several advantages over legacy, file-based backup alternatives, and using a backup server adds even more convenience. We’ll show you some free tools for partition backup over the network.

comments powered by Disqus

Direct Download

Read full article as PDF: