Linux基础-用户和组管理

时间:2020-11-21
本文章向大家介绍Linux基础-用户和组管理,主要包括Linux基础-用户和组管理使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

Linux基础-用户和组管理

//Red Hat Enterprise Linux 8.2

用户分类

Linux用户分为管理员和普通用户两种:
用户类别 用户ID
管理员 0
普通用户 1-65535
其中普通用户又分为系统用户和登录用户两种:
用户类别 用户ID
系统用户 1-999(为守护类进程获取系统资源而完成权限指派的用户)
登录用户 1000-60000(为了完成交互式登录使用的用户)

用户管理

useradd //用户创建命令

[root@localhost ~]# useradd 001
useradd: invalid user name '001'				//用户创建不能以纯数字命名
[root@localhost ~]# useradd linux001
[root@localhost ~]# id linux001
uid=1002(linux001) gid=1002(linux001) groups=1002(linux001)

查看用户进程

进程所能够访问的所有资源的权限取决于进程的发起者的身份
①
[root@localhost ~]# vi abc
②
[root@localhost ~]# su - linux001
[linux001@localhost ~]$ vi abc
③
[root@localhost ~]# ps -ef|grep vi
root         985       1  0 11:08 ?        00:00:00 /usr/bin/VGAuthService -s
root        6867    6848  0 19:20 pts/4    00:00:00 vi abc
linux001    6896    6820  0 19:23 pts/0    00:00:00 vi abc
root        6898    6873  0 19:23 pts/5    00:00:00 grep --color=auto vi

文件控制权限

[root@localhost ~]# ll
total 4
-rw-r--r--. 1 root root    0 Nov  9 19:27 abc
-rw-------. 1 root root 1204 Nov  9 19:27 anaconda-ks.cfg
-rw-r--r--. 1 root root    0 Nov  9 19:27 def

↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓

所有者权限	 所有组权限	  其他人权限      所有者  所有组
-  rw-         r--        r--    . 1   root   root    0 Nov  9 19:27 abc
-  rw-         ---        ---    . 1   root   root 1204 Nov  9 19:27 anaconda-ks.cfg
-  rw-         r--        r--    . 1   root   root    0 Nov  9 19:27 def

useradd //用户创建命令

-u (UID) //指定用户的用户号
[root@localhost ~]# useradd -u 2000 linux002
[root@localhost ~]# id linux002
uid=2000(linux002) gid=2000(linux002) groups=2000(linux002)
-g (GID) //指定用户所属的用户组(基本组)
[root@localhost ~]# useradd -g linux001 linux003
[root@localhost ~]# id linux003
uid=2001(linux003) gid=1002(linux001) groups=1002(linux001)
-G (Groupname) //指定用户所属的附加组,可以有多个,用逗号隔开
[root@localhost ~]# useradd -g linux001 -G linux002 linux004
[root@localhost ~]# id linux004
uid=2002(linux004) gid=1002(linux001) groups=1002(linux001),2000(linux002)
覆盖附加组
[root@localhost ~]# useradd -g linux001 -G linux002 -G tom linux005
[root@localhost ~]# id linux005			//linux002被后面的tom覆盖了
uid=2003(linux005) gid=1002(linux001) groups=1002(linux001),1001(tom)
-c (comment) //注释信息
[root@localhost ~]# useradd -c "喻庆浩注释了信息" yqh
[root@localhost ~]# id yqh
uid=2004(yqh) gid=2004(yqh) groups=2004(yqh)
[root@localhost ~]# grep yqh /etc/passwd
yqh:x:2004:2004:喻庆浩注释了信息:/home/yqh:/bin/bash
-d (directory) //指定用户的家目录,此目录必须不能事先存在
[root@localhost ~]# ls /opt/
abc  anaconda-ks.cfg
[root@localhost ~]# useradd -d /opt/007 linux007
[root@localhost ~]# ls /opt/
007  abc  anaconda-ks.cfg
[root@localhost ~]# ll /opt/
total 4
drwx------. 2 linux007 linux007   62 Nov  9 20:00 007
-rw-r--r--. 1 root     root        0 Nov  6 16:39 abc
-rw-------. 1 root     root     1204 Nov  6 15:00 anaconda-ks.cfg
-s (shell) //指定用户登入后所使用的shell
-M //创建用户时不给其创建家目录
-r //添加一个系统用户(uid:1-999)
[root@localhost ~]# useradd -r -M -s /sbin/nologin linux009
[root@localhost ~]# id linux009
uid=994(linux009) gid=991(linux009) groups=991(linux009)

