Sudo has many compile-time configuration options. You can list the settings in your version with sudo -V
. One of the differences between the configuration in Debian wheezy and in Ubuntu 12.04 is that the HOME
environment variable is preserved in Ubuntu but not in Debian; both distributions erase all environment variables except for a few that are explicitly marked as safe to preserve. Thus sudo -s
preserves HOME
on Ubuntu, while on Debian HOME
is erased and sudo
then sets it to the home directory of the target user.
You can override this behavior in the sudoers
file. Run visudo
to edit the sudoers
file. There are several relevant options:
env_keep
determines which environment variables are preserved. Use Defaults env_keep += "HOME"
to retain the caller's HOME
environment variable or Defaults env_keep -= "HOME"
to erase it (and replace it by the home directory of the target user).
env_reset
determines whether environment variables are reset at all. Resetting environment variables is often necessary for rules that allow running a specific command, but does not have a direct security benefit for rules that allow running arbitrary commands anyway.
always_set_home
, if set, causes HOME
to be overridden even if it was preserved due to env_reset
being disabled or HOME
being in the env_keep
list. This option has no effect if HOME
isn't preserved anyway.
set_home
is like always_set_home
, but only applies to sudo -s
, not when calling sudo
with an explicit command.
These options can be set for a given source user, a given target user or a given command; see the sudoers
manual for details.
You can always choose to override HOME
for a given call to sudo
by passing the option -H
.
The shell will never override the value of HOME
. (It would set HOME
if it was unset, but sudo
always sets HOME
one way or another.)
If you run sudo -i
, sudo
simulates an initial login. This includes setting HOME
to the home directory of the target user and invoking a login shell.
sudo
behaves differently between Debian and Ubuntu. – msw Sep 19 '13 at 11:38sudo
goes, I think that indeed there is a difference by default. However, I don't bet on it, since I am on a box which has been setup by someone else and has been running for quite a while. In any case, for anyone interested, I found http://security.stackexchange.com/questions/18369/issues-with-preserving-home-on-sudo and https://bugs.launchpad.net/ubuntu/+source/sudo/+bug/760140. – alekosot Sep 19 '13 at 12:00