Linux 管理员 - 用户管理

在讨论用户管理时,我们有三个重要的术语需要理解 −

  • 用户
  • 权限

我们已经深入讨论了应用于文件和文件夹的权限。在本章中,让我们讨论用户和组。

CentOS 用户

在 CentOS 中,有两种类型的帐户 −

  • 系统帐户 − 用于守护进程或其他软件。

  • 交互式帐户 −通常分配给用户以访问系统资源。

两种用户类型之间的主要区别是 −

  • 系统帐户由守护进程用于访问文件和目录。通常不允许通过 shell 或物理控制台登录进行交互式登录。

  • 交互式帐户由最终用户使用,用于从 shell 或物理控制台登录访问计算资源。

有了对用户的基本了解,现在让我们为会计部门的 Bob Jones 创建一个新用户。使用 adduser 命令添加新用户。

以下是一些 adduser 常用开关 −

开关 操作
-c 向用户帐户添加评论
-m 如果不存在,则在默认位置创建用户主目录
-g 分配给用户的默认组
-n 不为用户创建私人组,通常是具有用户名
-M 不创建主目录
-s 除 /bin/bash 之外的默认 shell
-u 指定 UID(否则由系统分配)
-G 将用户分配到的其他组

创建新用户时,使用 -c、-m、-g、-n 开关,如下所示 −

[root@localhost Downloads]# useradd -c "Bob Jones  Accounting Dept Manager" 
-m -g accounting -n bjones

现在让我们看看我们的新用户是否已创建 −

[root@localhost Downloads]# id bjones
(bjones) gid = 1001(accounting) groups = 1001(accounting)

[root@localhost Downloads]# grep bjones /etc/passwd
bjones:x:1001:1001:Bob Jones Accounting Dept Manager:/home/bjones:/bin/bash

[root@localhost Downloads]#

现在我们需要使用 passwd 命令启用新帐户 −

[root@localhost Downloads]# passwd bjones 
Changing password for user bjones. 
New password:  
Retype new password:  
passwd: all authentication tokens updated successfully.

[root@localhost Downloads]#

用户帐户未启用,因此用户无法登录系统。

禁用用户帐户

有几种方法可以禁用系统上的帐户。这些方法包括手动编辑 /etc/passwd 文件。或者甚至使用带有 -l 开关的 passwd 命令。这两种方法都有一个很大的缺点:如果用户具有 ssh 访问权限并使用 RSA 密钥进行身份验证,他们仍然可以使用此方法登录。

现在让我们使用 chage 命令,将密码到期日期更改为之前的日期。此外,最好在帐户上注明我们禁用它的原因。

[root@localhost Downloads]# chage -E 2005-10-01 bjones

[root@localhost Downloads]# usermod -c "Disabled Account while Bob out of the country
for five months" bjones

[root@localhost Downloads]# grep bjones /etc/passwd
bjones:x:1001:1001:Disabled Account while Bob out of the country for four
months:/home/bjones:/bin/bash

[root@localhost Downloads]#

管理组

在 Linux 中管理组使管理员可以方便地将用户组合到容器中,并应用适用于所有组成员的权限集。例如,会计部门的所有用户可能需要访问相同的文件。因此,我们创建一个会计组,添加会计用户。

大多数情况下,任何需要特殊权限的操作都应在组中完成。这种方法通常比将特殊权限应用于单个用户更节省时间。例如,Sally 负责报告,只有 Sally 需要访问某些文件以进行报告。但是,如果 Sally 有一天生病了,而 Bob 负责报告怎么办?或者报告需求增长了?创建组后,管理员只需执行一次。添加用户是在需求发生变化或扩展时应用的。

以下是一些用于管理组的常用命令 −

  • chgrp
  • groupadd
  • groups
  • usermod

chgrp −更改文件或目录的组所有权。

让我们为会计组中的人员创建一个目录来存储文件,并为文件创建目录。

[root@localhost Downloads]# mkdir /home/accounting

[root@localhost Downloads]# ls -ld /home/accounting
drwxr-xr-x. 2 root root 6 Jan 13 10:18 /home/accounting

[root@localhost Downloads]#

接下来,让我们将组所有权赋予accounting组。

[root@localhost Downloads]# chgrp -v  accounting /home/accounting/ 
changed group of '/home/accounting/' from root to accounting

[root@localhost Downloads]# ls -ld /home/accounting/
drwxr-xr-x. 2 root accounting 6 Jan 13 10:18 /home/accounting/

[root@localhost Downloads]#

现在,accounting 组中的每个人都对/home/accounting具有读取执行权限。他们也需要写入权限。

[root@localhost Downloads]# chmod g+w /home/accounting/

[root@localhost Downloads]# ls -ld /home/accounting/
drwxrwxr-x. 2 root accounting 6 Jan 13 10:18 /home/accounting/

[root@localhost Downloads]#

由于 accounting 组 可能处理敏感文件,我们需要对 otherworld 应用一些限制性权限。

[root@localhost Downloads]# chmod o-rx /home/accounting/

[root@localhost Downloads]# ls -ld /home/accounting/
drwxrwx---. 2 root accounting 6 Jan 13 10:18 /home/accounting/

[root@localhost Downloads]#

groupadd − 用于创建新组。

开关 操作
-g 为组指定 GID
-K 覆盖 /etc/login.defs 中的 GID 规范
-o 允许覆盖非唯一组 ID 禁止
-p 组密码,允许用户自行激活

让我们创建一个名为 secret 的新组。我们将为该组添加一个密码,允许用户使用已知密码添加自己。

[root@localhost]# groupadd secret

[root@localhost]# gpasswd secret 
Changing the password for group secret 
New Password:  
Re-enter new password:

[root@localhost]# exit 
exit

[centos@localhost ~]$ newgrp secret 
Password:

[centos@localhost ~]$ groups 
secret wheel rdc

[centos@localhost ~]$

实际上,群组密码并不常用。次要群组就足够了,与其他用户共享密码并不是一个好的安全做法。

groups 命令用于显示用户属于哪个群组。在对当前用户进行一些更改后,我们将使用它。

usermod 用于更新帐户属性。

以下是常见的 usermod 开关。

开关 操作
-a 附加,将用户添加到补充组,仅使用 -G 选项
-c 注释,更新用户注释值
-d 主目录,更新用户的主目录目录
-G 分组,添加或删除次要用户组
-g 组,用户的默认主要组
[root@localhost]# groups centos 
centos : accounting secret

[root@localhost]#

[root@localhost]# usermod -a -G wheel centos

[root@localhost]# groups centos
centos : accounting wheel secret

[root@localhost]#