Change location of root's home directory

10

4

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)?

I could probably sed /etc/passwd, but that seems sloppy.

Kevin S.

Posted 2011-12-12T17:10:38.980

Reputation: 675

Hard-link /root to /var/root? – Garrett – 2011-12-12T17:34:25.147

You can modify /etc/passwd manually I guess. – billc.cn – 2011-12-12T17:38:12.913

Answers

11

  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

  2. 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.

  3. 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.

haimg

Posted 2011-12-12T17:10:38.980

Reputation: 19 503

5Using vipw or sudoedit can protect against #1. As for #2, a missing home directory will usually result in a warning only. – user1686 – 2011-12-12T18:32:54.257

@grawity: Thanks for vipw info, I didn't know about it. I think using vipw is probably the best solution overall. – haimg – 2011-12-12T18:54:52.333