Location of the root's home directory in macOS

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.


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

Example:

ls -ld ~root

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.