用户和组管理

何为用户何为组?

例子 进火车站刷脸 上下班刷指纹 天地会接头暗号

用户存在的目的是实现资源的配置。

[En]

The purpose of user existence is to realize the allocation of resources.

例子 共享单车抱回家 总经理行使权利

Authentication:认证
Authorization:授权
Accouting:审计

Linux用户分为管理员和普通用户两种:

用户类别 用户ID 管理员 0 普通用户 1-65535

其中,普通用户分为系统用户和登录用户两类:

[En]

Among them, ordinary users are divided into two types: system users and login users:

用户类别 用户ID 系统用户 1-999(为守护类进程获取系统资源而完成权限指派的用户) 登录用户 1000-60000(为了完成交互式登录使用的用户)

用户组类别 特性 私有组 创建用户时,如果没有为其指定所属的组,

系统会自动创建一个与基本组用户的默认组相同的用户名的组,除了默认组之外的其他组(额外组

[En]

The system will automatically create a group with the same user name as the basic group user’s default group additional group (extra group) other than the default group

Linux用户和组相关的配置文件

配置文件 作用 /etc/passwd 用户及其属性信息(名称、uid、基本组id等等) /etc/group 组及其属性信息 /etc/shadow 用户密码及其相关属性 /etc/gshadow 组密码及其相关属性。在用户执行基本组切换时使用 配置文件 /etc/passwd /etc/group 第一字段 用户名 组名 第二字段 密码占位符 组密码 第三字段 UID GID 第四字段 GID 以当前组为附加组的用户列表(分隔符为逗号) 第五字段 用户的描述信息 第六字段 用户家目录 第七字段 用户的登录shell 配置文件 /etc/shadow 第一字段 登录名 第二字段 加密后的密码 第三字段 最近一次更改密码的日期 第四字段 密码的最小使用期限 第五字段 密码的最大使用期限 第六字段 密码警告时间段 第七字段 密码禁用期 第八字段 帐号的过期日期 第九字段 保留字段

用户和组相关的管理命令

-u UID
useradd命令用来建立用户账号和创建用户的起始目录,使用权限是终极用户。-g:指定用户所属的起始群组。-G:指定用户所属的附加群组。

[root@lnh ~]# id root
uid=0(root) gid=0(root) groups=0(root)
//查看root用户信息
[root@lnh ~]# useradd xbz
[root@lnh ~]# id xbz
uid=1000(xbz) gid=1000(xbz) groups=1000(xbz)
//查看用户id, 组id ,所属组
[root@lnh ~]# useradd -u 2000 xbz1
[root@lnh ~]# id xbz1
uid=2000(xbz1) gid=2000(xbz1) groups=2000(xbz1)
//定义用户id,一般组id也会和其一样

-g GID

[root@lnh ~]# useradd -g 2000 xbz1
[root@lnh ~]# id xbz1
uid=1001(xbz1) gid=2000(xbz1) groups=2000(xbz1)
//定义gid组id

-G groupname(组名)

[root@lnh ~]# useradd -G xbz1  xbz2
[root@lnh ~]# id xbz2
uid=1002(xbz2) gid=1002(xbz2) groups=1002(xbz2),2000(xbz1)
//给xbz2添加xbz1附属组

-c “COMMENT”

[root@lnh ~]# useradd -c xbz1 xbz3
[root@lnh ~]# id xbz3
uid=1003(xbz3) gid=1003(xbz3) groups=1003(xbz3)
[root@lnh ~]# useradd -c xx hh
[root@lnh ~]# tail -5  /etc/passwd
xbz:x:1000:1000::/home/xbz:/bin/bash
xbz1:x:1001:2000::/home/xbz1:/bin/bash
xbz2:x:1002:1002::/home/xbz2:/bin/bash
xbz3:x:1003:1003:xbz1:/home/xbz3:/bin/bash
hh:x:1004:1004:xx:/home/hh:/bin/bash
//注释信息
[root@lnh ~]# useradd -d /home/xbz5 xbz5
[root@lnh ~]# cd ~xbz5/
[root@lnh xbz5]# pwd
/home/xbz5
//指定用户的家目录。此目录必须不能事先存在, 否则将不会从/home/xbz5中复制环境设置文件

-s,-M,-r

[root@lnh ~]# useradd -rMs /sbin/nologin tsb
[root@lnh ~]# ll /home/
total 0
drwx------. 2 hh  hh  62 Jul  3 18:24 hh
drwx------. 2 xbz xbz 62 Jul  3 17:44 xbz
//-r添加一个系统用户,-M创建用户时不创建家目录,-s登录shell不允许登录这个用户
[root@lnh ~]# userdel aaa
[root@lnh ~]# ls /home/
aaa  bbb
[root@lnh ~]# userdel -r bbb
[root@lnh ~]# ls /home/
aaa
//直接用userdel删除,其是默认不删除家目录,-r删除是连着家目录一起删除

id,-u,-g,-G

[root@lnh ~]# id xbz
uid=1000(xbz) gid=1000(xbz) groups=1000(xbz)
//查看用户信息
[root@lnh ~]# id -u xbz
1000
//用户id
[root@lnh ~]# id -g xbz
1000
//组id
[root@lnh ~]# id -G xbz
1000
//附属组

-u UID ,-g GID,-ag

[root@lnh ~]# id tsb
uid=995(tsb) gid=992(tsb) groups=992(tsb)
[root@lnh ~]# usermod -u 111 tsb
//-u 修改用户id,不能和其他用户相同
[root@lnh ~]# usermod -g 993 tsb
[root@lnh ~]# id tsb
uid=111(tsb) gid=993(rngd) groups=993(rngd)
//修改组id,组必须事先存在
[root@lnh ~]# usermod -G xxxb tsb
[root@lnh ~]# id tsb
uid=111(tsb) gid=993(rngd) groups=993(rngd),1112(xxxb)
[root@lnh ~]# usermod -G xbz tsb
[root@lnh ~]# id tsb
uid=111(tsb) gid=993(rngd) groups=993(rngd),1000(xbz)
[root@lnh ~]# usermod -aG xxxb tsb
[root@lnh ~]# id tsb
uid=111(tsb) gid=993(rngd) groups=993(rngd),1000(xbz),1112(xxxb)
//-G修改附加组,没有加-a会覆盖前面的附加组

-md

[root@lnh ~]# ll /home/
total 0
drwx------. 2 1007 1007 62 Jul  3 18:53 aaa
drwx------. 2 xxxb xxxb 62 Jul  3 19:04 xxxb
[root@lnh ~]# usermod -md /opt/shan xxxb
[root@lnh ~]# ll /home/
total 0
drwx------. 2 1007 1007 62 Jul  3 18:53 aaa
[root@lnh ~]# ll /opt/
total 0
drwx------. 2 xxxb xxxb 62 Jul  3 19:04 shan
-rw-r--r--. 1 root root  0 Jul  3 07:55 xbz
//改变用户家目录的同时把原来家目录的文件移动到新的家目录中

-e -f

[root@lnh ~]# usermod -e 2022-7-7 xxxb
[root@lnh ~]# usermod -f 2 xxxb
[root@lnh ~]# cat /etc/shadow |grep xxxb
xxxb:!!:19176:0:99999:7:2:19180:
//-e YYYY-MM-DD 指明用户帐号过期日期
//-f  设置过期的延缓期限

-L -U

[root@lnh ~]# passwd xxxb
Changing password for user xxxb.

New password:
BAD PASSWORD: The password is a palindrome
Retype new password:
passwd: all authentication tokens updated successfully.

[root@lnh ~]# usermod -L xxxb
[root@lnh ~]# cat /etc/shadow |grep xxxb
xxxb:!$6$6RKtLUh/iJRAf5aq$J7MIzJ2sY2VrS5FmeNvrampnjO2S1HyRQKVJz2wYrRI0Qq35CrE/QyxjE6K8mhEV15JqmFOem9ICO0FbeEP6M/:19176:0:99999:7:2:19180:
[root@lnh ~]# usermod -U xxxb
[root@lnh ~]# cat /etc/shadow |grep xxxb
xxxb:$6$6RKtLUh/iJRAf5aq$J7MIzJ2sY2VrS5FmeNvrampnjO2S1HyRQKVJz2wYrRI0Qq35CrE/QyxjE6K8mhEV15JqmFOem9ICO0FbeEP6M/:19176:0:99999:7:2:19180:
//-L锁定帐号,被锁定的帐号在/etc/shadow文件中密码前面会有一个!感叹号
//-U 解锁帐号

-s shell

[root@lnh ~]# usermod -s /sbin/nologin xxxb
[root@lnh ~]# cat /etc/passwd |grep xxxb
xxxb:x:1122:1112::/opt/shan:/sbin/nologin
-s//禁止其登录shell进入xxxb

切换用户的方式 特点 su USERNAME 非登录式切换,即不会读取目标用户的配置文件 su – USERNAME 登录式切换,即会读取目标用户的配置文件。完全切换 su – 不指定用户时默认切换至root用户 root su至其他用户不需要密码,非root用户su至其他用户时需要输入目标用户的密码

[root@lnh ~]# su tushanbu
[tushanbu@lnh root]$ su -
Password:
Last login: Sun Jul  3 19:56:01 CST 2022 from 192.168.222.1 on pts/0
[root@lnh ~]# su - tushanbu
Last login: Sun Jul  3 19:57:00 CST 2022 on pts/0
[tushanbu@lnh ~]$ su -
Password:
Last login: Sun Jul  3 19:57:31 CST 2022 on pts/0
//在root用户下可以直接su tushanbu进入,su -是不指定用户默认切换到root用户,su - tushanbu是登录的方式进行切换
[root@lnh ~]# su - tushanbu -c "mkdir xxx"
[root@lnh ~]# ls /home/tushanbu/
xxx
//不进入用户进行创建

bash的配置文件:

配置文件类型 配置文件路径 全局配置 etc/profile

/etc/profile.d/*.sh

/etc/bashrc 个人配置 ~/.bash_profile

~/.bashrc 配置文件类型 功能 profile类 为交互式登录的shell提供配置,用来设定环境变量、运行命令或脚本 bashrc类 为非交互式登录的shell提供配置,用来设定本地变量、定义命令别名 登录式shell如何读取配置文件? /etc/profile –> /etc/profile.d/*.sh –> ~/.bash_profile –> ~/.bashrc –> /etc/bashrc

非登录式shell如何读取配置文件?
~/.bashrc –> /etc/bashrc –> /etc/profile.d/*.sh

passwd , –stdin

[root@lnh ~]# passwd tushanbu
Changing password for user tushanbu.

New password:
BAD PASSWORD: The password is a palindrome
Retype new password:
passwd: all authentication tokens updated successfully.

//在root下可以直接进行设置密码
[root@lnh ~]# echo 1 |passwd --stdin tushanbu
Changing password for user tushanbu.

passwd: all authentication tokens updated successfully.

//标准输入获取用户密码

-l ,-u ,-d

[root@lnh ~]# passwd -l tushanbu
Locking password for user tushanbu.

passwd: Success
//锁定用户
[root@lnh ~]# passwd -u tushanbu
Unlocking password for user tushanbu.

passwd: Success
//解锁用户
[root@lnh ~]# passwd -d tushanbu
Removing password for user tushanbu.

passwd: Success
//删除用户密码

-n ,-x,-w,-i

[root@lnh ~]# passwd -n 20 tushanbu
Adjusting aging data for user tushanbu.

passwd: Success
//-n mindays最短使用天数20天
[root@lnh ~]# passwd -x 200 tushanbu
Adjusting aging data for user tushanbu.

passwd: Success
//-x maxdays最长使用天数200天
[root@lnh ~]# passwd -w 10 tushanbu
Adjusting aging data for user tushanbu.

passwd: Success
//-w warndays提前10天前发出过期警告
[root@lnh ~]# passwd -i 10 tushanbu
Adjusting aging data for user tushanbu.

passwd: Success
//-i inactivedays可以延期10天
[root@lnh ~]# cat /etc/passwd |grep tushanbu
tushanbu:x:1123:1123::/home/tushanbu:/bin/bash

//语法:openssl command [ command_opts ] [ command_args ]
command //包含标准命令、消息摘要命令、加密命令
version //查看程序版本号
dgst //提取特征码
passwd //生成密码
rand //生成伪随机数

[root@lnh ~]# openssl dgst -md5 /etc/fstab
MD5(/etc/fstab)= 431fca5169ffb3a30eb83c94a11dc2f6
//提取特征码
[root@lnh ~]# openssl passwd -1 -salt hellotom
Password:
$1$hellotom$o9rUZ07NstylvbqW9RrdV/
//生成密码 openssl passwd -1 -salt string  string一般为8位
[root@lnh ~]# openssl rand -base64 20
aDsYyelB9h6ksGSke6A3bN4OzfI=
//生成随机数 openssl rand -base64 NUM,NUM表示随机数的长度
[root@lnh ~]# tr -dc A-Za-z0-9_ < /dev/urandom | head -c 30 |xargs
VnRr0QqXyGGS5SHuv1Rl1KK3TYPbcb
//&#x751F;&#x6210;30&#x4F4D;&#x7684;&#x5BC6;&#x7801;

-g GID ,-r

[root@lnh ~]# groupadd bnx
//&#x521B;&#x5EFA;&#x7EC4;
[root@lnh ~]# groupadd   -g 1222 xnx
//&#x6307;&#x5B9A;&#x7EC4;id
[root@lnh ~]# groupadd -r xbn
//-r &#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x7CFB;&#x7EDF;&#x7EC4;
[root@lnh ~]# cat /etc/passwd |grep xnx
[root@lnh ~]# cat /etc/passwd |grep xbn
[root@lnh ~]# cat /etc/group |grep xbn
xbn:x:991:
[root@lnh ~]# cat /etc/group |grep xnx
xnx:x:1222:
[root@lnh ~]# cat /etc/group |grep bnx
bnx:x:2001:
[root@lnh ~]# groupdel bnx
[root@lnh ~]# groupdel xbn
[root@lnh ~]# groupdel xnx
[root@lnh ~]# cat /etc/group |grep bnx
[root@lnh ~]# cat /etc/group |grep xbn
[root@lnh ~]# cat /etc/group |grep xnx

Original: https://www.cnblogs.com/tushanbu/p/16449412.html
Author: 涂山布
Title: 用户和组管理

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/523187/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球