用户的权限
基本权限UGO(user,group,other)
用户的权限(由高级管理员决定某个计算机用户,能否访问某个权限。)
设置权限的两个基本元素
权限对象
- 属主:u
- 属组:g
- 其他人:o
- 特殊对象:所有人:a(u+g+o);(a:all全部)(-a减全部权限)
权限类型
- 读:r=4
- 写:w=2
- 执行:x=1
u=7(让用户拥有读写执行的权限)
u=6(让用户拥有读写的权限)
u=4(让用户拥有读的权限)
设置权限
更改权限
-
使用符号
使用符号:chmod 对象(u用户,g组,o其他,a全部)赋值法(+加/-减/=覆盖)权限类型(r读,w写,x执行) 文件/目录(绝对路径)
eg:chmod (-R) u+r 1.txt
命令 目录 对象加减权限 文件或目录(绝对路径)
查看文件的属性: [root@localhost tmp]# ls -l file01 #以长文件的形式查看当前目录下的文件信息 -rw-r--r--. 1 root root 0 5月 19 15:43 file01 [root@localhost tmp]# ll file01 #以长文件的形式查看当前目录下的文件信息 -rw-r--r--. 1 root root 0 5月 19 15:43 file01
[root@localhost tmp]# ls -l -d /tmp/ #以长文件的形式查看当前目录下的目录信息。
drwxrwxrwx. 31 root root 4096 5月 19 16:42 /tmp/
- rw-r--r-- . 类型和权限(11个字符)-表示没有rwx权限 属主 属组 其他人 rwx rwx rwx u g o 1链接 root属主 root属组 0大小 5月 19 15:43创建时间 file01文件名
eg:1.了解普通文件的基本权限
[root@localhost tmp]# ll file1 -r-xr-xr-x. 1 root root 2 5月 19 15:59 file1 [root@localhost tmp]# chmod u=r file1 #把file1文件属主的权限覆盖为只读 [root@localhost tmp]# ll file1 -r--r-xr-x. 1 root root 2 5月 19 15:59 file1 [root@localhost tmp]# chmod g=rwx file1 #把file1文件属组的权限覆盖为读写执行 [root@localhost tmp]# ll file1 -r--rwxr-x. 1 root root 2 5月 19 15:59 file1 [root@localhost tmp]# chmod g-x file1 #把file1文件属组的权限减去执行 [root@localhost tmp]# ll file1 -r--rw-r-x. 1 root root 2 5月 19 15:59 file1 [root@localhost tmp]# chmod o= file1 #把file文件其他人的权限覆盖为没有 [root@localhost tmp]# ll file1 -r--rw----. 1 root root 2 5月 19 15:59 file1 [root@localhost tmp]# chmod u+wx file1 #把file文件属主的权限加上写执行的权限 [root@localhost tmp]# ll file1 -rwxrw----. 1 root root 2 5月 19 15:59 file1 [root@localhost tmp]# chmod o+r file1 #把file文件其他人的权限加上可读的权限 [root@localhost tmp]# ll file1 -rwxrw-r--. 1 root root 2 5月 19 15:59 file1
eg:2.编写程序
[root@localhost tmp]# ll file1 -rwxrw-r--. 1 root root 14 5月 19 16:56 file1 [root@localhost tmp]# vim file1 [root@localhost tmp]# echo "2004" 2004 [root@localhost tmp]# vim file1 [root@localhost tmp]# cat file1 echo "hello 2022" read -p "input name:" name echo "hello $name beauty" [root@localhost tmp]#
eg:3.增加执行权限
[root@localhost tmp]# chmod u+x file1
[root@localhost tmp]# ll file1
-rwxrw-r--. 1 root root 73 5月 19 17:04 file1#文件绿色代表具有可执行权
eg:4.运行测试
[root@localhost tmp]# ./file1
hello 2022
input name:liu
hello liu beauty
eg:5.去除权限,运行失败
[root@localhost tmp]# chmod u-x file1
[root@localhost tmp]# ./file1
bash: ./file1: 权限不够
eg:6.更多的修该权限练习
[root@localhost tmp]# chmod a=rwx file1 [root@localhost tmp]# ll file1 -rwxrwxrwx. 1 root root 73 5月 19 17:04 file1 [root@localhost tmp]# chmod a= file1 [root@localhost tmp]# ll file1 ----------. 1 root root 73 5月 19 17:04 file1 [root@localhost tmp]# chmod ug=rw,o=r file1 [root@localhost tmp]# ll file1 -rw-rw-r--. 1 root root 73 5月 19 17:04 file1
-
使用数字
chmod 数字(0无权限,4读,2写,1执行;7读写执行,6读写;)数字数字 文件/目录(绝对路径)
eg:
[root@localhost tmp]# ll file1 -rw-rw-r--. 1 root root 73 5月 19 17:04 file1 [root@localhost tmp]# chmod 777 file1#赋予属主属组其他人读写执行权限 [root@localhost tmp]# ll file1 -rwxrwxrwx. 1 root root 73 5月 19 17:04 file1 [root@localhost tmp]# chmod 007 file1#不成立命令 [root@localhost tmp]# ll file1 -------rwx. 1 root root 73 5月 19 17:04 file1 [root@localhost tmp]# chmod 664 file1#赋予属主和属组读写权限,其他人只读的权限。 [root@localhost tmp]# ll file1 -rw-rw-r--. 1 root root 73 5月 19 17:04 file1
更改属主,属组
可以直接改用户的属主和属组,让文件具有权限。
[root@localhost tmp]# ll file1
-rw-rw-r--. 1 root root 73 5月 19 17:04 file1
chown:设置一个文件属于谁;属主,属组。
语法:chown 用户名.组名 绝对文件路径
[root@localhost tmp]# groupadd hr #创建组hr [root@localhost tmp]# chown 001.hr /tmp/file1 #设置文件file1的属主为001,属组为hr。 [root@localhost tmp]# ll file1 -rw-rw-r--. 1 001 hr 73 5月 19 17:04 file1 [root@localhost tmp]# whoami root [root@localhost tmp]# chown root /tmp/file1 #设置文件file1的属主为root;路径一定要是绝对路径。 [root@localhost tmp]# ll file1 -rw-rw-r--. 1 root hr 73 5月 19 17:04 file1 [root@localhost tmp]# chown .root /tmp/file1 #设置文件file1的属组为root。 [root@localhost tmp]# ll file1 -rw-rw-r--. 1 root root 73 5月 19 17:04 file1 [root@localhost tmp]#
chgrp:设置一个文件属于哪个组;属组。 语法:chgrp 新组名 文件/目录(绝对路径) -R(递归)
[root@localhost tmp]# chgrp root /tmp/file1 #使用chgrp命令把/tmp/file1的属组改为root
[root@localhost tmp]# ll /tmp/file1
-rw-rw-r--. 1 001 root 73 5月 19 17:04 /tmp/file1
-R:对文件夹操作,不对文件操作。递归使目录下所有文件和目录,统一设置。
[root@localhost dir]# mkdir -p /dir1/dir2/dir3 [root@localhost dir]# cd /dir1/dir2 [root@localhost dir2]# ls dir3 [root@localhost dir2]# touch file1 [root@localhost dir2]# ls dir3 file1 [root@localhost dir2]# ll file1 -rw-r--r--. 1 root root 0 5月 19 20:43 file1 [root@localhost dir2]# chown -R 001.hr /dir1 [root@localhost dir2]# ll -d /dir1 drwxr-xr-x. 3 001 hr 18 5月 19 20:43 /dir1 [root@localhost dir2]# ll -d /dir1/dir2/ drwxr-xr-x. 3 001 hr 31 5月 19 20:43 /dir1/dir2/ [root@localhost dir2]# ll -d /dir1/dir2/file1 -rw-r--r--. 1 001 hr 0 5月 19 20:43 /dir1/dir2/file1
[root@localhost dir2]# groupadd hr #创建组hr [root@localhost dir2]# useradd hr01 #创建用户hr01 [root@localhost dir2]# useradd hr02 #创建用户hr02 [root@localhost dir2]# usermod hr01 -G hr #把用户hr01添加进hr组 [root@localhost dir2]# usermod hr02 -G hr #把用户hr02添加进hr组 [root@localhost dir2]# mkdir /home/hr #创建文件夹hr [root@localhost dir2]# ll -d /home/hr #以长文件的形式查看hr文件 drwxr-xr-x. 2 root root 6 5月 19 21:03 /home/hr [root@localhost dir2]# chown root.hr /home/hr #修改文件hr的属组为hr [root@localhost dir2]# chmod 744 /home/hr #设置组hr的权限为744 [root@localhost dir2]# ll -d /home/hr #以长文件的形式查看hr文件 drwxr--r--. 2 root hr 6 5月 19 21:03 /home/hr
#1.每个用户都会有一个自己的基本组; user1的用户,就会有一个user1组;
创建组可以在/home目录下找到; #2.文件有二个属性,属主:主人;属组:组员。针对一个文件。
基本权限ACL
(access访问 control控制 list列表)限制用户对文件的访问。)
(ACL是UGO的补充。(加强版))
区别: ACL文件权限管理:设置不同用户,不同的基本权限(r.w.x)。对象数量不同。 UGO设置基本权限:只能一个用户,一个组和其他人。
语法
setfacl -m u/g/o/a : 组名 :权限rwx 文件/目录的绝对路径
设置文件防控 -设置 对象 :对象名 :权限
getfacl 文件路径:查看路径下文件有哪些ACL权限;
[root@localhost ~]# getfacl /home/hr getfacl: Removing leading '/' from absolute path names # file: home/hr # owner: root # group: hr user::rwx group::rwx other::rwx
[root@localhost ~]# id user01 uid=1001(user01) gid=1002(user01) 组=1002(user01),1001(hr) [root@localhost ~]# id user02 uid=1002(user02) gid=1003(user02) 组=1003(user02),1001(hr) [root@localhost ~]# setfacl -m u:user01:rw /home/hr [root@localhost ~]# setfacl -m u:user02:rw /home/hr [root@localhost ~]# getfacl /home/hr getfacl: Removing leading '/' from absolute path names # file: home/hr # owner: root # group: hr user::rwx user:user01:rw- user:user02:rw- group::rwx mask::rwx other::rwx
setfacl -x u/g/o/a : 组名 文件/目录的绝对路径:删除组组的acl权限。
setfacl -b 文件/目录的绝对路径:删除所有acl权限。
ctrl+c终止命令
特殊权限(了解)
特殊位suid
高级权限的类型 suid,(sgid)针对文件/程序时,具备临时提升权限。 1.suid针对文件所设置的一个特别的权限。 功能:使调用文件的用户,临时具备属主的能力。 eg: [root@localhost /]# ll /usr/bin/cat -rwxr-xr-x. 1 root root 54080 6月 30 2016 /usr/bin/cat [root@localhost /]# chmod u+s /usr/bin/cat [root@localhost /]# ll /usr/bin/cat -rwsr-xr-x. 1 root root 54080 6月 30 2016 /usr/bin/cat [root@localhost /]# su 001 [001@localhost /]$ cat /root cat: /root: 是一个目录
文件属性chattr
常用于锁定文件,拒绝修改。
chattr +i不允许操作
[root@localhost ~]# touch /001 [root@localhost ~]# ll /001 -rw-r--r--. 1 root root 0 5月 23 12:44 /001 [root@localhost ~]# chattr +i /001 [root@localhost ~]# ll /001 -rw-r--r--. 1 root root 0 5月 23 12:44 /001 [root@localhost ~]# rm -fv /001 rm: 无法删除"/001": 不允许的操作
[root@localhost ~]# chattr -i /001 [root@localhost ~]# rm -rfv /001 已删除"/001"
chattr +a:允许在文件中进行追加