文件权限 Ⅰ
1.基本权限 UGO
=====================================================
- 权限对象:
- 属主: u
- 属组: g
- 其他人: o
- 基本权限类型:
- 读: r 4
- 写: w 2
- 执行: x 1
[root@newrain ~]# ll testfile
-rwxrw-r-- 1 alice hr 0 Aug 4 22:29 testfile
rwx rw- r-- alice hr test.txt
属主权限 属组权限 其他人权限 属主 属组
- 一、alice对testfile.txt文件有什么权限?
- a. alice是所有者吗? 是,rwx
- 二、jack对testfile.txt文件有什么权限? 前提:jack属于hr组
- a. jack是所有者吗?
- b. jack属于hr组吗? 是,rw
- 三、 tom对testfile.txt文件有什么权限?
- a. tom是所有者吗?
- b. tom属于hr组吗?
- c. tom为其他人
1.1.设置权限
更改文件的属主、属组
chown:
[root@newrain ~]# chown alice.hr testfile //改属主、属组
[root@newrain ~]# chown alice testfile //只改属主
[root@newrain ~]# chown .hr testfile //只改属组
[root@newrain ~]# chown -R zhouchen.hr dir1
[root@newrain ~]# chgrp it testfile //改文件属组
[root@newrain ~]# chgrp -R it dir1 //改文件属组
chgrp:
[root@newrain ~]# chgrp it testfile //改文件属组
[root@newrain ~]# chgrp -R it dir1 //改文件属组
1.2.更改权限
a. 使用符号
对象 赋值符 权限类型
u + r
chmod g - w testfile
o = x
a
[root@newrain ~]# chmod u+x testfile //属主增加执行
[root@newrain ~]# chmod a=rwx testfile //所有人等于读写执行
[root@newrain ~]# chmod a=- testfile //所有人没有权限
[root@newrain ~]# chmod ug=rw,o=r testfile //属主属组等于读写,其他人只读
[root@newrain ~]# ll testfile //以长模式方式查看文件权限
-rw-rw-r-- 1 alice it 17 10-25 16:45 testfile //显示的结果
b.使用数字
[root@newrain ~]# chmod 644 testfile
[root@newrain ~]# ll testfile
-rw-r--r-- 1 alice it 17 10-25 16:45 testfile1
2 .权限案例 UGO
======================================================
2.1.设置权限案例
针对hr部门的访问目录/home/hr设置权限,要求如下:
- root用户和hr组的员工可以读、写、执行
- 其他用户没有任何权限
[root@newrain ~]# groupadd hr
[root@newrain ~]# useradd hr01 -G hr
[root@newrain ~]# useradd hr02 -G hr
[root@newrain ~]# mkdir /home/hr
[root@newrain ~]# chgrp hr /home/hr
[root@newrain ~]# chmod 770 /home/hr
[root@newrain ~]# ll -d /home/hr/
drwxrwx---. 2 root hr 4096 3月 13 14:26 /home/hr/
重要: r、w、x权限对文件和目录的意义
2.2.rwx对文件的影响
实战案例1:rwx对文件的影响
[root@newrain ~]# vim /home/testfile
date
[root@newrain ~]# ll /home/testfile
-rw-r--r-- 1 root root 5 7月 26 14:43 /home/testfile
[root@newrain ~]# chmod o+x /home/testfile
[root@newrain ~]# chmod o+w /home/testfile
[root@newrain ~]# su - alice
[alice@newrain ~]$ cat /home/testfile //测试读
[alice@newrain ~]$ /home/testfile //测试执行
-bash: /home/testfile: 权限不够
[alice@newrain ~]$ /home/testfile
2017年 07月 26日 星期三 14:46:29 CST
[alice@newrain ~]$ vim /home/testfile //测试写
date
ls
实战案例2:对目录没有w,对文件有rwx
2.3.rwx对目录的影响
[root@newrain ~]# mkdir /dir10
[root@newrain ~]# touch /dir10/testfile
[root@newrain ~]# chmod 777 /dir10/testfile
[root@newrain ~]# ll -d /dir10/
drwxr-xr-x. 2 root root 4096 3月 11 18:37 /dir10/
[root@newrain ~]# ll /dir10/testfile
-rwxrwxrwx. 1 root root 0 3月 11 18:37 /dir10/testfile
[alice@newrain ~]$ cat /dir10/testfile
[alice@newrain ~]$ rm -rf /dir10/testfile
rm: 无法删除"/dir10/testfile": 权限不够
实战案例3:对目录有w,对文件没有任何权限
[root@newrain ~]# chmod 777 /dir10/
[root@newrain ~]# chmod 000 /dir10/testfile
[root@newrain ~]# ll -d /dir10/
drwxrwxrwx. 2 root root 4096 3月 11 18:37 /dir10/
[root@newrain ~]# ll /dir10/testfile
----------. 1 root root 0 3月 11 18:37 /dir10/testfile
[alice@newrain ~]$ cat /dir10/testfile
cat: /dir10/testfile: 权限不够
[alice@newrain ~]$ rm -rf /dir10/testfile
[alice@newrain ~]$ touch /dir10/file2
对目录有w权限,可以在目录中创建新文件,可以删除目录中的文件(跟文件权限无关)
小结
对目录有w权限,可以在目录中创建新文件,可以删除目录中的文件(跟文件权限无关)
注意事项
文件: x 权限小心给予
目录: w 权限小心给予
3. 基本权限ACL
====================================================
- 文件权限管理之: ACL设置基本权限(r、w、x)
- UGO设置基本权限: 只能一个用户,一个组和其他人
- ACL 设置基本权限: r,w,x
3.1.ACL基本用法
设置:
[root@newrain ~]# touch /home/test.txt
[root@newrain ~]# ll /home/test.txt
-rw-r--r-- 1 root root 0 10-26 13:59 /home/test.txt
[root@newrain ~]# getfacl /home/test.txt
[root@newrain ~]# setfacl -m u:alice:rw /home/test.txt //增加用户alice权限
[root@newrain ~]# setfacl -m u:jack:- /home/test.txt //增加用户jack权限
[root@newrain ~]# setfacl -m o::rw /home/test.txt
3.2.查看帮助
[root@newrain ~]# ll /home/test.txt -rw-rw-r--+ 1 root root 0 10-26 13:59 /home/test.txt [root@newrain ~]# getfacl /home/test.txt
[root@newrain ~]# setfacl -m g:hr:r /home/test.txt
[root@newrain ~]# setfacl -x g:hr /home/test.txt //删除组hr的acl权限
[root@newrain ~]# setfacl -b /home/test.txt //删除所有acl权限
3.2.查看/删除:
[root@newrain ~]# man setfacl /EXAMPLES
[root@newrain ~]# getfacl testfile |setfacl --set-file=- file2 //复制testfile的ACL权限给file2
[root@newrain ~]# chmod a-x /usr/bin/chmod 如何添加x权限???