How we can better manage user under Linux?

User management under Linux

1.Linux Help

2. existing users and user groups significance

1. Why should we manage users?
Because Linux system is a multi-user multi-tasking system, manage users, orderly system to perform a task!
2. User meaning of existence

  • Users are identified in the system operator's identity
  • Presence of the user is to limit the authority (such as the right to use my cup, you want to use my cup you need to see what kind of user identity)
  • The user is the lowest level of security equipment linux system, within the competence of the problem, a system to recycle power
  • That system user system users, user management, in fact, manage files, there is a user's authority to recover
  • Better use of resources

3. The existing user group meaning

  • User groups are collections of users
  • The presence of the group is to facilitate management classify user permissions
  • Better management of resources
  • The initial group: the equivalent one is born, you landed in this family, this home is your initial group. (Home is unique, only one initial group)
  • Additional Group: slowly you start to go to school, class you belong to your additional groups. You participated in a dance class, this class is your additional groups. (You are in a different community activities, you can have several different additional groups)
  • The existence of the group can share power (for example, a class I was, I could go to school as a class to receive information from)
    The initial group: the user just created to join the group
    Additional groups: users join the group at a later stage

3. User profiles and related to the content

User repository /etc/passwd
Group Information Base /etc/group
The default user's home directory / Home / username
User Environment Profile Template / Etc / skel / All Files
User authentication information file /etc/shadow
User group authentication information /etc/gshadow

User Environment Profile template: byebye words when you configure a shutdown for example.

4. User Info for View

1. How to view the user currently in use

[root@workstation Desktop]# whoami
root

Here Insert Picture Description
2. Review the information specified user id

id username View information about specified user id
id -u username View user's uid
id -g View user's gid (see the user's initial group of id)
id -G View user's Gi (see additional group id users)
id -n When you view the display name instead of id numbers

实验解析
(1)查看指定用户id信息

[root@workstation Desktop]# id student
uid=1000(student) gid=1000(student) groups=1000(student),10(wheel)

Here Insert Picture Description
(2)查看指定用户uid(用户id号)

[root@workstation Desktop]# id -u student 
1000

Here Insert Picture Description
(3)查看指定用户的初始组id 即gid

[root@workstation Desktop]# id -g student 
1000

Here Insert Picture Description
(4)查看指定用户的附加组id 即Gid

[root@workstation Desktop]# id -G student 
1000 10

(5)查看用户id信息时显示名字,而不是显示数字的形式

[root@workstation Desktop]# id -Gn student
student wheel

Here Insert Picture Description
(6)若没有指定用户,则查询的时当前用户的id信息

[student@workstation Desktop]$ id -Gn
student wheel

Here Insert Picture Description

5.用户身份的切换方式及环境变量

1.注销当前用户,想用其他用户进行登陆(返回到登陆页面,相当于layout,只不过是用命令实现)

[student@workstation Desktop]$ gnome-session-quit --force

注意:–force和quit之间有一个空格
Here Insert Picture Description
Here Insert Picture Description
2.su命令切换用户身份

su 用户名 仅仅切换用户身份,不切换用户环境
su - 用户名 切换用户身份,并且切换用户环境
[root@workstation Desktop]# su student

(1)当su切换之后,查看所在的环境,虽然用户名已经切换为student,但是用户环境(当前所处的绝对路径)没有切换(仍然在root/Desktop)
扩充:Linux pwd命令用于显示工作目录。
执行pwd指令可立刻得知您目前所在的工作目录的绝对路径名称。

[student@workstation Desktop]$ pwd
/root/Desktop

Here Insert Picture Description
(2)如果想在切换中,不仅实现用户的切换,同时用户所处的环境(当前所在的工作路径)也进行切换。需要用到su - 命令。

[root@workstation Desktop]# su - student 
Last login: Wed Jan  1 11:50:13 EST 2020 on pts/0
[student@workstation ~]$ pwd
/home/student

Here Insert Picture Description
su - 切换到普通用户(student),用户所处的环境(用户环境)切换到普通用户的家目录!

由超级用户切换到普通用户不需要输入密码

但是普通用户切换到超级用户,需要输入密码
也就是 执行高级用户切换到低级目录不需要密码,低级用户切换到高级用户需要密码,平级切换也需要密码
Here Insert Picture Description

6.用户及用户组的建立删除

前言:知识点巩固和扩充
(1)当一个命令想控制多个目标文件时,目标文件之间用空格 隔开

[root@workstation Desktop]# cat file1 file2

Here Insert Picture Description

[root@workstation Desktop]# ls -ls file1 file2

