读取:允许查看内容-read r
写入:允许修改内容-write w
可执行:允许运行和切换-excute x
1、对于文本文件:
r读取权限:cat、less、grep、head、tail
w写入权限:vim、> 、 >>
x可执行权限:Shell与Python\Go
2、对于目录:
r读取权限:ls命令查看目录内容
w写入权限:能够创建、删除、修改等目录的内容
x 执行权限:能够cd切换到此目录下(进入此目录)
3、归属关系
所有者(属主):拥有此文件/目录的用户-user u
所属组(属组):拥有此文件/目录的组-group g
其他人:除所有者、所属组以外的用户-other o
4、Linux中判断用户具备的权限:
1)查看用户,对于该数据所处的身份,
顺序:所有者>所属组>其他人,原则是匹配即停止
2)查看相应身份的权限
[root@localhost ~]# mkdir /ns10
[root@localhost ~]# ls -ld /ns10
[root@localhost ~]# chmod u-w /ns10 #所有者去掉w权限
[root@localhost ~]# ls -ld /ns10
[root@localhost ~]# chmod u+w /ns10 #所有者加上w权限
[root@localhost ~]# ls -ld /ns10
[root@localhost ~]# chmod g+w /ns10 #所属组加上w权限
[root@localhost ~]# ls -ld /ns10
[root@localhost ~]# chmod g=r /ns10 #所属组重新定义权限
[root@localhost ~]# ls -ld /ns10
[root@localhost ~]# chmod a=rwx /ns10 #a表示所有人
[root@localhost ~]# ls -ld /ns10
[root@localhost ~]# chmod u=---,g=rx,o=rwx /ns10
[root@localhost ~]# ls -ld /ns10
-R:递归修改权限
[root@localhost ~]# mkdir -p /opt/aa/bb/cc
[root@localhost ~]# chmod -R a=rwx /opt/aa
[root@localhost ~]# ls -ld /opt/aa
[root@localhost ~]# ls -ld /opt/aa/bb
[root@localhost ~]# ls -ld /opt/aa/bb/cc
案例:设置基本权限
1)以root身份新建/dir目录,在此目录下新建readme.txt文件
[root@localhost ~]# mkdir /dir
[root@localhost ~]# echo 123456 > /dir/readme.txt
[root@localhost ~]# cat /dir/readme.txt
2)使用户zhangsan能够修改readme.txt文件内容
[root@localhost ~]# chmod o+w /dir/readme.txt
3)使用户zhangsan不可以修改readme.txt文件内容
[root@localhost ~]# chmod o-w /dir/readme.txt
4)使用户zhangsan能够在此目录下创建/删除子目录
[root@localhost ~]# chmod o+w /dir/
5)调整此目录的权限,使任何用户都不能进入,然后测试用户zhangsan是否还能查看readme.txt内容(测试结果是不能,对父目录没有权限)
[root@localhost ~]# chmod a-x /dir/
6)为此目录及其下所有文档设置权限 rwxr-x---
[root@localhost ~]# chmod -R u=rwx,g=rx,o=--- /dir
r 4
w 2
x 1
u rwx 4+2+1 7
g r-x 4+1 5
o r-x 4+1 5
[root@localhost ~]# mkdir /ns14
[root@localhost ~]# ls -ld /ns14
[root@localhost ~]# chmod 700 /ns14
[root@localhost ~]# ls -ld /ns14
[root@localhost ~]# chmod 007 /ns14
[root@localhost ~]# ls -ld /ns14
[root@localhost ~]# chmod 750 /ns14
[root@localhost ~]# ls -ld /ns14
[root@localhost ~]# chmod 755 /ns14
[root@localhost ~]# ls -ld /ns14
chown 属主 文件...
–chown 属主:属组 文件...
[root@localhost ~]# mkdir /ns15
[root@localhost ~]# ls -ld /ns15
[root@localhost ~]# groupadd tmooc #创建组tmooc
[root@localhost ~]# useradd lisi #创建用户lisi
[root@localhost ~]# chown lisi:tmooc /ns15 #修改所有者与所属组
[root@localhost ~]# ls -ld /ns15
[root@localhost ~]# chown zhangsan /ns15 #仅修改所有者
[root@localhost ~]# ls -ld /ns15
[root@localhost ~]# chown :root /ns15 #仅修改所属组
[root@localhost ~]# ls -ld /ns15
-R 递归修改权限,修改目录以及目录下所有的文件权限
]# chown -R zhangsan:tm /opt/aa
]# ls -ld /opt/aa
]# ls -ld /opt/aa/bb/
]# ls -ld /opt/aa/bb/cc/
]# ls -ld /opt/aa/bb/cc/dd/
文档归属的局限性:
–任何人只属于三种角色:属主、属组、其他人
–针对特殊的人实现更精细的控制
acl访问策略作用:
–能够对个别用户、个别组设置独立的权限
Linux中判断用户具备的权限:
1.首先查看该用户或者组是否有ACL策略
2.查看用户,对于该数据所处的身份,顺序所有者>所属组>其他人,原则是匹配即停止
3.查看相应身份的权限位
[root@localhost ~]# mkdir /ns19
[root@localhost ~]# chmod 770 /ns19
[root@localhost ~]# ls -ld /ns19
[root@localhost ~]# useradd dc
[root@localhost ~]# su - dc
[dc@localhost ~]$ cd /ns19
-bash: cd: /ns19: 权限不够
[dc@localhost ~]$ exit
[root@localhost ~]# setfacl -m u:dc:rx /ns19 #单独赋予dc用户权限
[root@localhost ~]# getfacl /ns19 #查看ACL策略
[root@localhost ~]# su - dc
[dc@localhost ~]$ cd /ns19
[dc@localhost ~]$ pwd
[dc@localhost ~]$ exit
[root@localhost ~]# mkdir /ns22
[root@localhost ~]# setfacl -m u:dc:rx /ns22
[root@localhost ~]# setfacl -m u:zhangsan:rwx /ns22
[root@localhost ~]# setfacl -m u:lisi:rx /ns22
[root@localhost ~]# setfacl -m u:gelin01:rwx /ns22
[root@localhost ~]# getfacl /ns22
[root@localhost ~]# setfacl -x u:zhangsan /ns22 #删除指定用户ACL
[root@localhost ~]# getfacl /ns22
[root@localhost ~]# setfacl -x u:dc /ns22 #删除指定用户ACL
[root@localhost ~]# getfacl /ns22
[root@localhost ~]# setfacl -b /ns22 #清除目录所有ACL策略
[root@localhost ~]# getfacl /ns22
ACL策略-黑名单的使用(单独拒绝某些用户)
[root@localhost ~]# mkdir /home/public
[root@localhost ~]# chmod 777 /home/public
[root@localhost ~]# setfacl -m u:lisi:--- /home/public
[root@localhost ~]# getfacl /home/public
-R:递归设置ACL策略
[root@localhost ~]# setfacl -Rm u:dc:rwx /opt/aa
[root@localhost ~]# getfacl /opt/aa
[root@localhost ~]# getfacl /opt/aa/bb
[root@localhost /]# mkdir /nsd17
[root@localhost /]# chmod 770 /nsd17
[root@localhost /]# ls -ld /nsd17
[root@localhost /]# setfacl -m g:tmooc:rwx /nsd17
[root@localhost /]# getfacl /nsd17
–占用其他人(Other)的 x 位
–显示为 t 或 T,取决于其他人是否有 x 权限
–适用于目录,用来限制用户滥用写入权
–在设置了t权限的目录下,即使用户有写入权限,也不能删除或改名其他用户文档
[root@localhost ~]# mkdir /ns26
[root@localhost ~]# chmod 777 /ns26
[root@localhost ~]# ls -ld /ns26
[root@localhost ~]# chmod o+t /ns26
[root@localhost ~]# ls -ld /ns26
–占用属组(Group)的 x 位
–显示为 s 或 S,取决于属组是否有 x 权限
–对目录有效
–在一个具有SGID权限的目录下,新建的文档会自动继承父目录的属组身份
[root@localhost ~]# mkdir /ns18
[root@localhost ~]# chown :tm /ns18 #修改所属组
[root@localhost ~]# ls -ld /ns18
[root@localhost ~]# chmod g+s /ns18 #赋予SGID特殊权限
[root@localhost ~]# ls -ld /ns18
[root@localhost ~]# mkdir /ns18/abc01
[root@localhost ~]# ls -ld /ns18/abc01
[root@localhost ~]# mkdir /ns18/abc02
[root@localhost ~]# ls -ld /ns18/abc02
[root@localhost ~]# touch /ns18/1.txt
[root@localhost ~]# ls -ld /ns18/1.txt
占用属主(User)的 x 位
显示为 s 或 S,取决于属主是否有 x 权限
仅对可执行的程序有意义
当其他用户执行带SUID标记的程序时,具有此程序属主的身份和相应权限
[root@localhost ~]# which mkdir
[root@localhost ~]# /usr/bin/mkdir /opt/abc01
[root@localhost ~]# ls /opt/
[root@localhost ~]# cp /usr/bin/mkdir /opt/hahadir
[root@localhost ~]# ls -l /opt/hahadir
[root@localhost ~]# /opt/hahadir /opt/abc02
[root@localhost ~]# ls /opt/
[root@localhost ~]# chmod u+s /opt/hahadir
[root@localhost ~]# ls -l /opt/hahadir
[root@localhost ~]# su - zhangsan
[zhangsan@localhost ~]$ mkdir zs01
[zhangsan@localhost ~]$ ls -l
[zhangsan@localhost ~]$ /opt/hahadir zs02
[zhangsan@localhost ~]$ ls -l