查看用户账号信息

id //默认查看当前用户

[root@localhost ~]# id
uid=0(root) gid=0(root) groups=0(root) context=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023
查看用户帐号的信息命令
-u //查看用户的用户号
-g //查看基本组
-G //查看附加组
[root@localhost ~]# id linux004
uid=2002(linux004) gid=1002(linux001) groups=1002(linux001),2000(linux002)
[root@localhost ~]# id -u linux004
2002
[root@localhost ~]# id -g linux004
1002
[root@localhost ~]# id -G linux004
1002 2000

userdel //用户删除命令

[root@localhost ~]# userdel linux005
[root@localhost ~]# ll /home/
total 0
drwx------. 2 linux001 linux001 62 Nov 10 11:50 linux001
drwx------. 2 linux002 linux002 62 Nov 10 11:51 linux002
drwx------. 2 linux003 linux001 62 Nov 10 11:51 linux003
drwx------. 2 linux004 linux001 62 Nov 10 11:51 linux004
drwx------. 2     2003 linux001 62 Nov 10 11:55 linux005
drwx------. 2 tom      tom      62 Nov 10 11:55 tom
drwx------. 2 yqh      yqh      62 Nov 10 11:56 yqh
[root@localhost ~]# find / -nouser
/home/linux005
/home/linux005/.bash_logout
/home/linux005/.bash_profile
/home/linux005/.bashrc
/var/spool/mail/linux005
[root@localhost ~]# rm -rf /home/linux005
[root@localhost ~]# ll /home/
total 0
drwx------. 2 linux001 linux001 62 Nov 10 11:50 linux001
drwx------. 2 linux002 linux002 62 Nov 10 11:51 linux002
drwx------. 2 linux003 linux001 62 Nov 10 11:51 linux003
drwx------. 2 linux004 linux001 62 Nov 10 11:51 linux004
drwx------. 2 tom      tom      62 Nov 10 11:55 tom
drwx------. 2 yqh      yqh      62 Nov 10 11:56 yqh
-r //删除用户的同时删除其家目录
[root@localhost ~]# userdel -r linux004
[root@localhost ~]# ll /home/
total 0
drwx------. 2 linux001 linux001 62 Nov 10 11:50 linux001
drwx------. 2 linux002 linux002 62 Nov 10 11:51 linux002
drwx------. 2 linux003 linux001 62 Nov 10 11:51 linux003
drwx------. 2 tom      tom      62 Nov 10 11:55 tom
drwx------. 2 yqh      yqh      62 Nov 10 11:56 yqh

usermod //修改用户帐号属性的命令

