本文共 5367 字,大约阅读时间需要 17 分钟。
# 1.权限查看 #ls -l file ##查看文件权限ls -ld dir ##查看目录权限# 2.权限的读取 #"文件的属性被叫做文件的元数据(meta data)""一种元数据用 1 个 byte 来记录内容"#文件权限信息#- | rw-r--r-- | . | 1 | root | root | 0 | Apr 12 10:57 | westos[1] [2] [3] [4] [5] [6] [7] [8] [9]#目录权限信息#d | rw-r--r-- | . | 2 | root | root | 0 | Apr 12 10:57 | westosdir[1] [2] [3] [4] [5] [6] [7] [8] [9]#对于每一位的解释#[1] # 文件类型 #- 普通文件 #d 目录 #l 软连接 #b 快设备 #c 字符设备 #s socket 套接字 #p 管道[2] ##用户权限 ##rw-|r--|r-- 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 ##复制/tmp 目录的权限到/mnt/westosdir 上chmod -R --reference=/tmp /mnt/westosdir #复制/tmp 目录的权限到/mnt/westosdir 及 #目录中的子文件上 -R 代表第归操作#chmod 字符方式设定权限chmod <+|-|=>file示例:chmod u+rw /mnt/westos1chmod u-rw /mnt/westosfile1chmod u-rw,g+x,o+wx /mnt/westosfile2chmod a-rwx /mnt/westosfile3chmod u=rwx,g=rx,o=--- /mnt/westosfile4chmod -R u=rwx,g=rx,o=--- /mnt/westosdir/#chmod 数字方式设定权限#权限波尔指表示方式rwx = 111--- = 000三位二进制可以表示的最大范围为 8 进至数rwx=111=7rw-=110=6r-x=101=5r--=100=4=r-wx=011=3-w-=010=2=w--x=001=1=x---=000=0chmod 600 /mnt/westosfile1 rw-------
系统本身存在的意义共享资源从安全角度讲系统共享的资源越少,开放的权力越小系统安全性越高既要保证系统安全,又要系统创造价值,于是把应该开放的权力默认开放把不安全的权力默认保留#如何保留权力## umask 表示系统保留权力umask #查看保留权力umask 权限值 #临时设定系统预留权力文件默认权限 = 777-umask-111目录默认权限 = 777-umaskumask 值越大系统安全性越高#umask 临时更改umask 077#永久更改vim /etc/bashrc ##shell 系统配置文件74 if [ $UID -gt 199 ] && [ "`id -gn`" = "`id -un`" ]; then75 umask 002-->022 #普通用户的 umask76 else77 umask 022 #root 用户的 umask78 fivim /etc/profile ##系统环境配置文件59 if [ $UID -gt 199 ] && [ "`id -gn`" = "`id -un`" ]; then60 umask 002-->022 #普通用户的 umask61 else62 umask 022 #root 用户的 umask63 fisource /etc/bashrc ##source 作用时使我们更改的内容立即被系统识别source /etc/profile
chown username file ##更改文件拥有者chgrp groupname file ##更改文件拥有组chown username:groupname file ##同时更改文件的拥有者和拥有组chown|chgrp -R user|group dir ##更改目录本身及目录中内容的拥有者或者拥有组
stickyid 粘制位针对目录: #如果一个目录 stickyid 开启,那么这个目录中的文件只能被文件所有人删除chmod 1原始权限 dirchmod o+t dir实验:mkdir /pubchmod 777 /pubsu - westos ----> touch /pub/westosfileexitsu - lee --------> touch /pub/leefilerm -fr /pub/leefile #可以删除rm -fr /pub/westosfile #不属于自己的文件也可以删除如何解决此问题:chmod 1777 /pubchmod o+t /pub以上两条命令都可以开启 pub 目录的 t 权限su - westos ----> touch /pub/westosfileexitsu - lee --------> touch /pub/leefilerm -fr /pub/leefile #可以删除rm -fr /pub/westosfile #不属于自己的文件不能删除rm: cannot remove 'westosfile': Operation not permittedsgid 强制位针对目录: 目录中新建的文件自动归属到目录的所属组中设定:chmod 2源文件权限 dirchmod g+s dir实验:group westostestmkdir /mnt/westosdirchmod 777 /mnt/westosdirchgrp westostest /mnt/westosdirwatch -n 1 ls -lR /mntroot ---> touch /mnt/westosdir/file ##是谁建立的文件组就是谁的chmod g+s /mnt/westosdirroot ---> touch /mnt/westosdir/file1 ##file1 自动复制了/mnt/westosdir 目录组实验:su - westos/bin/watch -n 1 dateps ax -o user,group,comm | grep watch westos westos watch用 root 用户身份chmod g+s /bin/watchsu - westos/bin/watch -n 1 dateps ax -o user,group,comm | grep watch westos root watchsuid 冒险位#只针对二进制的可执行文件(c 程序)#当运行二进制可执行文件时都是用文件拥有者身份运行,和执行用户无关chmod 4原属性 filechmod u+s file实验:su - westos/bin/watch -n 1 dateps ax -o user,group,comm | grep watch westos westos watch用 root 用户身份chmod u+s /bin/watchsu - westos/bin/watch -n 1 dateps ax -o user,group,comm | grep watch root westos watch
Aiccess Control Lists #访问控制列表
功能: 在列表中可以设定特殊用户对于特殊文件有特殊权限#acl 列表开启标识-rw-rw---- 1 root caiwu 0 Apr 18 09:03 westosfile ^ 没有"+"代表 acl 列表未开启-rw-rw----+ 1 root caiwu 0 Apr 18 09:03 westosfile ^ acl 列表功能开启#acl 列表权限读取getfacl westosfile显示内容分析:file: westosfile #文件名称owner: root #文件拥有者group: root #文件拥有组user::rw- #文件拥有者权限user:lee:rw- #特殊指定用户权限group::r-- #文件拥有组权限group:westos:--- #特殊指定的用户组的权限mask::rw- #能够赋予特殊用户和特殊用户组的最大权限阀值other::r-- #其他人的权限"注意:""当文件权限列表开启,不要用 ls -l 的方式来读取文件的权限"#acl 列表的控制setfacl -m u:lee:rw westosfile #设定setfacl -m g:westos:rw westosfilesetfacl -m u::rwx westosfilesetfacl -m g::0 westosfilesetfacl -x u:lee westosfile ##删除列表中的 leesetfacl -b westosfile #关闭#acl 权限优先级拥有者 > 特殊指定用户 > 权限多的组 >权限少的组 > 其他#acl mask 控制mask 是能够赋予指定用户权限的最大阀值问题:当设定完毕文件的 acl 列表之后用 chmod 缩小了文件拥有组的权力mask 会发生变化恢复:setfacl -m m:权限 文件#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 #能添加不能删除lsattr dir|file ##查看 attr 权限chattr +i|+a|-i|-a dir|file ##设定 attr 权限
转载地址:http://ijhzi.baihongyu.com/