This question already has an answer here:

People usually want to preserve their environment variables while executing sudo for various reasons, but, there are some cases where discarding environment variables makes much more sense. For example, on Ubuntu, $HOME (along with a few other variables) is carried over to the process spawned as root; however, this makes no sense when trying to use Linux containers.

One could obviously edit /etc/sudoers to configure this behaviour, but I'd like to keep this behaviour and resort to discarding environment variables only for certain commands. There is a -H flag, but that is only for $HOME and doesn't really help in my case since I need to unset all variables.

How should I go about doing this?

marked as duplicate by user2064000, G-Man, Raphael Ahrens, Kusalananda, Jeff Schaller Jul 7 '16 at 9:51



You need to use the dash as in:

sudo su - me

That way you run in me's environment and not the environment you come from.

  • Oh, I went a little too fast I guess. You want to start su, but you need sudo in order to do that... (at least under Debian, Ubuntu...) – Alexis Wilke Jul 7 '16 at 13:19
