我怎样才能做到这一点? 我创建了一个没有主目录的用户,现在我想创建一个。不仅仅是一个名为new_user的文件夹,我想要默认的主目录以及所有的文件夹和隐藏文件等。
子目录(文档、下载等)会在用户第一次通过GNOME登录时自动创建,前提是主目录是以正确的权限创建的。这里有一个示范。
alaa@aa-lu:~$ sudo useradd testinguser alaa@aa-lu:~$ sudo passwd testinguser 输入新的UNIX密码。 重新输入新的UNIX密码。 passwd: 密码更新成功 alaa@aa-lu:~$ sudo ls -l /home 共20个 drwxr-xr-x 55 alaa alaa 4096 Aug 22 22:00 alaa drwx------ 2 root root 16384 Jun 5 09:46 lost+found alaa@aa-lu:~$ sudo mkdir /home/testinguser laa@aa-lu:~$ sudo chown testinguser:testinguser /home/testinguser laa@aa-lu:~$ ls -l /home 总数 24 drwxr-xr-x 55 alaa alaa 4096 Aug 22 22:00 alaa drwx------ 2 root root 16384 Jun 5 09:46 lost+found drwxr-xr-x 2 testinguser testinguser 4096 Aug 23 10:03 testinguser laa@aa-lu:~$ ls -l /home/testinguser/ 总数 0 alaa@aa-lu:~$
你可以通过检查/etc/passwd
中的条目来检查用户的主目录是否正确设置。默认情况下,你应该看到主目录设置为/home/testinguser
。
alaa@aa-lu:~$ grep testinguser /etc/passwd testinguser:x:1001:1001::/home/testinguser:/bin/sh
如果你没有看到主目录/home/testinguser
在那里,你需要执行sudo usermod -d /home/testinguser testinguser
命令来更新它,尽管你不应该需要使用这个命令,因为它应该是默认设置的(根据useradd
'的手册)。
然后我注销了我的账户,用testinguser
重新登录,下面是自动创建的子目录。
alaa@aa-lu:~$ ls -l /home/testinguser/ 共计 36 drwxr-xr-x 2 testinguser testinguser 4096 Aug 23 10:05 Desktop drwxr-xr-x 2 testinguser testinguser 4096 Aug 23 10:05 Documents drwxr-xr-x 2 testinguser testinguser 4096 Aug 23 10:05 下载 drwxr-xr-x 2 testinguser testinguser 4096 Aug 23 10:05 Music drwxr-xr-x 2 testinguser testinguser 4096 Aug 23 10:07 图片 drwxr-xr-x 2 testinguser testinguser 4096 Aug 23 10:05 Public drwxr-xr-x 2 testinguser testinguser 4096 Aug 23 10:05 Templates drwxr-xr-x 2 testinguser testinguser 4096 Aug 23 10:05 Video
我不需要复制/etc/skel
的内容。
如果可能的话,请你尝试按照这些步骤,创建另一个新用户。一旦你完成了,你可以通过sudo deluser testinguser && sudo rm -r /home/testinguser
删除这个新用户。
如果所有这些对你来说都不起作用,那么我猜这是一个错误。
**更新:该解决方案已被破坏,对我也不起作用。
如果你想创建用户的主目录,如果它不存在,那么运行useradd
命令,加上m
标志。这将复制/etc/skel
目录中的所有文件。
useradd -m username
你可能需要为你的系统进行配置。根据man page :
-m, --create-home
Create the user's home directory if it does not exist. The files and directories
contained in the skeleton directory (which can be defined with the -k option)
will be copied to the home directory.
By default, if this option is not specified and CREATE_HOME is not enabled, no
home directories are created.
并进一步指出:
CONFIGURATION
The following configuration variables in /etc/login.defs change the behavior
of this tool:
CREATE_HOME (boolean)
Indicate if a home directory should be created by default for new users.
如果你用adduser
命令创建了新用户,你不需要为新用户创建一个主目录。在/home
目录下建立一个以用户名字命名的主目录,其中有以下子目录。桌面、下载、文档、音乐、图片、视频(和其他),将为新用户自动创建。
如果你用useradd
命令创建了新用户,那么在终端运行以下命令。
sudo -i #enter your user password when you are asked
gedit /etc/passwd
以root身份编辑/etc/passwd
文件(这是最重要的一步)。找到写有新用户名字的那一行,为他设置默认的主目录,像这样。
new_username:x:1001:1001::/home/new_username:/bin/sh
在这一行,你也可以为新用户添加一个真实的名字,或者设置默认的shell。像这样。
new_username:x:1001:1001:Real New Username,,,:/home/new_username:/bin/bash
小心,不要做任何其他改动。
保存文件后,在从根账户出去之前,运行以下命令。
mkdir /home/new_username #to create the directory /home/new_username
cp -r /etc/skel/. /home/new_username #to copy skeleton files to /home/new_username
chown -R new_username:new_username /home/new_username #to change the owner of /home/new_username to the new user
所有这些之后,新用户的主文件夹将在第一次登录后自动填充。
另见:https://askubuntu.com/questions/152707/how-to-make-user-home-folder-after-account-creation