点关注不迷路
目录
3、在编辑界面找到Linux16开头的行,在行尾添加rd.break
1用户管理
一、用户介绍
- 多用户多任务的操作系统
- 用户、用户组
- 用户组
- 便于统一设置权限
1、用户相关文件
- /etc/passwd
- 用户信息
- 格式
- 用户名:X:uid:gid:说明信息:家目录:shell
- root:x:0:0:root:/root:/bin/bash
- shell 翻译的作用
- /bin/bash 默认
- /sbin/nologin 不允许登录系统
- shell 翻译的作用
- /etc/shadow
- 用户密码信息
- $6$6iQ2r92GW8UiDSBy$B23qIJIqmASBwZf/FcrlfijEn
- $6: SHA算法,红帽5.x版本为$1 MD5算法
- salt 盐,随机数
- /etc/group
- 用户组
2、用户类型
- 管理员用户
- root,0
- 普通用户
- 1000以后
- 系统用户
- 1---999
- 保证某应用、软件可正常安装部署、运行
- 不允许登录系统 /sbin/nologin
3、用户组
- 基本组
- 一个用户只能加入一个基本组
- 附属组/从属组
- 一个用户加入多个附加组
二、用户操作
1、创建用户
命令:useradd [选项] 用户名
eg:useradd hmq
常用选项
1)-u :指定用户的UID
useradd -u 2000 user01
id user01
2)-G:指定用户加入的组
[root@localhost ~]# vim /etc/passwd
[root@localhost ~]# vim /etc/shadow
[root@localhost ~]# ls -ldh /etc/group
-rw-r--r--. 1 root root 1.2K 7月 14 18:45 /etc/group
[root@localhost ~]# vim /etc/gro
groff/ group group-
[root@localhost ~]# vim /etc/group
[root@localhost ~]# groupadd chuangye
[root@localhost ~]# useradd -G chuangye hmq
[root@localhost ~]# id hmq
uid=1009(hmq) gid=1017(hmq) 组=1017(hmq),1016(chuangye)
[root@localhost ~]# grep "chuangye" /etc/group
chuangye:x:1016:hmq
[root@localhost ~]#
3)-s shell名称 -M
[root@localhost ~]# useradd -u 520 -s /sbin/nologin -M qqq
[root@localhost ~]# grep "qqq" /etc/passwd
qqq:x:520:1018::/home/qqq:/sbin/nologin
2、设置用户密码
- 只有root用户可修改其他密码
- 普通用户只能修改自己的密码
passwd [选项] [用户名]
更改指定的密码
passwd hmq
更改当前用户的密码
passwd
常用选项:
1)-l 锁定用户
passwd -l hmq
2)-u 解锁用户
passwd -u hmq
3、切换用户
su - 用户名
[root@localhost ~]# ls -ldh
drwxr-x---. 15 root root 4.0K 7月 16 09:50 .
[root@localhost ~]# su - userA
上一次登录:四 7月 14 18:55:54 CST 2022pts/0 上
[userA@localhost ~]$ exit
登出
4、删除用户
[root@localhost ~]# grep "hmq" /etc/passwd
[root@localhost ~]# userdel -r boss
[root@localhost ~]# grep "boss" /etc/passwd
[root@localhost ~]# ls /home
ck ggy hmq user1 userA userB userC yg1 yg2
[root@localhost ~]# ls /var/spool/mail/
ck ggy hmq qqq rpc user1 userA userB userC yg1 yg2
[root@localhost ~]#
5、查看用户信息
1)查看用户ID
[root@localhost ~]# id ck
uid=1000(ck) gid=1000(ck) 组=1000(ck)
2)查看当前登录系统的用户
root@localhost ~]# whoami
root
[root@localhost ~]# su - ck
[ck@localhost ~]$ whoami
ck
[ck@localhost ~]$
3)查看系统都有哪些用户登录
[root@localhost ~]# who
root :0 2022-07-16 09:50 (:0)
root pts/0 2022-07-16 09:50 (:0)
6、修改用户信息
命令:usermod [选项] 用户名
1)-u 修改UID
2)-s 修改shell
[root@localhost ~]# grep "www" /etc/passwd
www:x:111:2003::/home/www:/sbin/nologin
[root@localhost ~]# usermod -s /bin/bash www
[root@localhost ~]# grep "www" /etc/passwd
www:x:111:2003::/home/www:/bin/bash
3)-G 将用户加入到组
[root@localhost ~]# id jerry
uid=2001(jerry) gid=2002(jerry) 组=2002(jerry),2001(jishu)
[root@localhost ~]#
[root@localhost ~]# groupadd caiwu
[root@localhost ~]# usermod -G caiwu jerry
[root@localhost ~]#
[root@localhost ~]# id jerry
uid=2001(jerry) gid=2002(jerry) 组=2002(jerry),2004(caiwu)
同时加入到jishu组
[root@localhost ~]# usermod -aG jishu jerry
[root@localhost ~]#
[root@localhost ~]# id jerry
uid=2001(jerry) gid=2002(jerry) 组=2002(jerry),2001(jishu),2004(caiwu)
三、用户组操作
1、创建用户组
命令:groupadd 用户组名
2、删除用户组
命令:groupdel 用户组名
四、用户相关配置文件
1、用户配置文件
- 创建用户时,系统会自动从/etc/skel目录中复制配置文件到用户的家目录
[root@localhost ~]# ls -a /etc/skel
. .. .bash_logout .bash_profile .bashrc .mozilla
[root@localhost ~]# ls -a /home/userB
. .bash_history .bash_profile .cache .mozilla
.. .bash_logout .bashrc .config .viminfo
[root@localhost ~]#
2、.bash_profile文件
- 在用户打开终端时,自动执行该文件中的操作
- 典型应用于定义环境变量
- ~/.bash_profile 针对当前用户生效
- /etc/profile 针对所有用户生效
- 修改命令提示符,su - 切换用户查看效果
export PS1='\[\e[31;1m\]\u\[\e[0m\]\[\e[32;1m\]\[\e[0m\]:\[\e[35;1m\]\w\[\e[0m\]\n\$'
3、.bashrc文件
- 在用户打开终端时,自动执行该文件中的操作
- 典型应用于定义命令别名
- ~/.bashrc 针对当前用户生效
- /etc/bashrc 针对所有用户生效
4、.bash_history
- 对应用户的历史命令
1)查看历史命令
命令:history
2)清除历史命令
命令:history -c
五、重置root密码
- 进入Linux救援模式,重置密码
1、重启系统,在菜单界面按上、下键停止启动
2、选择第一个菜单项,按字母e进入编辑界面
3、在编辑界面找到Linux16开头的行,在行尾添加rd.break
4、按CTRL + x 再次启动系统,进入救援模式
5、以读写的重新关在根目录,并切换到根目录
switch_root# mount -o remount,rw /sysroot
switch_root# chroot /sysroot
6、使用passwd命令修改root密码
命令:passwd root
7、创建隐藏文件
命令:touch /.autorelabel
8、输入两次exit退出,系统会自动重启,验证密码是否正确
2文件目录权限
一、权限表示方法
1、查看文件目录权限
[root@localhost ~]# ls -l /etc/fstab
- rw- r-- r--. 1 root root 465 7月 5 13:51 /etc/fstab
属主用户权限 属组权限 其他用户 属主用户 属组
[root@localhost ~]# ls -ld /etc/
drwxr-xr-x. 148 root root 8192 7月 13 09:08 /etc/
2、常规权限
r 读 4 w 写 2 x 执行 1
- 针对文件
- r:查看文件内容
- w:修改文件内容
- x:脚本文件
- 针对目录
- 用户要正常查看目录下的文件,必须同时拥有rx权限
- r:查看目录下的文件(ls 目录名称)
- w:修改目录下的文件(touch,rm,mv)
- x:可正常切换目录(cd)
二、修改文件、目录权限
1、修改属主、属组
命令:chown 用户名[.用户组名] 文件名称
[root@localhost ~]# chown zzj.caiwu /opt/test/1.txt
[root@localhost ~]# ls -l /opt/test/1.txt
-rw-r--r--. 1 zzj caiwu 0 7月 13 10:37 /opt/test/1.txt
[root@localhost ~]# chown martin /opt/test/2.txt
[root@localhost ~]# ls -l /opt/test/2.txt
-rw-r--r--. 1 martin root 0 7月 13 10:37 /opt/test/2.txt
2、修改属组
命令:chgrp 用户组名 文件名称
[root@localhost ~]# chgrp caiwu /opt/test/3.txt
[root@localhost ~]# ls -l /opt/test/3.txt
-rw-r--r--. 1 root caiwu 0 7月 13 10:37 /opt/test/3.txt
3、修改权限
# chmod {augo}{+-=}{rwx} 文件名称
或
# chmod xxx 文件名称
- a all 所有
- u user 属主
- g group 属组
- o other 其他用户
[root@localhost ~]# chmod g+w /opt/test/1.txt
[root@localhost ~]# ls -l /opt/test/1.txt
-rw-rw-r--. 1 martin caiwu 0 7月 13 10:37 /opt/test/1.txt
[root@localhost ~]# chmod g-rw,o+x /opt/test/1.txt
[root@localhost ~]# ls -l /opt/test/1.txt
-rw----r-x. 1 martin caiwu 0 7月 13 10:37 /opt/test/1.txt
[root@localhost ~]# chmod 600 /opt/test/2.txt
[root@localhost ~]# ls -l /opt/test/2.txt
-rw-------. 1 martin root 0 7月 13 10:37 /opt/test/2.txt
三、facl 文件访问控制列表
- 优势
- 针对单个用户、单个用户组设置权限
1、设置权限
1)针对单个用户
命令:setfacl -m u:用户名:权限 文件名称
[root@localhost ~]# setfacl -m u:user4:r /opt/test/file03
2)针对单个用户组
命令:setfacl -m g:用户组名:权限 文件名称
2、查看facl权限
[root@localhost ~]# getfacl /opt/test/file03
getfacl: Removing leading '/' from absolute path names
# file: opt/test/file03
# owner: user1
# group: user3
user::rw-
user:user4:r--
group::rwx
mask::rwx
other::r-x
3、撤销权限
命令:setfacl -x u:用户名 文件名称
命令:setfacl -x g:用户组名 文件名称
[root@localhost ~]# setfacl -x u:martin /opt/test/file04
[root@localhost ~]# getfacl /opt/test/file04
getfacl: Removing leading '/' from absolute path names
# file: opt/test/file04
# owner: root
# group: root
user::rw-
group::r--
mask::r--
other::r--
四、文件特殊权限
1、特殊权限
- suid 4
- sgid 2
- sticky bit 1
2、suid
- 针对脚本文件、命令设置
- 作用:当普通用户运行期间,会临时获取到该命令对应的属主用户对操作系统的使用权限
[root@localhost ~]# ls -l /usr/bin/passwd
-rwsr-xr-x. 1 root root 27856 4月 1 2020 /usr/bin/passwd
添加suid权限
命令:chmod u+s /usr/bin/passwd
删除suid权限
命令:chmod u-s /usr/bin/passwd
3、sgid
- 针对目录设置
- 作用:在目录下创建文件时,文件会自动继承目录的属组
添加sgid权限
# chmod g+s /opt/linux/
[root@localhost ~]# ls -ldh /opt/linux/
drwxr-srwx. 2 root jishu 201 7月 13 14:50 /opt/linux/
删除sgid权限
# chmod g-s /opt/linux
4、sticky bit
- 针对目录设置
- 作用:防止普通用户相互删除文件
添加sticky bit权限
# chmod o+t /opt/work/
删除sticky bit权限
# chmod o-t /opt/work/
五、umask 权限的反掩码
- 作用:影响文件、目录的默认权限
1、查看umask值
命令:umask
- root 管理员
- 默认值:0022
- 文件:666-022
- 目录:777-022
- 默认值:0022
- 普通用户
- 默认值:0002
六、chown,chmod,chgrp,setfacl
-R 递归修改文件权限
[root@localhost ~]# chown -R martin.caiwu /opt/test/