我希望能够使用不同的设置登录,但仍然是同一个“用户”。因此,我添加了第二个用户,该用户具有相同的 UID/GUID,但主目录不同,现在启动 Ubuntu 时系统就挂起了。如果我从passwd
和shadow
文件中删除第二个用户,系统就可以正常启动。
答案1
/etc/passwd
手动编辑和文件通常不是一个好主意/etc/shadow
。我建议使用系统提供的工具。除此之外,虽然您可以让一个用户与另一个用户共享相同的 GUID,但每个用户的 UID 必须是唯一的。因此,启动很可能会失败,要么是 Linux 阻止启动必要的应用程序,要么是期望 UID 唯一的应用程序失败。
两个用户共享目录的更好方法是使用组管理来实现。请参阅有关用户管理的文档或浏览询问 Ubuntu可能的指南:
但是,您只需发出以下命令即可创建用户:
sudo useradd -U -m -G <additional groups here> <user-name>
这将向您的系统添加一个用户,这将起作用。上面一行的解释来自man 8 useradd
(极度缩短为仅最常用的选项,完整概述请查看手册页):
选项
The options which apply to the useradd command are: -c, --comment COMMENT Any text string. It is generally a short description of the login, and is currently used as the field for the user's full name. -g, --gid GROUP The group name or number of the user's initial login group. The group name must exist. A group number must refer to an already existing group. If not specified, the behavior of useradd will depend on the USERGROUPS_ENAB variable in /etc/login.defs. If this variable is set to yes (or -U/--user-group is specified on the command line), a group will be created for the user, with the same name as her loginname. If the variable is set to no (or -N/--no-user-group is specified on the command line), useradd will set the primary group of the new user to the value specified by the GROUP variable in /etc/default/useradd, or 100 by default. -G, --groups GROUP1[,GROUP2,...[,GROUPN]]] A list of supplementary groups which the user is also a member of. Each group is separated from the next by a comma, with no intervening whitespace. The groups are subject to the same restrictions as the group given with the -g option. The default is for the user to belong only to the initial group. -k, --skel SKEL_DIR The skeleton directory, which contains files and directories to be copied in the user's home directory, when the home directory is created by useradd. This option is only valid if the -m (or --create-home) option is specified. If this option is not set, the skeleton directory is defined by the SKEL variable in /etc/default/useradd or, by default, /etc/skel. If possible, the ACLs and extended attributes are copied. -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. -M Do no create the user's home directory, even if the system wide setting from /etc/login.defs (CREATE_HOME) is set to yes. -s, --shell SHELL The name of the user's login shell. The default is to leave this field blank, which causes the system to select the default login shell specified by the SHELL variable in /etc/default/useradd, or an empty string by default. -U, --user-group Create a group with the same name as the user, and add the user to this group. The default behavior (if the -g, -N, and -U options are not specified) is defined by the USERGROUPS_ENAB variable in /etc/login.defs.
因此-U
将创建一个类似于用户名的用户组,其 GUID 与用户 UID 匹配。-m
将在中创建用户主目录,/home/<user-name>
并-G
允许您指定此用户应属于的其他组。您也可以稍后添加组。Ubuntu 添加到主用户的组是adm
、cdrom
、sudo
、dip
、plugdev
和。要稍后添加组lpadmin
,sambashare
您可以使用以下命令:
sudo usermod -aG <group-or groups> <user-name>
最后,您的用户应该有一个密码,您可以使用以下命令设置:
sudo passwd <username>
这结束了终端中的用户创建过程。
还有一个名为的组合命令adduser
,可以自动执行上面提到的一些过程,有关其用法,请查看man 8 adduser
。