Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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.

share|improve this question
    
Hard-link /root to /var/root? –  Garrett Dec 12 '11 at 17:34
    
You can modify /etc/passwd manually I guess. –  billc.cn Dec 12 '11 at 17:38
add comment

1 Answer

up vote 5 down vote accepted
  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.

share|improve this answer
3  
Using vipw or sudoedit can protect against #1. As for #2, a missing home directory will usually result in a warning only. –  grawity Dec 12 '11 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 '11 at 18:54
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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