-u //修改用户的用户号(事先必须不存在)
[root@localhost ~]# id linux001
uid=1002(linux001) gid=1002(linux001) groups=1002(linux001)
[root@localhost ~]# usermod -u 3000 linux001
[root@localhost ~]# id linux001
uid=3000(linux001) gid=1002(linux001) groups=1002(linux001)
-g //修改基本组(事先必须存在)
[root@localhost ~]# id linux002
uid=2000(linux002) gid=2000(linux002) groups=2000(linux002)
[root@localhost ~]# usermod -g linux002 linux001
[root@localhost ~]# id linux001
uid=3000(linux001) gid=2000(linux002) groups=2000(linux002)
-a -G //添加附加组
[root@localhost ~]# id linux004
uid=3001(linux004) gid=1002(linux001) groups=1002(linux001),2000(linux002)
[root@localhost ~]# id tom
uid=1001(tom) gid=1001(tom) groups=1001(tom)
[root@localhost ~]# usermod -a -G tom linux004
[root@localhost ~]# id linux004
uid=3001(linux004) gid=1002(linux001) groups=1002(linux001),2000(linux002),1001(tom)
-G //覆盖此前的附加组
[root@localhost ~]# usermod -G tom linux004
[root@localhost ~]# id linux004
uid=3001(linux004) gid=1002(linux001) groups=1002(linux001),1001(tom)
-m -d //改变用户家目录的同时把原来家目录的文件移动到新的家目录中
[root@localhost ~]# ll /home/
total 0
drwx------. 2 linux001 linux002 62 Nov 10 11:50 linux001
drwx------. 2 linux002 linux002 62 Nov 10 11:51 linux002
drwx------. 2 linux003 linux001 62 Nov 10 11:51 linux003
drwx------. 2 linux004 linux001 62 Nov 10 12:31 linux004
drwx------. 2 tom      tom      62 Nov 10 11:55 tom
drwx------. 2 yqh      yqh      62 Nov 10 11:56 yqh
[root@localhost ~]# ls /opt/
007  abc  anaconda-ks.cfg
[root@localhost ~]# usermod -m -d /opt/004 linux004
[root@localhost ~]# ll /home/
total 0
drwx------. 2 linux001 linux002 62 Nov 10 11:50 linux001
drwx------. 2 linux002 linux002 62 Nov 10 11:51 linux002
drwx------. 2 linux003 linux001 62 Nov 10 11:51 linux003
drwx------. 2 tom      tom      62 Nov 10 11:55 tom
drwx------. 2 yqh      yqh      62 Nov 10 11:56 yqh
[root@localhost ~]# ls /opt/
004  007  abc  anaconda-ks.cfg
[root@localhost ~]# ll /opt/
total 4
drwx------. 2 linux004 linux001   62 Nov 10 12:31 004
drwx------. 2 linux007 linux007   62 Nov 10 11:57 007
-rw-r--r--. 1 root     root        0 Nov 10 11:56 abc
-rw-r--r--. 1 root     root     1714 Nov  9 12:10 anaconda-ks.cfg
-L //锁定帐号(登录不了,但是用root管理员账户可以切换)
[root@localhost ~]# usermod -L linux004
-U //解锁帐号
[root@localhost ~]# usermod -U linux004
-s //修改用户登入后所使用的shell
[root@localhost ~]# useradd -r -M -s /sbin/nologin linux008
[root@localhost ~]# id linux008
uid=993(linux008) gid=990(linux008) groups=990(linux008)
[root@localhost ~]# grep linux008 /etc/passwd
linux008:x:993:990::/home/linux008:/sbin/nologin
[root@localhost ~]# cat /etc/shells 
/bin/sh
/bin/bash
/usr/bin/sh
/usr/bin/bash
[root@localhost ~]# usermod -s /bin/sh linux008
[root@localhost ~]# grep linux008 /etc/passwd
linux008:x:993:990::/home/linux008:/bin/sh
-c //修改注释信息
[root@localhost ~]# grep linux001 /etc/passwd
linux001:x:3000:2000::/home/linux001:/bin/bash
[root@localhost ~]# usermod -c '喻庆浩修改了注释' linux001
[root@localhost ~]# grep linux001 /etc/passwd
linux001:x:3000:2000:喻庆浩修改了注释:/home/linux001:/bin/bash

切换用户命令su

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

su USERNAME:登录式切换,即不会读取目标用户的配置文件

[root@localhost ~]# su linux001
[linux001@localhost root]$ ls
ls: cannot open directory '.': Permission denied
[linux001@localhost root]$ touch abc
touch: cannot touch 'abc': Permission denied

su - USERNAME:登录式切换,即会读取目标用户的配置文件。完全切换

[linux001@localhost root]$ su - root
Password: 
Last login: Tue Nov 10 11:47:47 CST 2020 from 192.168.21.1 on pts/1
[root@localhost ~]# 

su -:不指定用户时默认切换至root用户

[linux001@localhost root]$ su -
Password: 
Last login: Tue Nov 10 13:13:18 CST 2020 on pts/1
[root@localhost ~]# 

-c //切换身份执行命令,命令执行结束后又回到原来的身份

[root@localhost ~]# su - tom -c 'touch pdf'			//登录tom用户创建pdf文件
[root@localhost ~]# ll /home/tom/
total 0
-rw-rw-r--. 1 tom tom 0 Nov 10 13:15 pdf
[root@localhost ~]# su - tom -c 'rm -f pdf'			//登录tom用户删除pdf文件
[root@localhost ~]# ll /home/tom/
total 0

本地和用户配置文件

①
[root@localhost ~]# ls -a
.   anaconda-ks.cfg  .bash_profile  .cshrc
..  .bash_logout     .bashrc        .tcshrc
[root@localhost ~]# echo "alias cdnet='cd /etc/sysconfig/network-scripts'" >> .bashrc
[root@localhost ~]# source .bashrc
[root@localhost ~]# cdnet
[root@localhost network-scripts]# pwd
/etc/sysconfig/network-scripts
②
[root@localhost ~]# echo "alias cdnet='cd /etc/sysconfig/network-scripts'" >> /etc/bashrc
[root@localhost ~]# su - tom
[tom@localhost ~]$ source /etc/bashrc
[tom@localhost ~]$ cdnet
[tom@localhost network-scripts]$ pwd
/etc/sysconfig/network-scripts

