Linux管理员 - 用户管理

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

  • 用户

  • 群组

  • 权限

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

CentOS用户

在CentOS中,有两种类型的帐号;

  • 系统帐户 : 用于守护程序或其他软件.

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

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

$ b守护进程使用$ b

  • 系统帐户来访问文件和目录.这些通常不会通过shell或物理控制台登录进行交互式登录.

  • 交互式帐户供最终用户访问从shell或物理控制台登录计算资源.

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

以下是一些 adduser 常用开关 :


的其他组

SwitchAction
- 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文件.甚至使用 passwd 命令和 -l 开关.这两种方法都有一个很大的缺点:如果用户具有 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使管理员可以方便地将容器内的用户组合应用适用于所有组成员的权限集.例如,Accounting中的所有用户可能需要访问相同的文件.因此,我们建立一个会计组,添加会计用户.

在大多数情况下,任何需要特殊权限的事情都应该在一个组中完成.这种方法通常可以节省超过仅对一个用户应用特殊权限的时间.例如,Sally负责报告,只有Sally需要访问某些文件进行报告.但是,如果莎莉有一天病了,鲍勃做报告怎么办?或者报告的需求在增长?制作组时,管理员只需要执行一次.添加用户将在需要更改或扩展时应用.

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

  • chgrp

  • groupadd

  • 群组

  • 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]#

接下来,让我们给出分组所有权会计组.

[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]#

现在,每个人在记帐组中,读取执行/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]#

会计小组可以处理敏感文件,我们需要对其他世界应用一些限制性权限.

[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 开关.

SwitchAction
-  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]#