linux文件权限【基本权限ugo】

来源:互联网 发布:吉他软件手机版 编辑:程序博客网 时间:2024/06/05 00:19

1*基本权限 U G O

1.文件权限设置: 可以赋于某个用户或组 能够以何种方式 访问某个文件
这里写图片描述
权限对象:
属主: u
属组: g
其他人: o

基本权限类型:
读:r 4
写:w 2
执行: x 1

这里写图片描述
如上图
d:表示文件类型(后续内容会讲到)
rwx:表示属主u的权限
r-x:表示属组g的权限
r-x:表示其他人的权限

==设置权限

【更改文件的属主和属组】
1.chown:
[root@w_hat ~]#chown alice.hr file1
[root@w_hat ~]#chown alice file1 //更改属主
[root@w_hat ~]#chown .hr file //更改属组
[root@w_hat ~]#chown -R wp.hr dir1 //dir1下的所有文件的属主和属组均改变

2.chgrp:
[root@w_hat ~]#chgrp it file1 //更改文件属组
[root@w_hat ~]#chgrp -R it dir1 //递归更改属组

【更改权限】
1.chmod(使用字符更改)
[root@w_hat ~]#chmod u+x file1 //对属主增加执行的权限
[root@w_hat ~]#chmod a=rwx file1 //对所有人的权限都设置为读写执行
[root@w_hat ~]#chmod a=- file1 //所有人都没有权限
[root@w_hat ~]# chmod ug=rw,o=r file1 //属主和属组读写权限,其他人只读

2.chmod(使用数字更改)
[root@w_hat ~]#chmod 644 file1 //属主读写权限,属组和其他人只读

这里写图片描述
这里写图片描述

2*权限设置案例

==设置权限案例

1.针对hr部门的访问目录/home/hr设置权限,要求如下:
(1)root用户和hr组的员工可以读、写、执行
(2)其他用户没有任何权限

[root@w_hat ~]#groupadd hr
[root@w_hat ~]#useradd hr01 -G hr
[root@w_hat ~]#useradd hr02 -G hr
[root@w_hat ~]#mkdir /home/hr
[root@w_hat ~]#chgrp hr /home/hr
[root@w_hat ~]#chmod 770 /home/hr

重要*rwx权限对于文件和目录的意义

这里写图片描述

1.rwx对文件的影响
在home下创建一个文件写入date
[root@w_hat ~]# ll /home/file1
-rw-r–r– /home/file1
[root@w_hat ~]# chmod o+x /home/file1
[root@w_hat ~]# chmod o+w /home/file1
[root@w_hat ~]# su - alice

[alice@w_hat ~]$ cat /home/file1          //测试读[alice@w_hat ~]$ /home/file1               //测试执行-bash: /home/file1: 权限不够[alice@w_hat ~]$ /home/file1
2017年 08月 1日 星期二 19:52:29 CST

[alice@w_hat ~]$ vim /home/file1 //测试写

==rwx对目录的影响==
实战案例2:对目录没有w,对文件有rwx
[root@w_hat ~]# mkdir /dir10
[root@w_hat ~]# touch /dir10/file1
[root@w_hat ~]# chmod 777 /dir10/file1
[root@w_hat ~]# ll -d /dir10/
drwxr-xr-x. /dir10
[root@w_hat ~]# ll /dir10/file1
-rwxrwxrwx. /dir10/file1

[alice@w_hat ~]$ cat /dir10/file1 [alice@w_hat ~]$ rm -rf /dir10/file1 rm: 无法删除"/dir10/file1": 权限不够

实战案例3:对目录有w,对文件没有任何权限
[root@w_hat ~]# chmod 777 /dir10/
[root@w_hat ~]# chmod 000 /dir10/file1
[root@w_hat ~]# ll -d /dir10/
drwxrwxrwx. 2 root root 4096 3月 11 18:37 /dir10/
[root@w_hat ~]# ll /dir10/file1
———-.      /dir10/file1

[alice@w_hat ~]$ cat /dir10/file1 cat: /dir10/file1: 权限不够[alice@w_hat ~]$ rm -rf /dir10/file1 [alice@w_hat ~]$ touch /dir10/file2
原创粉丝点击