可以使用不同的路径来代替 /home 吗?

Brō*_*rāx 2 home

是否可以将通常分配给/home(在 Debian 和 Ubuntu 等发行版中)的目录的功能(完全)赋予另一个目录?例如,如果您可以执行此操作并将其更改为,/xyz那么所有新的人类拥有的用户目录都将安装在/xyz(例如,Sally 的桌面路径将/xyz/Sally/Desktop代替/home/Sally/Desktop并且/home不需要存在)。

I'm working on a portable program that saves paths and loads them. If it's used on a different computer with a home directory that isn't at /home (and consequently there is no /home, but rather another path with its functionality), then I'll want it to adjust the path to have the proper home directory location in it for the new computer when the path is loaded instead.

Jde*_*eBP 8

Home directories do not need to be placed in /home and your program is erroneous if it is hardwiring any such assumptions as that all home directories share a common parent or that that parent is named /home.

/home is not even a universal convention.

/home was an idea conceived a fair while after Unix was invented. In early Unices other directories were used. This can still be seen today on operating systems like FreeBSD (and its derivatives) where /home is a symbolic link and user directories actually live under /usr/home.

Solaris likewise places "real" home directories in /export/home so that /home can be full of automatic NFS mounts and suchlike.

/home is not the parent of many common home directories.

There are plenty of home directories that don't live in /home.

  • The most obvious one is /root, the home directory for the superuser, moved from its older location at / so that root's personal and "dot" files do not clutter the root directory, but kept on the root volume so that the superuser can log in even when mounting other disc volumes is failing.
  • Various dæmon softwares have home directories in other places, for the dedicated accounts that those dæmons run as.
    • qmail's various dæmon accounts use /var/qmail for example, or /var/qmail/alias. The latter is even commonly addressed as ~alias and is designed to be a home directory, with ~alias/.qmail files as in other (real) users' home directories.
    • Various HTTP(S) and FTP(s) server softwares have (official or unofficial) conventions. For example: home directories for virtual hosts that have dedicated system accounts can be /var/www or /var/www/$VHOST.
    • Other softwares can be found on various operating systems using home directories for non-personal user accounts such as /var/unbound, /var/db/mysql, and /var/db/tor.
    • Various conventional non-personal user accounts have home directories such as /sbin, /var/adm, /var/spool/lpd, /var/spool/mail, /var/spool/news, /var/spool/uucp, and so forth.
  • On OpenBSD the system operator account has the home directory /operator and various non-personal user accounts have /var/empty as their home directories.

Home directories do not have to remain in /home.

Home directories can be moved after account creation by using the -d (--home) and -m (--move-home) options to the usermod command on Linux operating systems.

OpenBSD's usermod has the same options. (Don't do the same with the pw usermod command on FreeBSD, TrueOS/PC-BSD, et al.. The -m -d combination there has a subtly different meaning.)

Home directories do not have to be created in /home.

Even the conventional parent directory used when creating accounts can be changed, and isn't necessarily /home.

On Linux operating systems and OpenBSD the useradd command's -b (--base-dir) option specifies the parent in which home directories are created if not explicitly named with -d (--home). The default base directory is the base_dir variable in /etc/usermgmt.conf on OpenBSD, and the HOME variable in /etc/default/useradd on many Linuxes. A system administrator can change this at whim.

On FreeBSD, TrueOS/PC-BSD, et al. there's a similar -b option to the pw useradd command and a default for that modifiable via the home variable in /etc/pw.conf.

Coping with this

Your program should not hardwire any expectation at all about the locations of home directories or their parents.

  • If you want to know the currently logged-in user's home directory, use the HOME environment variable. It's set up by programs such as login, userenv or systemd when the logged-in account is switched to. If there is no HOME environment variable, it's a valid design choice to just abort, on the grounds that the login session environment variables need to be present for your program to run. Otherwise you can fall back on obtaining the process' effective/real (as appropriate) UID and querying the password database.
  • If you want to know a specific user's home directory, query the password database with the getpwnam()/getpwnam_r() or getpwuid()/getpwiud_r() library functions and pull out the pw_dir field. (Note that this field can be NULL or can point to a zero-length string.)
  • If you want to symbolically denote the home directory for a user in a way that is independent of its actual location, you can adopt the convention of a shell-like tilde expansion: ~JdeBP. Many programs do this, from vim to mailx.

Further reading