2

All Unix-like operating systems I've seen use /root as the home directory for the root account. What's the reason it was moved to /var/root in macOS? Can I change it to the usual /root?
I need to run some scripts on macOS which hardcode /root as the root's home directory. Also, I thought that /var was for variable data (spool, log files, etc.).

4
  • 1
    You are skating close to a question that asks someone to divine why Apple does a thing. Which could get your question deleted because we deal in facts and not guessing why a company does something.. If you expand your question with what you are trying to accomplish it might help. – Steve Chambers Oct 17 '18 at 16:00
  • Also why do you think it should be /root? – mmmmmm Oct 17 '18 at 16:32
  • 1
    If the scripts hardcode root what other non standard things do they do which will fail on macOS? – mmmmmm Oct 17 '18 at 17:54
  • 2
    ln -s /var/root /root # but then you still have to worry about Mark's comment. If you fully understand the script, why not edit it to work? If you don't understand it, do you really want to run it with root privilege? – WGroleau Oct 17 '18 at 18:37
4

I think the problem is that you haven't really seen that many Unix-like operating systems. Traditionally with Unix-like operating systems, you would actually have the root user's home directory at /.

If you have worked with Linux or perhaps FreeBSD, you would expect the root user's home directory to be at /root - but it's not really "God-given".

There's plenty examples of Unix-like operating systems that use varying locations. For example on HP-UX systems you wouldn't blink an eye to find the home directory at /homeroot. Similarly on macOS it is at /var/root.

Regarding your assumption on the use of the /var folder: Traditionally there hasn't been any kind of hard consensus on what each of the traditional system folders are used for (for example /etc, /usr, /usr/local, /lib, /etc.). This might vary from operating system to operating system.

Usually you would say that the /var folder is for variable data - however that doesn't mean that it is limited to spool or log files, like you write. It means that the contents of the folder might change while using your system - in contrast to for example /bin, where traditionally you could assume that this folder could just be reinstalled from the original installation medium. In this sense the root user's home folders fits the description. You usually don't want it in /home or /Users as that could be a networked file system, and you traditionally wanted the root user to be able to always be able to login for system recovery.

On macOS you can change the location of the root user's home directory. For recovery purposes, change the location in /etc/passwd. For ordinary usage, change it in the Open Directory.

You can see the current settings with:

dscl . -read Users/root | grep NFSHomeDirectory

And change them with:

dscl . -change Users/root NFSHomeDirectory <old> <new>

where and are replaced with the relevant paths.

2

A simple solution would use tilde expansion in the scripts instead of a hard coded location.

Example:

ls -ld ~root
1
  • But what else is hardcoded - you need to read every line of that script to check it especially as it runs as root so could do anything – mmmmmm Oct 18 '18 at 12:07
1

I think this a better question than it seems because it gets at some system-level facts that are bit opaque and historical.

Firstly, the reason GNU/Linux, BSD systems and some other Unix variants tend to put the root user's home directory in /root/ is to keep it out of /home/, which is often a separate mount -- potentially mounted over a network. If the /home/ mount fails, you'd want root to retain access to their home directory.

Second, although it's Linux specific, the File Hierarchy Standard (https://en.wikipedia.org/wiki/Filesystem_Hierarchy_Standard) is a fairly good explanation of what goes where and why. In GNU/Linux, BSDs as well as commercial Unix variants, /var/ is for "variable" files that are changing often. Stuff like databases, logs, sockets, etc. This, again, is to provide the ability for a separate /var/ mount, which could be configured or optimized for more volatile files.

Third, my guess about my Apple moved root's home directory to /var/ is that it coincided with a move toward more "rootless" system (SIP; https://en.wikipedia.org/wiki/System_Integrity_Protection). With a rootless system, root's home directory is significantly less important. Moving may be an indirect way of signalling a change in the use of root in macOS. And perhaps in an ideal rootless system, root's home directory is purely vestigial -- used to accommodate legacy applications that, for whatever reason, needed root to have some home directory. That Apple put in /var/ can only suggest that they though rootless uses of root's home directory were more at home in /var/ than elsewhere.

1
  • 1
    /var/root exists in Mountain Lion, and if memory serves, it's always been there on OS X. It's just tidier. – benwiggy Oct 16 '20 at 16:41

You must log in to answer this question.

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