I would like to keep my dotfiles and other configuration files (especially in the /etc directory) in the git repository. But in order to write inside the /etc directory git must be run as root. This has an unacceptable consequence - it changes the owner and the group of files that git touches to root:root. Git also changes the owner of some of the files in the repository (e.g. index) which makes it impossible to run it next time as a normal user.

Is it possible to run a command with escalated privileges but keep the current user, not root, as the one executing this command?

  • @drewbenn Yes. But keeping dotfiles and other configuration files in separate repos is somewhat inconvenient. Especially if you have to maintain separate branches for your every machine – rubix_addict Aug 15 '19 at 17:01
  • 1
    Why are you mixing your dotfiles and /etc files? There are typically equivalent user overrides that live in ~ – D. Ben Knoble Aug 16 '19 at 1:37
  • @D.BenKnoble In practice not really. For example, I have an udev rule that fires whenever I connect the PS4 controller to my computer. It shows a notification and restores the calibration data from a file. It really is a part of what I would consider a "user config" but the rule itself must reside inside /etc. There is no ~ equivalent. Another example is bluetooth config. I want my bt devices to autoconnect. You can only do this by putting AutoEnable=true in /etc/bluetooth/main.conf and there is no dotfile equivalent in this case too. – rubix_addict Aug 16 '19 at 11:46

The common way of handling this is not exactly as you ask -- to manage configuration files in /etc using a Configuration Management tool such as Ansible.

For example, with Ansible you'd store all your playbooks in git, and deploy the configuration across your fleet of computers from that configuration. Management of the file would be run as root, but you'd be kicking off the ansible run as your normal user.

This way of managing your files in /etc (and elsewhere on the OS) has the added benefit of being a lot more reproducible (as in, you can reinstall the host and it will always look the same way). It can also manage what packages are installed, what services are running and other OS-related management.

|improve this answer|||||
  • 1
    I like to think of Ansible and co as state management: you tell it what state you want the server to be in (X installed, Y running with Z config), and it will do what's needed to get to that state. – muru Aug 16 '19 at 1:44
  • @jsbillings I understand that my question is probably an XY problem, but isn't there really a way to "execute as myself but with superuser privileges" in Linux? – rubix_addict Aug 16 '19 at 11:50
  • @rubix_addict other than something like ‘sudo’? Not really. – jsbillings Aug 16 '19 at 16:08
  • 1
    Well, if the Linux distribution in question was equipped to use Linux capabilities, then you could either already have CAP_SYS_ADMIN or have access to run a program that has that capability granted to it by a filesystem extended attribute. But not many distributions are using the capabilities mechanism. – telcoM Aug 17 '19 at 14:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.