Here Insert Picture Description
(2)想接受命令控制的目标范围,用“;”隔开

[root@workstation Desktop]# cat file1 file2 ; echo hahaha

Here Insert Picture Description
(3)tail -数字 和 tail -n 数字 的区别
显示一个文件的后5行

[root@workstation Desktop]# tail -5 file1
[root@workstation Desktop]# tail -n 5 file1

显示两个文件的后五行

[root@workstation Desktop]# tail -n 5 file1 file2

Here Insert Picture Description
(3)查看普通户用家目录下的详细信息

[root@workstation Desktop]# ls -l /home/

(4)监视命令 watch ,若监视的命令不止一个,应当用双引号“”包住!
进入监视后,按ctrl+c可以退出监视

[root@workstation Desktop]# watch -n 1 "tail -n 5 /etc/passwd /etc/group"

Here Insert Picture Description
Here Insert Picture Description
根据上述知识点的普及,首先搭建实验环境,监视用户信息库/用户组信息库/普通用户家目录的情况

[root@workstation Desktop]# watch -n 1 "tail -n 5 /etc/passwd /etc/group;echo =====;ls -l /home/"

15 16 FIG.

1.用户组的建立和删除

groupadd 组名 建立组(组id系统自动分配)
groupadd -g ID号 组名 指定组号(特定组)建立
groupdel 组名 删除指定组
[root@workstation Desktop]# groupadd westosgroup1
[root@workstation Desktop]# groupadd -g 666 westosgroup2
[root@workstation Desktop]# groupdel westosgroup1

Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description

注意:
(1)当这个组的ID是用户的初始组的时候,是不能进行删除的
(2)若想删除该组,要么更改用户初始组gid,要么删除该用户,或者/etc/group配置文件中手动删除
(3)理论上,不能建立同名的用户和组,但是配置文件中能写入。但是!!!轻易不要写配置文件
(4)删除时,groupdel + 用户名 必须时指定组名,若指定组ID,则删除失败
(5)echo hahaha注意echo命令后面需要有个空格

2.用户的建立和删除

useradd 用户名 用户的建立
userdel -r 用户名 用户的删除
useradd -u 666 用户名 指定uid建立
useradd -g 40 用户名 指定gid建立,40组必须存在
useradd -G 40 用户名 指定用户组,40组必须存在
useradd -c “xxxxx” 用户名 指定用户的说明
useradd -d /home/lee 用户名 指定用户的家目录
useradd -s /bin/sh 用户名 指定用户的默认shell
useradd -M 用户名 建立用户时,不建立家目录

实验解析:
(1)用户建立和删除

[root@workstation Desktop]# useradd westos1
[root@workstation Desktop]# userdel -r westos1

注意:建立和删除用户时,用户/组/家目录都发生变化。因为在建立用户时,会自动建立用户的初始组
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description

(2)建立用户常用参数
1.指定用户uid

[root@workstation Desktop]# useradd -u 666 westos1

Here Insert Picture Description
2.指定用户gid

[root@workstation Desktop]# useradd -g 40 westos1

Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
3.指定用户附加组(gid或者组名都可以)

[root@workstation Desktop]# useradd -c "westos user" westos1

Here Insert Picture Description
Here Insert Picture Description
4.指定用户的说明

[root@workstation Desktop]# useradd -c "westos user" westos1

Here Insert Picture Description
5.指定用户的家目录

[root@workstation Desktop]# useradd -d /home/jia westos1

Here Insert Picture Description

Here Insert Picture Description6.指定用户的默认shell类型
首先查看 系统支持的 和系统对话的 所有shell类型

[root@workstation Desktop]# vim /etc/shells 

Here Insert Picture Description

[root@workstation Desktop]# useradd -s /bin/sh westos1

Here Insert Picture Description
7.不建立家目录的情况下 建立用户(因为无-M时,建立用户时系统自动建立家目录)

[root@workstation Desktop]# useradd -M westos1

Here Insert Picture Description
8.同时指定多个参数建立用户

[root@workstation Desktop]#useradd -u 666 -c "westos user" -d /home/jia -s /bin/bash westos1 

Here Insert Picture Description

7.用户及用户组的信息管理(usermod)

用户和用户组的建立实验过程中,不能更改用户的信息,若想更改,需要先进行删除再新建。本节讲述了如何对已经建好的用户和用户组进行更改。

