10

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.

2
  • Hard-link /root to /var/root?
    – Garrett
    Dec 12, 2011 at 17:34
  • You can modify /etc/passwd manually I guess.
    – billc.cn
    Dec 12, 2011 at 17:38

1 Answer 1

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.

2
  • 5
    Using vipw or sudoedit can protect against #1. As for #2, a missing home directory will usually result in a warning only. Dec 12, 2011 at 18:32
  • @grawity: Thanks for vipw info, I didn't know about it. I think using vipw is probably the best solution overall.
    – haimg
    Dec 12, 2011 at 18:54

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .