实验 6 - 用户和组管理
目标¶
完成此实验后,您将能够
- 在系统上添加和删除用户
- 在系统上添加和删除组
- 修改系统上的用户和组
- 更改密码
完成此实验的估计时间:40 分钟
用户账户¶
在任何多用户网络操作系统中,用户管理都很重要。Linux 是一种多用户网络操作系统。如果没有用户,根本就不需要多用户网络操作系统!
在系统上管理用户与系统的安全性息息相关。有句老话说:
系统的安全性取决于其最薄弱的用户。
Linux 继承了旧的 UNIX 传统,即基于每个用户和每个组来管理对文件、程序和其他资源的访问权限。
与 Linux 中几乎所有的配置一样,可以通过直接编辑文件系统中的配置文件来执行用户管理。本实验将通过手动方式以及使用系统工具来探索用户管理。
我们还将简要检查文件权限和所有权。
重要的用户和组管理文件如下。其中一些字段或条目也将在下面讨论。
/etc/passwd
¶
- 目的: 用户账户信息
- 内容
- 登录名
- 加密密码
- 用户 ID (UID)
- 组 ID (GID)
- 用户全名
- 用户主目录
- 默认 Shell
/etc/shadow
¶
- 目的: 安全的用户账户信息
- 内容
- 登录名
- 哈希密码
- 自 1970 年 1 月 1 日以来最后一次更改密码的天数
- 密码修改前天数。通常为零。
- 密码必须更改的天数之后
- 密码过期前用户收到过期警告的天数
- 密码过期后账户被视为不活跃和禁用的天数
- 自 1970 年 1 月 1 日以来账户被禁用的天数
- 保留
/etc/group
¶
- 目的: 组信息
- 内容
- 组名
- 组的密码
- 组 ID (GID)
- 属于该组的用户列表
/etc/skel
¶
- 目的: 存储应用于新账户的模板
常用工具¶
以下是一些日常用户和组管理任务中使用的常用工具
useradd
¶
-
useradd
命令是一个用户管理工具,允许系统管理员直接从命令行创建新的用户账户。Usage: useradd [options] LOGIN useradd -D useradd -D [options] Options: --badname do not check for bad names -b, --base-dir BASE_DIR base directory for the home directory of the new account --btrfs-subvolume-home use BTRFS subvolume for home directory -c, --comment COMMENT GECOS field of the new account -d, --home-dir HOME_DIR home directory of the new account -D, --defaults print or change default useradd configuration -e, --expiredate EXPIRE_DATE expiration date of the new account -g, --gid GROUP name or ID of the primary group of the new account -G, --groups GROUPS list of supplementary groups of the new account -h, --help display this help message and exit -k, --skel SKEL_DIR use this alternative skeleton directory -K, --key KEY=VALUE override /etc/login.defs defaults -l, --no-log-init do not add the user to the lastlog and faillog databases -m, --create-home create the user's home directory -M, --no-create-home do not create the user's home directory -N, --no-user-group do not create a group with the same name as the user -o, --non-unique allow to create users with duplicate (non-unique) UID -p, --password PASSWORD encrypted password of the new account -r, --system create a system account -R, --root CHROOT_DIR directory to chroot into -P, --prefix PREFIX_DIR prefix directory where are located the /etc/* files -s, --shell SHELL login shell of the new account -u, --uid UID user ID of the new account -U, --user-group create a group with the same name as the user -Z, --selinux-user SEUSER use a specific SEUSER for the SELinux user mapping
groupadd
¶
-
groupadd
命令是一个用于管理系统上用户组的实用程序。此命令在大多数 Linux 发行版中都可用,它允许管理员创建新组,并指定 GID(组 ID)、组名和其他属性等选项。Usage: groupadd [options] GROUP Options: -f, --force exit successfully if the group already exists, and cancel -g if the GID is already used -g, --gid GID use GID for the new group -h, --help display this help message and exit -K, --key KEY=VALUE override /etc/login.defs defaults -o, --non-unique allow to create groups with duplicate (non-unique) GID -p, --password PASSWORD use this encrypted password for the new group -r, --system create a system account -R, --root CHROOT_DIR directory to chroot into -P, --prefix PREFIX_DIR directory prefix -U, --users USERS list of user members of this group
passwd
¶
-
passwd
命令是用于管理用户密码的工具。它允许用户更改自己的密码,而管理员(root)可以重置其他账户的密码、管理密码过期以及配置安全策略。Usage: passwd [OPTION...] <accountName> Options: -k, --keep-tokens keep non-expired authentication tokens -d, --delete delete the password for the named account (root only); also removes password lock if any -l, --lock lock the password for the named account (root only) -u, --unlock unlock the password for the named account (root only) -e, --expire expire the password for the named account (root only) -f, --force force operation -x, --maximum=DAYS maximum password lifetime (root only) -n, --minimum=DAYS minimum password lifetime (root only) -w, --warning=DAYS number of days warning users receives before password expiration (root only) -i, --inactive=DAYS number of days after password expiration when an account becomes disabled (root only) -S, --status report password status on the named account (root only) --stdin read new tokens from stdin (root only) Help options: -?, --help Show this help message --usage Display brief usage message
练习¶
1. 手动创建新用户¶
到目前为止,在之前的实验中,您一直作为系统上最高权限的用户——root 用户使用系统。在生产系统中,这样做不是一个好习惯,因为它会使系统在安全方面变得脆弱。root 用户可以对系统造成无限的损害。
除了超级用户之外,其他所有用户对文件和目录的访问权限都是有限的。始终以普通用户的身份使用您的计算机。这里将澄清两个容易混淆的概念。
- 首先,root 用户的家目录是
/root
。 - 其次,根目录是顶层目录,称为
/
(斜杠)目录。(/root
与/
不同)
在这个实验中,您将创建一个名为“Me Mao”的新用户。用户“Me Mao”的用户名将是名字——“me”。这个新用户将属于“me”组。密码将是“a1b2c3”。
警告
系统配置通常遵循特定的格式。在手动编辑配置文件时,始终遵循此格式非常重要。一种方法是找到并复制文件中的现有条目,然后修改复制的行/部分以进行任何新更改。这将有助于减少出错的可能性。
-
以 root 身份登录计算机
-
使用
tail
命令查看/etc/passwd
文件底部的最后 4 个条目。[root@localhost root]# tail -n 4 /etc/passwd apache:x:48:48:Apache:/var/www:/sbin/nologin xfs:x:43:43:X Font Server:/etc/X11/fs:/sbin/nologin ntp:x:38:38::/etc/ntp:/sbin/nologin gdm:x:42:42::/var/gdm:/sbin/nologin
您将使用上面显示的格式编辑 *passwd 文件*。
创建用户¶
-
我们需要编辑
/etc/passwd
文件。启动您选择的编辑器并打开文件
/etc/passwd
将下面的文本添加到文件底部或末尾
me:x:1000:1000:me mao:/home/me:/bin/bash
-
保存更改并关闭
etc/passwd
文件。 -
接下来我们将编辑
/etc/shadow
文件。
启动编辑器并打开/etc/shadow
文件。
在密码字段中添加一个新条目,如上所示——在密码字段中放入一个星号(*)。输入me:x:11898:11898:99999:7:::
-
保存更改并关闭 shadow 文件。
-
接下来我们将编辑
/etc/group
文件。启动编辑器并打开/etc/group
文件。在文件底部添加一个新条目,如me:x:1000:me
-
保存更改并关闭 *group 文件*。
-
是时候创建家目录了。
将
/etc/skel
目录的全部内容复制到/home
目录,并将新目录重命名为用户名称,即/home/me
。输入[root@localhost root]# cp -r /etc/skel /home/me
-
您刚刚创建的目录归 root 用户所有,因为是她创建的。为了让“Me Mao”用户能够使用该目录,您需要更改该文件夹的权限/所有权。输入
[root@localhost root]# chown -R me:me /home/me
-
为用户创建密码。将密码设置为 a!b!c!d!。您将使用
passwd
工具。键入“passwd me”并按照提示操作[root@localhost root]# passwd me Changing password for user me. New password: Retype new password: passwd: all authentication tokens updated successfully.
-
完成后退出系统。
2. 自动创建新用户¶
有大量的实用程序可用于简化我们在上一个练习中手动执行的所有任务/步骤。我们只介绍了手动创建用户的过程,以便您可以看到后台实际发生了什么。
在这个练习中,我们将使用一些常用工具来管理和简化该过程。您将为用户“Ying Yang”创建一个新的用户账户,登录名为 ying。
ying 的密码将是 y@i@n@g@。
您还将创建一个名为“common”的组,并将用户 me 和 ying 添加到该组。
自动创建新账户¶
-
以 root 身份登录系统。
-
您将使用
useradd
命令的所有默认值来创建用户 ying。输入[root@localhost root]# useradd -c "Ying Yang" ying
-
使用
tail
命令检查您刚刚添加到/etc/passwd
文件的条目。输入flatpak:x:982:982:User for flatpak system helper:/:/sbin/nologin pesign:x:981:981:Group for the pesign signing daemon:/run/pesign:/sbin/nologin me:x:1000:1000:99999:7::: ying:x:1001:1001:Ying Yang:/home/ying:/bin/bash
问题
在此列出新条目?
-
用户 ying 在您为此用户创建密码之前将无法登录系统。将 ying 的密码设置为 y@i@n@g@。输入
[root@localhost root]# passwd ying Changing password for user ying. New password: ********** Retype new password: ********** passwd: all authentication tokens updated successfully.
-
使用
id
工具快速查看您刚刚创建的新用户信息。输入[root@localhost root]# id me uid=1000(me) gid=1000(me) groups=1000(me)
-
对用户 ying 也执行相同的操作。输入
[root@localhost root]# id ying uid=1001(ying) gid=1001(ying) groups=1001(ying)
自动创建新组¶
-
使用
groupadd
程序创建新组 common。[root@localhost root]# groupadd common
-
检查
/etc/group
文件的尾部以查看新添加的条目。问题
为此的命令是什么?
-
使用
usermod
命令将现有用户添加到现有组。让我们将用户 ying 添加到我们在第 1 步中创建的 common 组。输入[root@localhost root]# usermod -G common -a ying
-
对用户 me 也执行相同的操作。输入
[root@localhost root]# usermod -G common -a me
-
再次运行
id
命令来检查用户 ying 和 me。问题
有什么变化?
-
使用
grep
命令查看文件中 common 组条目的更改。输入[root@localhost root]# grep common /etc/group common:x:1002:ying,me
修改用户账户¶
-
使用
usermod
命令更改用户 me 的注释字段。您将添加的新注释是“first last”。输入[root@localhost root]# usermod -c "first last" me
使用
tail
命令检查您对/etc/passwd
文件的更改。问题
在下方写下更改后的行。
问题
用户 me 的登录 Shell 是什么?
-
再次使用
usermod
命令将 me 的登录 Shell 更改为csh
Shell。输入[root@localhost root]# usermod -s /bin/csh me
-
最后,使用
usermod
命令撤销您上面对用户 me 所做的所有更改。将值(登录 Shell 等)恢复到原始值。
问题
执行此操作的命令是什么?
3. 设置用户¶
完全注销系统以登录其他用户并不总是方便的。这可能是因为您正在运行某些任务,并且不想结束它们。su
(*set user*)程序用于临时切换到其他用户。您可以从普通用户账户“su”到 root 账户,反之亦然。
它将当前用户更改为具有临时用户的访问权限。
默认情况下,HOME、LOGNAME 和 USER 环境变量将设置为临时用户的环境变量。
临时切换到其他用户¶
-
在以 root 用户身份登录的情况下,切换到用户 me。输入
[root@localhost root]# su me [me@localhost root]$
su
命令没有提示您输入用户 me 的密码,因为您是 root。 -
切换到 me 的家目录。
[me@localhost root]$ cd [me@localhost me]$ cd
-
以 me 的身份临时登录时,使用
su
登录到用户 ying。输入[me@localhost me]$ su ying password: [ying@localhost me]$
-
要退出 ying 的账户,请输入
[ying@localhost me]$ exit
这将让您回到 me 的账户。
-
退出 me 的账户以返回到 root 账户。
问题
命令是什么?
使 su 继承新用户的所有环境变量¶
-
强制
su
使用临时用户的所有环境变量。输入[root@system1 root]# su - me [me@system1 me]$
差异显而易见。请注意当前的*工作目录*。
-
完全注销系统并*重新启动*机器。
-
实验 6 全部完成!
作者:Wale Soyinka
贡献者:Steven Spencer, Ganna Zhyrnova, Franco Colussi