usermod -l 新名称 用户名 修改用户的名称
usermod -u uid 用户名 修改用户的uid
usermod -g gid 用户名 修改用户的gid
usermod -G gid 用户名 修改用户的附加组(删除原有的附加组,添加新的附加组)
usermod -aG id 用户名 指定用户的附加组(在已有的附加组基础上进行添加)
usermod -G “” 用户名 删除所有附加组的身份
usermod -c “XXX” 用户名 修改用户的说明
usermod -d /home/lee 用户名 更改用户的家目录的指向(只是修改的指向,家目录地址不变化)
usermod -md /home/lee 用户名 修改用户家目录(更改指向,也更改真实目录名称)
usermod -s /bin/sh 用户名 修改用户的shell类型

家目录指向:登陆后告诉用户应该去哪 (用户家目录的指向,但并一定真实存在)
实验解析:
(1)修改用户名

[root@workstation Desktop]# usermod -l jiajia westos1 

Here Insert Picture Description
(2)修改用户uid

[root@workstation Desktop]# usermod -u 666 jiajia 

(3)修改用户gid

[root@workstation Desktop]# usermod -g 1002 jiajia 

Here Insert Picture Description
(4)指定用户附加组

[root@workstation Desktop]# usermod -G dip jiajia 

Here Insert Picture Description

[root@workstation Desktop]# usermod -G westos2 jiajia 

Here Insert Picture Description

[root@workstation Desktop]# usermod -aG westos1 jiajia 

Here Insert Picture Description

(6)删除所有附加组的身份双引号之间无空格

[root@workstation Desktop]# usermod -G "" jiajia 

Here Insert Picture Description
(7)修改用户说明

[root@workstation Desktop]# usermod -c "westos user" jiajia

Here Insert Picture Description
(8)修改用户家目录的指向

[root@workstation Desktop]# usermod -d /home/dangshan jiajia

Here Insert Picture Description
(9)修改用户家目录指向+更改家目录
首先还原实验环境

[root@workstation Desktop]# usermod -d /home/westos1 jiajia 
[root@workstation Desktop]# usermod -md /home/dangshan jiajia 

Here Insert Picture Description
(10)修改用户shell类型

[root@workstation Desktop]# usermod -s /bin/sh jiajia 

Here Insert Picture Description
(11)一次修改多个参数
Here Insert Picture Description

8.用户认证文件的内容分析

前言知识:
(1)用户的认证信息文件 /etc/shadow
注意:
此文件记录用户的认证信息
此文件只有root用户可以查看

[root@workstation Desktop]# vim /etc/shadow

例如:

bin:*:17784:0:99999:7:::

vim /etc/shadow文件中每一行都有9列内容,查看这9列分别是什么意思 所使用的命令是:

[root@workstation Desktop]# vim /etc/shadow

含义分别是:

1用户名称 用户名称
2用户密码 用户密码的加密字符串,默认用的加密方式为sha512对称加密,如果加密字符串前出现“!”,则该用户的帐号被冻结。
3密码已经使用的时间 用户密码最后一次被修改的时间,此时间的计算是从1970-1-1日起累计的总天数。当设置为0时,首次登陆需要设定密码,密码要符合一定的俄规则
4 密码最短有效期 用户密码最短有效期限(用户在这个数字天数之内不能修改密码)如果此位有设定数字,那么在此数字范围内的天数内是不能更改用户的密码的
5密码最长有效期 用户密码最长有效期限,用户必须在此有效期内更新密码,如果超时会被冻结
6密码到期前警告 密码警告期限,在过期前指定天数内会发送警告信息给用户 (还有几天过期 赶紧该密码)
7密码非活跃天数 用户的非活跃天数(加的期限),此位默认为空,如果设定数值,那么在密码最长有效期过后仍然可以使用的天数(过期后还能到用几天 不设定到期就冻结)
8密码到期日 用户到期日,默认为空,表示帐号一定会被冻结的时间点,此时间计算是从1970-1-1开始计算的累计天数 (类似于会员)
9 用户自定义列,目前没有启用

解析名词:
密码规则:
(1)密码不能类似与用户名 (2)无序字符+无序数字 (3)字符和数字组合
对称加密:(加密解密相同字符串) 例如指纹和人脸识别

9.用户认证管理

前言知识
passwd不具备账号到期时间更改
change 不能更改/查看/删除密码 可以改账号到期时间

更改用户密码 passwd 用户名
查看用户密码信息 passwd -S 用户名
删除设定密码 passwd -d 用户名=
冻结用户密码(密码前面加了!!)passwd -l 用户名
解密码 passwd -u 用户名
最后一次修改时间 passwd -e 天数 用户名 d
最短有效期 passwd -n 天数 用户名 m
最长有效期 passwd -x 天数 用户名 M
警告期 passwd -w 天数 用户名 W
非活跃天数 passwd -i 天数 用户名 I
- 修改密码到期日 chage -E 天数 用户名

