3

I run zsh on Mac OSX, and sometimes want to switch in the terminal to the root user.

I'd like to keep all my dotfiles, and environment path configuration and scripts from my regular user, and just do lots of stuff as root.

I've been running sudo bash or sudo zsh as my easy way to switch to root, but this has the disadvantage of not running all my zsh config I have for my regular user.

What's the best way to "become root" for running a series of commands, and keep all the config/environment setup I have for my normal user?"

For example, I have customized the following for my normal user, and would like to have them all available to the root user, as well:

  • .zshrc
  • ~/bin
  • .aliases
  • vim mode for input

EDIT: Note that I am aware that I shouldn't "run as root" a lot. And I don't. But I do have to constantly run a few simple commands as root at times, and I open a tmux split just for this usage, and no other.

Brad Parks
  • 1,669
  • Is setting softlinks in your root's home folder an option? if you just link e.g. /root/.zshrc -> /your/user's/home/.zshrc, you'd always be up to the latest changes. Alternatively change root's home dir to your user's. – FelixJN Feb 15 '16 at 14:04
  • When you sudo bash or sudo zsh your calling a shell. That new shell will override some of the old shells "stuff", but you can check the man pages to see if there is a way to limit what variables it overrides. – coteyr Feb 15 '16 at 14:17
  • @Fiximan - I did try softlinks, but started to get permissions problems that made me think twice... I never thought of the change root's home dir though... hmmm.... – Brad Parks Feb 15 '16 at 14:29
  • @BradParks Well, I never knew this was a problem in the first place. Sorry. – FelixJN Feb 15 '16 at 14:34

2 Answers2

3

You almost certainly do not want to do this.

If your root user was pointed at your normal users dot files then the permissions on those dot files may change, thus complicating things for your normal user.

What you could do instead is copy the files over to your root users home directory on every login.

Somewhere in your ~/.zshenv (for root) copy over the files and adjust their permissions. That way you keep your environment sane, and get to use your customization.

Personally if I want to run several root commands I sudo su - but you could try sudo -E su -p to preserve the user's environment.

Obligatory warning!

There is never a good reason to run sudo su. It's far better to run sudo before every command. That said, I do sudo su sometimes. But the general rule is don't sudo su.

coteyr
  • 4,310
  • hmm... yeah so far that seems to be the best way... is to constantly sync it up.... thanks for the suggestion! – Brad Parks Feb 15 '16 at 14:32
  • Could you go into detail from where the permission conflicts arise? OP seems to have experienced them, but I do not understand why. – FelixJN Feb 15 '16 at 14:36
  • For example nano will re-create it's settings file, which is then only usable by root. Many apps will "edit" or "create" a config file. It's all up to the app. Many will write settings then "fix" permissions on that file. – coteyr Feb 15 '16 at 16:11
  • Sublime Text 2 was an app that if started with root permissions would totally mess up it's user config. – coteyr Feb 15 '16 at 16:11
  • Only a few programs are badly behaved in this respect. Systematically copying dot files is a very bad idea; you may overwrite modifications without realizing it, and of course it completely breaks down if there are multiple sudoers on the machine. Copying dot files in .zshenv is an especially bad idea: this runs on every script! – Gilles 'SO- stop being evil' Feb 16 '16 at 00:52
  • It's not behaving badly to write a file with owner and group set to the current user and group. You will overwrite modifications, but that's what he wants, and .zshenv may not be the best place, but it ensures it's always there. What is a better place? – coteyr Feb 16 '16 at 01:26
3

To get your usual dot files, make sure that sudo doesn't change the HOME environment variable. Whether this happens depends on the compile-time and run-time configuration of sudo. To preserve HOME, edit the sudoers file (run visudo, never edit this file directly!) and make sure that it contains

Defaults !always_set_home, !set_home
Defaults env_keep+=HOME

(Some of these may be unnecessary depending on compilation options.) And of course don't run sudo -i.

Do avoid doing too much as root though. For example, instead of running an editor as root, use sudoedit.