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.

4
  • 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. Commented Feb 15, 2016 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. Commented Feb 15, 2016 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.... Commented Feb 15, 2016 at 14:29
  • @BradParks Well, I never knew this was a problem in the first place. Sorry. Commented Feb 15, 2016 at 14:34

2 Answers 2

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.

3
  • This is the best way to run sudo, but can the user sudo su and still preserve those environment variables? Commented Feb 16, 2016 at 1:27
  • @coteyr Why would you run sudo su instead of sudo -s or sudo bash? Commented Feb 16, 2016 at 1:29
  • Personally, habit, though a bad one. I don't feel well today, and this not a place for discussions, even if I did, so.. Like I said, this is a much better way to run sudo. Commented Feb 16, 2016 at 1:32
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.

6
  • hmm... yeah so far that seems to be the best way... is to constantly sync it up.... thanks for the suggestion! Commented Feb 15, 2016 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. Commented Feb 15, 2016 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. Commented Feb 15, 2016 at 16:11
  • Sublime Text 2 was an app that if started with root permissions would totally mess up it's user config. Commented Feb 15, 2016 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! Commented Feb 16, 2016 at 0:52

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.