linux 系统中的企业用户管理

linux系统中的用户管理

1.用户及用户组存在的意义###

1)用户存在的意义
系统资源是有限的,如何合理分配系统资源?
在这个问题解决时必须要有连个资源配合
1.身份 account
2.授权 author
3.认证 auth

3A机制,3A机制组成系统中最底层的安全架构

2) 用户组存在的意义
用户组是一个逻辑容器
对用户进行归类和统一授权

2. 用户及用户组在系统中存在方式###

电脑对数字敏感 id
人类对字符串敏感 名称

id <------ > 名称 必须要记录到文件当中去用户才能存在
用户就是/etc/passwd文件中的一行字符

用户组存在的方式就是/etc/group 文件中的一行字符

3.用户切换

1)用户查看

whoami ##查看当前用户
id ##查看用户id信息
-u ##查看用户的用户id
-g ##查看用户的主组id
-G ##查看用户所有组的id
-n ##显示名称

[root@westoslinux Desktop]# whoami            ##查看用户名称
root
[root@westoslinux Desktop]# id -u root        ##查看用户的用户id 
0
[root@westoslinux Desktop]# id root 
uid=0(root) gid=0(root) groups=0(root)
[root@westoslinux Desktop]# id -g root        ##查看用户的主组id
0
[root@westoslinux Desktop]# id -G root        ##查看用户的所有组id
0
[root@westoslinux Desktop]# id -n root
id: cannot print only names or real IDs in default format
[root@westoslinux Desktop]# id -Gn root       ##查看用户的所有组id的名称
root 
[root@westoslinux Desktop]# id -gn root
root
[root@westoslinux Desktop]# id -u westos
1000
[root@westoslinux Desktop]# id westos
uid=1000(westos) gid=1000(westos) groups=1000(westos),10(wheel)
[root@westoslinux Desktop]# id -gn westos
westos
[root@westoslinux Desktop]# id -Gn westos
westos wheel
[root@westoslinux Desktop]# su -root

用户id的范围
0 - 65535
0; ##linux超级用户ID
1-999: ##linux系统自用ID
1000-65535 ##用户级ID
##以上ID设定规则都被记录在/etc/login.defs

2)用户切换
su - username

  •  #切换用户环境
    

username ##root 切换其他用户不需要密码
##其他用户之间的切换必须输入密码

           ##注意:在用户切换时当时使用完毕用户身份及时退出
           ##不要在一个shell中反复执行su命令会导致环境错乱

4.用户涉及到的系统配置文件###

/etc/passwd ##用户身份信息文件
##用户名称:用户密码:用户id:用户主组id:用户说明:用户家目录:用户默认shell

/etc/group ##组身份信息文件
##组名称:组密码:组id:组的附加成员

/etc/skel/.* ##用户认证信息文件

/etc/shadow ##用户环境配置文件模板

/home/username ##用户家目录
/var/spool/mail/username ##用户邮箱文件

5.用户和用户组建立以及删除

监控用户建立的命令
watch -n 1 "tail -n 4 /etc/passwd /etc/group;echo=========;ls -l /home "

useradd username ##用户建立
-u id username ##uid
##0 表示超级用户
##1-200 系统预留id
##201-999 系统用户
##1000-60000 用户级用户
##/etc/login.defs 记录用户建立的默认规则
-g id username ##主组id
-G id username ##附加组id
-d dir username ##指定用户家目录
-M username ##建立用户时不建立家目录
-c word username ##指定用户说明
-s shell username ## 指定用户所用的shell
在这里插入图片描述

userdel -r username ##用户删除 -r 删除用户的系统配置文件

groupadd groupname ##建立组
-g id groupname ##指定组名称(建立组id为 …的组)
groupdel groupname ##组删除

在这里插入图片描述

6.用户和用户组的信息管理###

usermod
-l #更改用户名称
-u #更改用户的id
-g #更改用户的组id
-G #更改用户的附加组身份
-aG #添加用户的附加组身份
-c #更改用户说明
-d #更改用户家目录指向
-md #更改用户家目录指向同时更改家目录名称
-s #更改默认shell
-L #冻结账号
-U #解锁

groupmod -g ##更改用户组id

修改用户的名称
在这里插入图片描述
在这里插入图片描述

[root@westoslinux Desktop]# useradd lee                 ##添加一个用户lee
[root@westoslinux Desktop]# userdel lee                 ##删除一个用户lee
[root@westoslinux Desktop]# useradd lee                 ##但是删除没有删除/home/的文件
useradd: warning: the home directory already exists.
Not copying any file from skel directory into it.
Creating mailbox file: File exists
[root@westoslinux Desktop]# userdel -r lee
[root@westoslinux Desktop]# groupadd lee                 ##添加一个组
[root@westoslinux Desktop]# groupdel lee

修改用户的主组id演示
在这里插入图片描述
用户添加附加组

在这里插入图片描述

usermod -G  “ ” username   删除用户的所有附加组

/etc/passwd ##用户身份信息文件
##组名称:组密码:id组:用户主组id:用户说明:用户家目录:用户默认的shell

