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"' qcnVJoZLZDRAE $ perl -e 'print crypt("writing", "gr"), "\n"' grrxaQN5BbNTM
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 reboot-restore.pl 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/reboot-restore.pl \ /etc/reboot-restore.conf
When the system starts, rc.local will invoke the script and restore the original user environment.
Infos
- Deep Freeze: http://www.faronics.com/html/deepfreeze.asp
« Previous 1 2
Buy Linux Magazine
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters
Support Our Work
Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.
News
-
Canonical Bumps LTS Support to 12 years
If you're worried that your Ubuntu LTS release won't be supported long enough to last, Canonical has a surprise for you in the form of 12 years of security coverage.
-
Fedora 40 Beta Released Soon
With the official release of Fedora 40 coming in April, it's almost time to download the beta and see what's new.
-
New Pentesting Distribution to Compete with Kali Linux
SnoopGod is now available for your testing needs
-
Juno Computers Launches Another Linux Laptop
If you're looking for a powerhouse laptop that runs Ubuntu, the Juno Computers Neptune 17 v6 should be on your radar.
-
ZorinOS 17.1 Released, Includes Improved Windows App Support
If you need or desire to run Windows applications on Linux, there's one distribution intent on making that easier for you and its new release further improves that feature.
-
Linux Market Share Surpasses 4% for the First Time
Look out Windows and macOS, Linux is on the rise and has even topped ChromeOS to become the fourth most widely used OS around the globe.
-
KDE’s Plasma 6 Officially Available
KDE’s Plasma 6.0 "Megarelease" has happened, and it's brimming with new features, polish, and performance.
-
Latest Version of Tails Unleashed
Tails 6.0 is based on Debian 12 and includes GNOME 43.
-
KDE Announces New Slimbook V with Plenty of Power and KDE’s Plasma 6
If you're a fan of KDE Plasma, you'll be thrilled to hear they've announced a new Slimbook with an AMD CPU and the latest version of KDE Plasma desktop.
-
Monthly Sponsorship Includes Early Access to elementary OS 8
If you want to get a glimpse of what's in the pipeline for elementary OS 8, just set up a monthly sponsorship to help fund its continued existence.