4

In our network, all user and group IDs are distributed through NIS from a central server and each user's $HOME directory is on a mounted folder which lives on that central server.

Obviously the user has a different GID from root and so root doesn't have access to the mounted $HOME folder, making it difficult (if not impossible) for the user to run something like sudo apt update as this needs access to $HOME as seen by sudo.

My question is, can I have a machine-local $HOME for the files & folders that sudo needs access to while the $HOME for my other (non-sudo) work keeps pointing to the mounted folder?

Additional information:

[user@machine ~]$ id
uid=544(user) gid=40(src) groups=40(src),101(systemd-journal)

[user@machine ~]$ sudo id
uid=0(root) gid=0(root) groups=0(root),40(src),101(systemd-journal),600(visitors),13010(hlcc),19016(identhw),51107(cadadm)

[user@machine ~]$ sh -c 'echo $HOME'
/net/server/home/user

[user@machine ~]$ sudo sh -c 'echo $HOME'
/net/server/home/user

How can I change the result of the last line (i.e. the sudo version of $HOME) point to a local folder on machine instead of the mounted network path?

Edit:

Thanks for the answers. Turns out my question was almost identical to this one on unix.stackexchange.com.

I also found out that the problem I had with apt-get update wasn't actually related to the HOME environment variable, instead it was related to the fact that I was calling sudo apt-get update from my (user's) home folder, similar to this question which can be solved by simply calling sudo apt-get update from another folder, e.g. /.

But since my question here was more about the HOME seen by sudo, I accepted techraf's answer because it doesn't require any additional switches or definitions in the sudo command line.

  • Why not just invoke sudo -i and have it use root's home on the local machine? – Stephen Dec 16 '16 at 2:05
  • 1
    Learnt a lot from your question! – techraf Dec 16 '16 at 6:10
2

You need to add the following line to sudoers Command Environment section (with sudo visudo).

Defaults        env_keep -= "HOME"

Then you can use your scripts without changes.


There is a difference between Debian and Ubuntu in a way sudo command handles by default the HOME variable (specifically this variable, not the whole environment):

  • In Debian HOME value is replaced with the root's (target user's) home directory;

  • Ubuntu preserves the calling user's setting.

This answer on Unix.SE contains more details.

6

If you invoke sudo with -i you will get root's home.

If you want sudo to inherit from the current environment you could just set home to something else.

So, either use sudo -i command or do HOME=/wherever sudo command.

For example:

sudo -i sh -c 'echo $HOME'

Which gives you:

/root

Or:

HOME=/foo sudo sh -c 'echo $HOME'

Which gives you:

/foo
  • Thanks for the response, is there a way to change the HOME variable for sudoing without having to type something extra on the command line? Your solution breaks scripts that just call sudo without the -i or the explicit HOME assignment. – FriendFX Dec 16 '16 at 4:23
  • Not really. The choice is to either inherit from the environment or the environment of the target user. You could always make a shell script that you call instead of having to type it each time, though. If you don't want to break scripts you could possibly use an alias that calls a script and sets up the environment. – Stephen Dec 16 '16 at 23:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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