/etc/group ##组身份信息文件
##组名称:组密码:组id:组的附加成员
/etc/skel/.* ##用户环境配置文件模板
/etc/shadow ##用户认证信息文件
/home/username ##用户家目录
/var/spool/mail/username ##用户邮箱文件

7.用户认证信息管理

#1.用户名称
passwd -S lee ##查看密码状态
在这里插入图片描述

#2.用户加密字符
#更改密码)
passwd lee ##只有root用户可以执行“echo 123 | passwd --stdin lee”

passwd ##普通用户改密码
Changing passwd for user lee
Current passwd: ##输入原始密码
New password : ##输入新的密码(8位以上无序数字+无序字母组合)
Retype new passwd: ##重新输入
passwd :all authentication tokens updated successfully
在这里插入图片描述

#冻结认证)
passwd -l lee ##冻结账号认证
passwd -u lee ##解冻账号认证
在这里插入图片描述
注:上图切换用户的原因(root可以切换任何用户,无论该账号是否冻结)
#密码删除)
passwd -d lee
在这里插入图片描述

/etc/shadow的解读
用户名:密码:密码使用天数:密码最短有效期:密码最长有效期:密码过期警告:认证非活跃天数:账号认证到期时间:为开启功能

在这里插入图片描述
#密码使用天数
*从1970-1-1 算到今天的时间
passwd -e lee ##修改默认使用时间为0
chage -d 0 lee ##账号必须改密码才能登陆系统

#密码最短有效期
passwd -n 1 lee ##lee在一天内不能改密码
chage -m 1 lee ##

#密码最长有效期
passwd -x 40 lee ##40天内lee用户必须更新新的密码否则会被冻结
chage -M 30 lee

#密码过期警告
passwd -w 2 lee ##账户过期前警告时间
chage -W 1 lee

#认证非活跃天数
passwd -i 2 lee ##账号认证最大时间超过后还能用多久
chage -I 1 lee

##账户认证到期时间
chage -E “2020-05-11” ##到“2020-05-11”这天账号会被冻结

##为启用功能
在这里插入图片描述
在这里插入图片描述

1  userdel -r lee                         
    3  useradd lee
    4  usermod -l  linux lee        ##改变用户的名称  将lee改成 linux
    5  usermod -l  lee linux        
    6  usermod -g 72 lee            ##改变用户的主组的id
    7  usermod -g 1001 lee
    8  id lee
    9  usermod -G 72 lee            ##改变用户的附加组的id
   10  id lee
   11  usermod -G 1000 lee
   12  id lee
   13  usermod -G 72 lee
   14  id lee
   15  usermod -aG 1000 lee         ##添加用户的附加组id
   16  usermod -G"" lee
   17  usermod -G ""
   18  usermod -G "" lee
   19  usermod -ag lee
   20  usermod -aG 1000 lee
   21  usermod -G 72 lee
   22  usermod -G "" lee
   23  usermod -c "hello westos" lee
   24  usermod -c "" lee
   25  usermod -d /home/linux lee        ##改变用户的指向
   26  usermod -d /home/lee lee
   27  usermod -md /home/linux lee       ##改变用户的指向和家目录
   28  usermod -s /bin/sh lee
   29  usermod -s /bin/bash              ##改变用户所用的shell
   30  usermod -s /bin/bash lee
   31  usermod -md /home/lee lee





   67  tail -n 3  /etc/passwd /etc/group
   68  tail -n 3  /etc/passwd /etc/group;ls -l /home/      
   69  watch -n 1 "tail -n 3  /etc/passwd /etc/group;ls -l /home/"   ##监控用户密码的文件
   70  watch -n 1 "tail -n 3  /etc/shodow " 
   71  watch -n 1 "tail -n 3  /etc/shadow " 
   72  passwd lee
   73  passwd -e lee                     ##将用户的最短有效期设置为0,意味着用户一登陆就要进行改密码的操作     
   74  chage -I 1 lee
   75  chage -E "2020-1-8"               ##将用户的账户到期时间设置为2020-1-8
   76  chage -E "2020-1-8" lee

####8.用户权力的下放 ###########
在系统中的普通用户时无法执行系统管理命令的
如果需要普通用户执行系统管理动作那么需要
root用户进行授权

普通用户的授权方式为sudo
作用:
可以使用普通用户使用指定用户身份运行命令

授权方法:
visudo ##此命令作用是编辑/etc/Sudoers并提供语法服务检测

visudo ##作为一个单个的命令进行,直接进入系统的权力界面
sudo ##在root用户给予权力之后,调用命令还是要使用sudo
which 一个命令 查看这个命令所在的位置
在此文件的100行左右 ##代码规范性
username hostname=(newusername) NOPASSWD: /command

                                 免密码操作

lee westoslinux.westos.org=(root) NOPASSWD: /usr/sbin/useradd,/usr/sbin/userdel

用户lee ## 在执行添加和删除用户时,相当于超级用户

在这里插入图片描述
在这里插入图片描述

测试 su - lee
sudo useradd westostest ##在第一次使用sudo 命令时需要输入lee密码

exit #退出 lee
su - westos
sudo userdel -r westostest ##westos可以免密执行userdel命令

猜你喜欢

转载自blog.csdn.net/Antonhu/article/details/112982737