跳到内容

实验室 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

```bash

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

```bash
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_DI        directory prefix
-U, --users USERS             list of user members of this group
```

passwd

```bash
Usage: passwd [OPTION...] <accountName>
-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”。

警告

系统配置通常符合特定的格式。手动编辑配置文件时,务必遵守此格式。一种方法是找到并复制文件中的现有条目,然后使用任何新的更改修改复制的行/部分。这将有助于减少您犯错的可能性。

  1. 以 root 用户身份登录计算机

  2. 使用 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 文件。

创建用户

  1. 我们需要编辑 /etc/passwd 文件。

    启动您选择的编辑器并打开“/etc/passwd”文件。

    在文件底部或末尾添加以下文本

    me:x:500:500:me mao:/home/me:/bin/bash    
    
  2. 保存更改并关闭 passwd 文件。

  3. 接下来,我们将编辑 /etc/shadow 文件。启动您的编辑器并打开“/etc/shadow”文件。在文件底部添加一个类似于以下条目 - 在密码字段中放置星号 (*)。键入

    me:x:11898:11898:99999:7:::   
    
  4. 保存更改并关闭 shadow 文件。

  5. 接下来,我们将编辑 /etc/group 文件。启动您的编辑器并打开 /etc/group 文件。在文件底部添加一个类似于以下条目

    me:x:1000:me
    
  6. 保存更改并关闭 group 文件。

  7. 现在创建主目录。

    将“/etc/skel”目录的全部内容复制到 /home 目录,并将新目录重命名为用户名,即“/home/me”。键入

    [root@localhost root]# cp -r /etc/skel /home/me
    
  8. 您刚刚创建的目录由 root 用户拥有,因为它是她创建的。要让用户“me mao”能够使用该目录,您需要更改文件夹的权限/所有权。输入

    [root@localhost root]# chown -R me:me /home/me
    
  9. 为用户创建一个密码。将密码的值设置为 a!b!c!d!。您将使用“passwd”工具。输入“passwd”并按照提示操作

    [root@localhost root]# passwd me
    Changing password for user me.
        New password:
        Retype new password:
        passwd: all authentication tokens updated successfully.
    
  10. 完成后退出系统。

练习 2

自动创建新用户

有许多实用程序可用于简化我们在上一个练习中手动执行的所有任务/步骤。我们只演示了创建用户的过程,以便您可以了解后台实际发生的事情。

在本练习中,我们将使用一些常见的工具来管理和简化流程。

您将为用户“Ying Yang”创建另一个用户帐户,登录名为“ying”。

“ying”的密码将是“y@i@n@g@”。

您还将创建一个名为“common”的组,并将用户“me”和“ying”添加到该组。

自动创建新帐户

  1. 以 root 用户身份登录系统。

  2. 您将使用 useradd 命令的所有默认值创建用户 ying。输入

    [root@localhost root]# useradd -c "Ying Yang" ying
    
  3. 使用 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
    

    问题

    在下面列出新条目?

  4. 在您为用户创建密码之前,用户 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.
    
  5. 使用 id 工具快速查看您刚创建的新用户的相关信息。输入

    [root@localhost root]# id me
        uid=1000(me) gid=1000(me) groups=1000(me)
    
  6. 对用户 ying 执行相同操作。输入

    [root@localhost root]# id ying
        uid=501(ying) gid=501(ying) groups=501(ying)
    

自动创建新组

  1. 使用 groupadd 程序创建新的组“common”。

    [root@localhost root]# groupadd common
    
  2. 检查 /etc/group 文件的末尾,以查看新添加的内容。

    问题

    执行此操作的命令是什么?

  3. 使用 usermod 命令将现有用户添加到现有组。让我们将用户 ying 添加到我们在步骤 1 中创建的 common 组。输入

    [root@localhost root]# usermod -G common -a ying
    
  4. 对用户 me 执行相同操作。输入

    [root@localhost root]# usermod -G common -a me
    
  5. 再次对用户“ying”和“me”运行 id 命令。

    问题

    发生了什么变化?

  6. 使用 grep 命令查看对文件中的 common 组条目的更改。输入

    bash [root@localhost root]# grep common /etc/group common:x:1002:ying,me

修改用户帐户

  1. 使用 usermod 命令更改用户“me”的注释字段。您将添加的新注释为“first last”。输入

    [root@localhost root]# usermod -c "first last" me
    

    使用 tail 命令检查您对 /etc/passwd 文件进行的更改。

    问题

    在下面写下更改后的行。

    问题

    用户 me 的登录 shell 是什么?

  2. 再次使用 usermod 命令将 me 的登录 shell 更改为 csh shell。输入

    [root@localhost root]# usermod -s /bin/csh me
    
  3. 最后,使用 usermod 命令撤消您对用户“me”进行的所有更改。

    将值(登录 shell 等)恢复到其原始值。

    问题

    执行此操作的命令是什么?

练习 3

设置用户

完全退出系统以以另一个用户身份登录并不总是方便。这可能是因为您有一些正在运行的任务,并且您不希望结束它们。su(设置用户)程序用于暂时成为另一个用户。您可以从普通用户帐户“su”到 root 帐户,反之亦然。

它会更改当前用户,使其拥有临时用户的访问权限。

HOME、LOGNAME 和 USER 环境变量默认情况下将设置为临时用户的环境变量。

暂时成为另一个用户

  1. 以 root 用户身份登录时,切换到用户“me”。输入

    [root@localhost root]# su   me
    
    [me@localhost root]$
    

    su 命令没有提示您输入用户 me 的密码,因为您是 root

  2. 更改到 me 的主目录。

    [me@localhost root]$ cd
    
    [me@localhost me]$ cd
    
  3. 临时以 me 身份登录时,使用 su 以用户 ying 身份登录。输入

    [me@localhost me]$ su  ying
    password:
    [ying@localhost me]$
    
  4. 要退出 ying 的帐户,请输入

    [ying@localhost me]$ exit
    

    这将使您返回到 me 的帐户。

  5. 退出 me 的帐户,返回到 root 帐户。

    问题

    命令是什么?

使 su 继承新用户的所有环境变量

  1. 要强制 su 使用临时用户的所有环境变量。输入

    [root@system1 root]# su - me
    
    [me@system1 me]$
    

    差异一目了然。注意当前工作目录。

  2. 完全退出系统并重新启动机器。

  3. 第 6 课结束!

作者:Wale Soyinka

贡献者:Steven Spencer、Ganna Zhyrnova