Search code examples
linuxshellrootsudodotfiles

Sharing dotfiles with the root user


Current method

Actually, I'm sharing some of my dotfiles with the root user using symbolic links:

ln -s ~user/.vimrc  /root/
ln -s ~user/.zshenv /root/
ln -s ~user/.zlogin /root/
ln -s ~user/.zshrc  /root/

Former method

Before, I was using the sudo command with the -E which preserves the environment. So, the root user, when in an interactive shell, use the standard user home directory and read the corresponding dotfiles.

It works, but :

  1. Some files may be created in the standard user directory with root as owner
  2. Some commands does not allow (or warn me) using files on directory which the owner is not the current user (it's obviously for security reasons). So, executing those commands as root is problematic.

Better method ?

The simplest method is to put shared settings in the system-wide configuration files (/etc/zshrc, /etc/vimrc).

But I want to keep all the settings in my home directory, where I can keep them synchronized with a Git remote repository. This way, I can deploy them easily on a new computer.

As my current method is tedious and the former was pleasant but problematic, is there a better method to make root use my current configuration file ?


Solution

  • What I usually do is to include a deployment script in the git repository. I then invoke that script using sudo. The script then runs with root credentials and updates the dotfiles, either in the root account or globally.

    I keep the install script as light as possible with no dependencies beyond shell and the core utilities (so no rsync).