Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I'm running OpenSuse 12.1 KDE. I was previously running Ubuntu. In Ubuntu there was a superuser option (sudo -s) that preserved the original user's HOME env var (and another option, sudo -i, that set the target user's HOME env var).

I want to have the same functionality in OpenSuse. However, by default every variation of su or sudo that I have tried sets the HOME env var to the target user's value.

Can anyone give me the precise steps for implementing one variation (I.e., an alias or something) of either su or sudo that will do the following:

  • preserve the original user's HOME env var
  • combine the original user's PATH env var with the target user's PATH env var
  • otherwise, work similarly to sudo -s in Ubuntu or su in OpenSuse.

I prefer to change as little about the default config as possible, and I would hope to come up with a script that will implement this minor change so that when I upgrade, I can restore my configuration with minimal disruption/effort.

share|improve this question
    
superuser.com/questions/232231/… - note that changes you make to these configuration files will not be overwritten with an upgrade, you will be prompted to keep, lose or merge any changes –  Paul Jan 17 '12 at 3:54
    
@Paul - yes, I know that. But experience tells me that this is not a good thing to do very often. Or, stated another way, I don't want to change files that cause me to have to merge config files during an upgrade. –  MountainX Jan 17 '12 at 7:57

1 Answer 1

up vote 1 down vote accepted

You can pass environment variables through to the sudo environment on the command line:

sudo env HOME=$HOME PATH=$PATH <command>

Because the variable substitutions happen first, the current versions of the variables are substituted on the command line, and then run with the env command in the new environment, and then <command> is executed.

This doesn't meet your second critiria for combining paths however, but you could wrap this in a script, and pass exactly the path you want into the sudo environment. System level paths don't change that much, especially given your approach of leaving as much vanilla as possible. So in your script, you could set up a new path variable and then go to sudo:

#!/bin/bash
PATH2=$PATH:/usr/sbin:/sbin
sudo env PATH=$PATH2 HOME=$HOME $@

Call this 'mysudo' perhaps and you could even alias sudo to mysudo in your bash profile so that your muscle memory for typing sudo still works.

share|improve this answer
    
Thank you. Very interesting and very helpful. How can I use that with "sudo -s" or similar variant where I want a root shell (rather than just executing a single command)? –  MountainX Jan 17 '12 at 17:33
    
You could do this using sudo env HOME=$HOME bash- so invoke a bash shell via sudo passing in the varaibles needed. –  Paul Jan 17 '12 at 22:18
    
Thanks! That works. –  MountainX Jan 18 '12 at 2:58

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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