一.权限查看及读取 1.权限查看 ls -l file #查看文件权限 ls -ld dir #查看目录权限 2.权限的读取 “文件的属性被叫做文件的元数据(meta data)” “一种元数据用1个byte来记录内容” #文件权限信息 - | rw-r–r-- | . | 1 | root | root | 0 | Apr 12 10:57 | westos
#目录权限信息 d | rw-r–r-- | . | 1 | root | root | 0 | Apr 12 10:57 | westosdir
#对于每一位的解释 [1] #文件类型 #- 普通文件 #d 目录 #l 软连接 #b 块设备 #c 字符设备 #s socket套接子 [2] #用户权限 #u|g|o [3] #系统的selinux开启 [4] #对于文件:文件内容被系统记录的次数(硬链接个数) #对于目录:目录中子目录的个数 [5] #文件拥有者 [6] #文件拥有组 [7] #对于文件:文件内容大小 #对于目录:目录中子文件的元数据大小 [8] #文件内容被修改的时间 [9] #文件名称
二.普通权限的类型及作用 1.用户对文件的身份 u: #user 文件的拥有,ls -l看到的第五列信息 g: #group文件拥有组,ls -l看到的第六列信息 o: #other既不是拥有者也不是拥有组成员的其他用户的通称 2.权限位 rwx|r–|r– u g o 3.用户身份匹配 user>group>other 4.权限类型 - #权限未开启 r #可读 #对于文件:可以读出文件内容 #对于目录:可以ls列出目录中的文件 w #可写 #对于文件:可以更改文件内容 #对于目录:可以再目录中新建或者删除文件 x #可执行 #对于文件:可以用文件名称调用文件内记录的程序 #对于目录:可以进入到目录中
三.设定普通权限的方法 chmod #设定文件权限 #chmod 复制权限 chmod --reference=/tmp /mnt/westosdir #复制文件夹的权限给另一个文件夹
chmod -R --reference=/tmp /mnt/westosdir #复制文件夹的权限递归给另一个文件夹
#chmod字符方式设定权限 chmod <a|u|g><+|-|=><r|w|x>file #用字符方式设定文件权限 实例: chmod u-rw /mnt/westos1 chmod u-rw /mnt/westosfile1 chmod u-rw,g+x,o+wx /mnt/westosfile2 chmod a-rwx /mnt/westosfile3 chmod u=rwx,g=rw,o=— /mnt/westosfile4 chmod -R u=rwx,g=rx,o=— /mnt/westosdir/
#chmod数字方式设定权限 权限波尔指数表示方式 rwx = 111 — = 000 三位二进制可以表示的最大范围为8进制数 rwx=111=7 示例: -wx=011=3 -w-=010=2=w –x=001=1=x —=000=0 chmod 600 /mnt/westosfile1 # rw-------
四.系统默认权限设定 #系统本身存在的意义共享资源 #从安全角度讲,系统共享资源越少,开放的权利越小系统安全性越高 #既要保证系统安全,又要系统创造价值,于是把应该开放的权力默认开放 #把不安全的权力默认保留 #umask表示系统保留权利 umask #查看保留权利 umask 权限值 #临时设定系统预留权利 文件默认权限 = 777-umask-111 目录默认权限 = 777-umask #umask值越大系统安全性越高 #umask临时更改 umask 077 #umask永久更改 vim /etc/bashrc #shell系统配置文件 if [KaTeX parse error: Expected 'EOF', got '&' at position 14: UID -gt 199] &̲& ["id -gn" = "…UID -gt 199] && [“id -gn” = “id -un”];then umask 002–>022 #普通用户的umask else umask 022 #root用户的umask fi source /etc/bashrc source/etc/profile #source作用时使我们更改的内容立即被系统识别
五.文件用户,用户组管理 chown username file #更改文件拥有者 chgrp groupname file #更改文件拥有组 chown username:groupname file #同时更改文件的拥有者和拥有组 chown|chgrp -R user|group dir #递归更改目录中所有内容的拥有者或拥有组
六.特殊权限 #stickyid 粘制位 #针对目录 #如果一个目录stickyid开启,那么这个目录中的文件 #只能被文件所有人删除 设定: chmod 1原始权限 dir chmod 0+t dir 实验: mkdir /pub chmod 777 /pub su -westos ---->touch /pub/westosfile exit su - lee ----> touch /pub/leefile rm - fr /pub/leefile #可以删除 rm -fr /pub/westosfile #不属于自己的文件也可以删除 #解决办法 chmod 1777 /pub chmod o+t /pub #以上两条命令都可以开启pub目录的y权限 su - westos ---->touch /pub/westosfile exit su - lee ---->touch /pub/leefile rm -fr /pub/leefile #可以删除 rm -fr /pub/westosfile #不属于自己的文件不能删除
#sgid 强制位 #针对目录:目录中新建的文件自动归属到目录的所属组中 设定: chmod 2源文件权限 dir chmod g+s dir 实验: group westos mkdir /mnt/westosdir chmod 777 /mnt/westosdir watch -n 1 “ls -lR /mnt” westos —> touch /mnt/westosdir/file #是谁建立的文件组就是谁的 chmod g+s /mnt/westosdir westos ----> touch /mnt/westosdir/file1 #file自动复制了/mnt/westosdir目录组
#suid 冒险位 #只针对二进制的可执行文件(c程序) #当运行二进制可执行文件时,都是用文件拥有者身份运行,和用户身份无关 设定: chmod 4原属性 file chmod u+s file 实验: su -westos /bin/cat ps ax -o user,group,comm | grep watch (上一条watch的内容:)westos westos cat su - chmod u+s /bin/watch su - westos /bin/cat ps ax -o user,group,comm | grep cat (上一条watch的内容:)westos root cat
七.acl权限列表 Access Control Lists #访问控制列表 #功能:在列表中可以设定特殊用户对与特殊文件有特殊权限 #acl列表开启标识 -rw-rw---- 1 root caiwu 0 Apr 18 09:02 westosdir ^ #没有"+"代表acl列表未开启 -rw-rw----+ 1 root caiwu 0 Apr 18 09:02 westosdir ^ #acl列表开启 #acl列表权限读取 getfacl westosfile #acl选项 setfacl -x u:lee #删除指定的acl权限 setfacl -b leefile #删除所有的acl权限 setfacl -d dir #设定默认acl权限,只对目录生效,即目录中新建的文件拥有的默认权限 setfacl -k dir #删除默认acl权限 setfacl -R #递归设定acl权限,指设定的acl权限会对目录下的所有的子文件生效
#acl权限优先级 拥有者 > 特殊指定用户 > 权限多的组 > 权限少的组 >其他 #acl mask 控制 #mask是能够赋予指定用户权限的最大阈值 问题:当设定完毕文件的acl列表之后用chmod缩小了文件拥有组的权力,mask会发生变化
恢复:setfacl -m m:权限 文件 #mask的阈值并不能限制chmod #acl列表的默认权限 setfacl -m u:lee:rwx /mnt/westosdir #只对于/mnt/westosdir目录本身生效 setfacl -Rm u:lee:rwx /mnt/westosdir #对于/mnt/westosdir目录及子目录中存在的内容生效 #以上命令只针对以存在的文件生效,新建文件是不会被设定的 setfacl -m d:u:lee:rwx /mnt/westosdir/ #针对于/mnt/westosdir目录中新建文件夹生效
八.attr权限 #attr权限限制所有用户 i #不能作任何的更改 a #能添加不能删除(root也不行),只能读取,只能用echo追加内容 lsattr dir|file #查看attr权限 chattr +i|+a|-i|-a dir|file #设定attr权限 chattr +a xxx #增加a权限 chattr -a xxx #取消a权限 示例a: touch 123 chattr +a 123 lsattr (show)-----a-------------./123 echo “hello” >> 123 vim file 示例b: chattr +i 123 vim 123 (输入123尝试用:wq!保存)