密码管理

passwd 密码管理命令

--stdin //从标准输入获取用户密码

[root@localhost ~]# echo 'passsword' | passwd --stdin tom
Changing password for user tom.
passwd: all authentication tokens updated successfully.

openssl 密码生成工具

dgst //提取特征码

-md5 //一种被广泛使用的密码散列函数,可以产生出一个128位的散列值
-sha256 //SHA是一个密码散列函数家族,是FIPS所认证的安全散列算法,算法使用的哈希值长度是256位
-sha512
[root@localhost ~]# ls
abc  anaconda-ks.cfg
[root@localhost ~]# openssl dgst -md5 abc
MD5(abc)= d41d8cd98f00b204e9800998ecf8427e
[root@localhost ~]# openssl dgst -sha256 abc
SHA256(abc)= e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855
[root@localhost ~]# openssl dgst -sha512 abc
SHA512(abc)= cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e

rand //生成伪随机数

-base64
[root@localhost ~]# openssl rand -base64 20
2cRVo6l2/0I7DyENSzx9VYDWW3I=
[root@localhost ~]# openssl rand -base64 50
Dv9I9JdAKJ9UGjytY7QIegtMStsvQhNi2Tn6a2CmGUN6WGQ5PneWdrEJ23Km0E13
eX0=

tr //生成30位的密码

[root@localhost ~]# tr -dc A-Za-z0-9 < /dev/urandom | head -c 30|xargs
2CtC9VYN0ptCVYezdHngzc5wLlgDpu

组管理

groupadd 创建组命令

-r //添加一个系统组

[root@localhost ~]# groupadd -r mysys
[root@localhost ~]# grep mysys /etc/group
mysys:x:991:

-g //指定GID

[root@localhost ~]# groupadd -g 6000 mysys1
[root@localhost ~]# grep mysys1 /etc/group
mysys1:x:6000:

groupdel 删除组命令

[root@localhost ~]# groupdel mysys
[root@localhost ~]# groupdel mysys1
[root@localhost ~]# grep mysys /etc/group
[root@localhost ~]# grep mysys1 /etc/group

各配置文件说明

配置文件 作用
/etc/passwd 用户及其属性信息(名称、uid、基本组id等等)
/etc/group 组及其属性信息
/etc/shadow 用户密码及其相关属性
/etc/gshadow 组密码及其相关属性。在用户执行基本组切换时使用
/etc/passwd
配置文件 /etc/passwd
第一字段 用户名
第二字段 密码占位符
第三字段 UID
第四字段 GID
第五字段 用户的描述信息
第六字段 用户家目录
第七字段 用户的登录shell
[root@localhost ~]# head /etc/passwd
root:x:0:0:root:/root:/bin/bash

↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓

用户名	 密码占位符	UID   GID   描述信息   家目录    登录xshell
root :    x    :  0  :  0  :  root  : /root  : /bin/bash
/etc/group
配置文件 /etc/group
第一字段 组名
第二字段 组密码
第三字段 GID
第四字段 以当前组为附加组的用户列表(分隔符为逗号)
[root@localhost ~]# tail -1 /etc/group
mysys2:x:1001:tom,mysys1

↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓

组名字    组密码   组ID	   组成员
mysys2 :   x   : 1001 :  tom,mysys1
/etc/shadow
配置文件 /etc/shadow
第一字段 登录名
第二字段 加密后的密码
第三字段 最近一次更改密码的日期
第四字段 密码的最小使用期限
第五字段 密码的最大使用期限
第六字段 密码警告时间段
第七字段 密码禁用期
第八字段 帐号的过期日期
第九字段 保留字段
[root@localhost ~]# head -1 /etc/shadow
root:$6$HfCU.N1tU4jF2lfh$Riu5YUAQfW4EoSDU7e4gWtRXulL7I1/pVrO.4xrMl3U.05cibZI3VjIb2IMx9DoOVv/SSfMtpZ7w3m2boBKuc.:18577:0:99999:7:::
[root@localhost ~]# tail -1 /etc/shadow
tom:$6$f7apjWv92zjOhoyD$ZmScI.GW/2mUWJEIbG.ueZ1nrWkWiX/22MvWTyPqlOVatQHX7xoCVJ/n6GiKu3G4nRUiqlallUWhEgWRxeJVI.:18577:0:99999:7:::

原文地址:https://www.cnblogs.com/yuqinghao/p/14017623.html