I would like to change the location of root's home directory from /root to /var/root. I used to be able to do this with usermod:

usermod -d /var/root root

However, with the latest version of usermod, you cannot change the directory of the currently logged in user. I am trying to change root's home directory at boot time in a system setup script that is run the first time a system is booted, so I can't really be any other user.

Is there another way to change the home directory of the currently logged in user (root in this case)?

1 Answer
1

The only problem with manually editing /etc/passwd is that there might be a race if some other user or program changes this file while you keep it open in your editor. In this case, that change will be lost when you save the file. If you're sure this problem does not apply to you (no other users logged in, etc.), then you can go ahead and edit /etc/passwd

Be sure that the root's home dir is available when booting into single-user mode, e.g. it should be on a root filesystem.

Various tricks like creating a secondary user with uid 0, switching to it, and changing root's home dir, won't work. The checks for currently logged in user are uid-based.