Create an exact clone of a fully configured reference user

Post your tutorials and howtos here.
User avatar
bas_otten
Posts: 212
Joined: 19 Oct 2013 12:22
Location: Netherlands

Create an exact clone of a fully configured reference user

Postby bas_otten » 01 Oct 2014 14:38

When you want to prepare a SolydXK system with several different users defined that have very similar configurations, it would be nice to have a mechanism to clone users fully and exactly. Because, there is a LOT of things you can configure: from config-files, startup-scripts, GUI stuff like System settings, KDE/desktop configuration, Application configurations and whatever you can think of. By cloning you need to set up all these things only once to provide a suitably customized baseline from which users can start working and make their own adjustments if needed. Besides cloning users within one system, it is also possible to copy a fully configured user to other systems, though the target system must be highly compatible.

For this purpose I developed a procedure that starts off with a reference user, called 'referentie'. This is the Dutch term for 'reference', and is carefully chosen such that this string is very unlikely to match anything else while doing the required string substitutions, as shown later on. The procedure targets the homedirectory of the user, which contains all the settings that we want to clone but is stuffed with hardcoded instances of '/home/referentie'. Of course the username and its UID as in /etc/passwd will remain different (otherwise it wouldn't be a 'clone', it would be the 'same' ;)) , as well as all other occurrences of user-specific settings outside its homedirectory. But fortunately, this last category is mostly empty or simply system-wide stuff that you rather require to be different (for example, autologin-user @ /etc/lightdm/lightdm.conf).

One warning beforehand: this mechanism assumes that (existing) users do not store their personal documents in their homedirectory, but in a separate 'data'-location. In my opinion, this is a best-practice anyway, since mixing OS-specific configuration and personal documents in the same directory is undesirable. For newly defined users this will not be a problem anyway.

This is how I set it up:

Step 1
Define a reference user on the system, using KUser or adduser and setting a password. I take a username 'referentie' in this example, with its own dedicated group 'referentie' and homedirectory '/home/referentie'. If you choose your own username, make sure it is a string that will never match anything unrelated to this user.

Step 2
Now log in to the system as user 'referentie'. Do all the configuration that you desire for a perfect user baseline configuration. Depending on the scope of customization and your attentions to details, this can be a lengthy process, taking several sessions to get everything just right. After you finish, be sure to clean up history-type stuff, like Recent documents and programs, Browser history, Clipboard history and such.

Step 3
Log back in to the desktop as your original install-time user, start a terminal and switch to root. Now we have a /home/referentie that we can clean, tweak and save into a tar-file. The trick is to prepare it such that we get a clean and consistent structure where 'referentie' can easily and completely be substituted with any other username while being sure to leave no remaining cross-references.

Now work your way through the following shell-commands. Please copy/paste into your favorite text-editor for readability.

Code: Select all

cd /home
tar -czf <your-safe-location>/referentie.tar referentie                       # first create backup of /home/referentie, if anything fails you can always go back!
> referentie/.bash_history                                                    # not removed, but cleaned up
> referentie/.recently-used                                                   # not removed, but cleaned up
rm referentie/.xsession-errors*                                               # xsession-errors can be cleaned
rm -rf referentie/.cache 2>/dev/null                                          # cache can safely be cleaned as well
rm referentie/.thumbnails/*/* 2>/dev/null                                     # thumbnail cache, if existent, can be cleaned
rm -rf referentie/.local/share/akonadi/db_data/* 2>/dev/null                  # large binary config cache, consumes a lot of unnecessary space, will be recreated
rm referentie/.local/share/akonadi/akonadiserver.err* 2>/dev/null             # error-files can be removed
rm referentie/.kde/share/apps/kfileplaces/bookmarks.xml.bak 2>/dev/null       # backup-files can be removed
rm referentie/.kde/share/apps/activitymanager/resources/database 2>/dev/null  # binary database, can safely be deleted

grep -rlI referentie referentie | xargs sed -i s/referentie/referentio/g      # separate binary from text files using -I grep-option, introducing another temporary nothing-else-matching string 'referentio'
grep -rl referentie referentie | xargs rm                                     # this *deletes* all binary files containing the 'referentie' username; garantees completeness of the process; so far I never had trouble doing this :D 
grep -rlI referentio referentie | xargs sed -i s/referentio/referentie/g      # restore 'referentie' username in all text files back from the temporary haven 'referentio'
#find referentie -name *bashost* | xargs rm                                   # optional, commented out, but you might want to clear out specific hostnames as well
chown -Rh referentie:referentie referentie                                    # straighten out ownerships, adjust to your local policy
chmod -R 755 referentie                                                       # straighten out permissions, adjust to your local policy
tar -czf scripts/referentie.tar referentie                                    # final step, create tar of /home/referentie, in this example at /home/scripts/referentie.tar
I love the power of the construction: grep -rl ..... | xargs sed -i ..... 8-)

Step 4
Now that we have prepared a perfect baseline referentie.tar, we can clone/distribute this towards other usernames. There are three variations to this process:
a) cloning to any number of other users,
b) cloning to overwrite your own original user (if desired),
c) and cloning to root.

a) Other users:
Stay logged in as your own original user, start a terminal and switch to root.

Code: Select all

name=ciska                                                                    # username to be cloned to, using a variable makes life easier, this name is an example
cd /home/scripts                                                              # the directory where referentie.tar was created, I use 'scripts' but pick any location you prefer
tar -xzf referentie.tar                                                       # unpack the tar to a temporary location under the same directory
cd /home                                                                      # easier to work from /home at this point
rm -rf $name                                                                  # skip if user did not exist before, otherwise be careful, make a backup first!
mv scripts/referentie $name                                                   # this moves the unpacked referentie homedir to its new target destination
chown -Rh $name:$name $name                                                   # change the ownership, adjust to your local policy, user must be defined on the system at this stage
grep -rlI referentie $name | xargs sed -i s/referentie/$name/g                # this is the magic trick, where all references to the referentie user are redirected to their new target

name=otten                                                                    # one more example username
cd /home/scripts;tar -xzf referentie.tar;cd /home;rm -rf $name;mv scripts/referentie $name;chown -Rh $name:$name $name;grep -rlI referentie $name | xargs sed -i s/referentie/$name/g
# the same batch of commands as after name=ciska; once you are comfortable with the procedure you can copy 'n paste the whole command-line at once.
b) Overwrite your own user:
Log in as user referentie, start a terminal and switch to root.
Backup your own homedirectory first: cd /home;tar -czf <your-safe-location>/<your-username>.tar <your-username>

Code: Select all

name=bas                                                                      # set your own username, in my case bas
cd /home/scripts;tar -xzf referentie.tar;cd /home;rm -rf $name;mv scripts/referentie $name;chown -Rh $name:$name $name;grep -rlI referentie $name | xargs sed -i s/referentie/$name/g
Log back in as your own user. Check your settings and make sure everything is OK.
To restore referentie to its untouched state: cd /home;rm -rf referentie;tar -xzf scripts/referentie.tar

c) Clone to root:
Cloning the referentie user to root yields two points of attention:
1. The hardcoded instances of '/home/referentie' will be substituted to '/home/root', but on a default Solyd system the homedirectory of root is /root, not /home/root. To circumvent this you can move /root physically to /home/root and create a symlink: /root -> /home/root. Do not forget to create the symlink, because there are many places in the system that assume root's homedirectory is /root.
2. Why in the first place would one do this, since it's not a good practice to log in to the desktop as root (it is not even enabled by default)? Well, there are many application that an administrative user with sudo-rights will (kde)su(do) to, and as soon as you do this configuration settings from the root homedirectory will apply. For applications that you also use as a regular user, like terminal, dolphin, maybe kate, settings are duplicated automatically by the procedure. But for applications that are typically or possibly only started as root, like softwaremanager or synaptics, settings will never be cloned from the referentie user, because they will never be run as the referentie user. The way this can be solved is to prepare the settings the way you want as root, and then copy the corresponding dot-directories (.aptitude and .synaptic are typical examples) into the referentie homedirectory before creating the referentie.tar in Step 3. This does not harm the referentie or any other user, but it allows these customizations to be available to root.

This being said the procedure is identical to before. Stay logged in as your own original user, start a terminal and switch to root.

Code: Select all

name=root
cd /home/scripts;tar -xzf referentie.tar;cd /home;rm -rf $name;mv scripts/referentie $name;chown -Rh $name:$name $name;grep -rlI referentie $name | xargs sed -i s/referentie/$name/g
> $name/.bash_history
The additional cleaning of .bash_history may be done afterwards, because there will show up a few commands in the history because of the switching to root.

Step 5
If you are aware of individual user's preferences and know their (initial) passwords, you can log in to their cloned user definitions and make a few customizations that are usually individual choices. Think of a backgroud picture or Firefox bookmarks. For a Thunderbird mail profile, you may decide to symlink that as a whole to a safe location, unaffected by user cloning and/or restoring os-filesystem images. But of course these deviations from the referentie baseline should not become too many, too fancy or too complicated, because that would lead to a contradiction of the whole idea of cloning in the first place :roll:

Now we're done! For myself, once I had developed this procedure and got comfortable using it, it saves me a lot of time whenever I create a new image of the SolydK64 system that I distribute across all family computers.

Regards, Bas.

Return to “Tutorials”

Who is online

Users browsing this forum: No registered users and 1 guest