把超级用户密码改为linux (非交互式更改密码)
usermod -L 用户名 冻结用户
usermod -U 用户名 解锁用户
实验搭建
实验仍需要进行监视,监视的文件是用户认证信息文件/etc/shadow

【root@workstation Desktop]# watch -n 1 tail -n 5 /etc/shadow

Here Insert Picture Description
(1)更改用户密码信息

[root@workstation Desktop]# passwd jjy

Here Insert Picture Description
修改jjy用户密码为8028482jjy
(2)查看用户密码信息

[root@workstation Desktop]# passwd -S jjy 

Here Insert Picture Description
其中,ps(密码被设置过) NP(没有设置密码)
(3)删除用户密码信息

[root@workstation Desktop]# passwd -d jjy 

Here Insert Picture Description
(4)passwd 冻结&解锁 用户密码(冻结密码,密码前加!!)

[root@workstation Desktop]# passwd -l jjy 

Here Insert Picture Description

[root@workstation Desktop]# passwd -u jjy 

Here Insert Picture Description
(5)usermod 冻结&解锁用户密码(冻结密码,密码前加!)

[root@workstation Desktop]# usermod -L jjy 

Here Insert Picture Description

[root@workstation Desktop]# usermod -L jjy 

Here Insert Picture Description
(6)最后一次修改时间

[root@workstation Desktop]# chage -d 0 jjy 

Here Insert Picture Description
(7)最短有效期

[root@workstation Desktop]# passwd -n 30 jjy 

Here Insert Picture Description
(8)最长有效期

[root@workstation Desktop]# chage -M 100 jjy

Here Insert Picture Description
(9)警告期

[root@workstation Desktop]# passwd -w 3 jjy 

Here Insert Picture Description
(10)非活跃天数

[root@workstation Desktop]# chage -I 7 jjy 

Here Insert Picture Description
(11)修改密码到期日

[root@workstation Desktop]# chage -E "2020-01-31" jjy 

Here Insert Picture Description

[root@workstation Desktop]# chage -E 2000000 jjy 

Here Insert Picture Description
扩充 非交互式更改密码

[root@workstation Desktop]# echo linux | passwd --stdin root

Here Insert Picture Description

10.用户权力的下放

前言引入:
1.什么叫用户权力的下放?

  • 超级用户的权力大于普通用户的权力,因此普通用户有些命令不能执行。譬如:用户的建立/删除/管理等。为了是普通用户具有某些超级用户才能够行使的权力,因此用到了权力的下放。
  • 一般情况下普通用户是不能新建用户或者新建用户组,只有超级用户有这个权力
  • Now let ordinary users superuser powers to establish a user or group of users
  • At this point need to be superuser powers to the average user, this is called user authorization
  • Super users to share their powers to the ordinary user, you can also called power-sharing

2. Specific content:

  • Decentralization file / etc / sudoers
  • This file can be edited with vim directly, but does not provide a syntax check
  • You can also use visudo to edit this file, visudo provides syntax detection

3. Specific steps
step1 :( ensure common user environment clean) new common user

[root@workstation Desktop]# useradd westos2

step2: test the average user does not have this authority to add new users

[westos2@workstation ~]$ useradd jia
useradd: Permission denied.

Here Insert Picture Description
step3: to root

[westos2@workstation ~]$ su - root

step4: execute visudo as the root user

   [root@workstation ~]# visudo

Here Insert Picture Description
step5: vim edit this file (ctrl + t to open two shell script in a terminal)
(1) Find the absolute path of useradd

[root@workstation Desktop]# which useradd
/usr/sbin/useradd

(2) View hostname

[root@workstation Desktop]# hostname
workstation.lab.example.com

1
(3) edit the configuration file (/ etc / sudoers)
into the configuration file mode is the root user directly visudo

[root@workstation Desktop]# visudo

Configuration file line 100 is written:

westos2  workstation.lab.example.com=(root) NOPASSWD: /usr/sbin/useradd,/usr/sbin/userdel

Here Insert Picture Description
step6: Switch to westos2 user using useradd / userdel command

[root@workstation Desktop]# su - westos2 
[westos2@workstation ~]$ sudo useradd jia2
[westos2@workstation ~]$ sudo userdel jia2

Note must run the command sudo add in the beginning

Published 11 original articles · won praise 3 · Views 536

Guess you like

Origin blog.csdn.net/baidu_40389082/article/details/103792713