是否可以将通常分配给/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.
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
.
/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./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./var/www
or /var/www/$VHOST
. /var/unbound
, /var/db/mysql
, and /var/db/tor
./sbin
, /var/adm
, /var/spool/lpd
, /var/spool/mail
, /var/spool/news
, /var/spool/uucp
, and so forth./operator
and various non-personal user accounts have /var/empty
as their home directories./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
.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
.
Your program should not hardwire any expectation at all about the locations of home directories or their parents.
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.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.)~JdeBP
. Many programs do this, from vim
to mailx
.userenv
. nosh toolset manual pages.