1

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.

JdeBP
  • 27,556
  • 1
  • 77
  • 106
MountainX
  • 2,214

1 Answers1

1

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.

Paul
  • 61,193