centos7-文件权限管理

7.1  文件的基本权限:r  w   x   (UGO)

7.2  文件的特殊权限:suid   sgid  sticky   和文件扩展权限ACL  

7.3 实战:创建一个让root都无法删除的文件

7.1  文件的基本权限

7.1.1  权限的作用

通过对文件设定权限可以达到以下三种访问权限

只允许自己访问,

允许一个预先访问的用户组的用户访问

允许系统中的任何用户访问


其中:文件类型 p  d   l   s   c   b   -  

p表示命名管道文件

d  表示目录文件

-  表示普通文件

l  表示符号链接文件

s  表示socket 套接口文件,比如我们启动mysql,会产生一个mysql.sock 

c   表示字符设备文件   例子:虚拟控制台或者tty0 

b  表示块设备文件   例  sda  ,cdrom 


7.1.3  权限说明

对于文件来说

r:读取

w: 写

x: 执行

对于目录本来说

r:  读

s:  在目录里面键文件,删除,移动,touch ,mkdir,rm  ,  mv  cp 

x  :进入cd 

7.1.4  文件拥有者

UGO  所有者-- 用户组--其他用户


其他用户:系统内的其他用户就是other用户类

7.1.5 举例说明



每个用户都有自己的专属目录,通常放置在/home下

[root@xueshen65 ~]# ll /home/

总用量 4

drwx------. 15 centos centos 4096 3月  24 10:50 centos

注:表示目录所有者拥有的权限,其他用户是无法进入的


7.1.5.2  更改文件的属主和属组 

改变人家的所属关系用到命令

chown :用来改变文件或目录的属主

chgrp  :用来改变文件或目录的默认属组

如果你要对目录进行操作 夹参数   -R    

chown  -R  


[root@xueshen65 ~]# touch 1.txt 2.txt 3.txt

[root@xueshen65 ~]# ls *.txt

1.txt  2.txt  3.txt

[root@xueshen65 ~]# touch {a,b,c}.txt

[root@xueshen65 ~]# ls *.txt

1.txt  2.txt  3.txt  a.txt  b.txt  c.txt

[root@xueshen65 ~]# ll *.txt

-rw-r--r--. 1 root root 0 3月  26 12:02 1.txt

-rw-r--r--. 1 root root 0 3月  26 12:02 2.txt

-rw-r--r--. 1 root root 0 3月  26 12:02 3.txt

-rw-r--r--. 1 root root 0 3月  26 12:03 a.txt

-rw-r--r--. 1 root root 0 3月  26 12:03 b.txt

-rw-r--r--. 1 root root 0 3月  26 12:03 c.txt


[root@xueshen65 ~]# ll a.txt

-rw-r--r--. 1 centos root 0 3月  26 12:03 a.txt

[root@xueshen65 ~]# chown centos:centos a.txt

[root@xueshen65 ~]# ll a.txt

-rw-r--r--. 1 centos centos 0 3月  26 12:03 a.txt

[root@xueshen65 ~]# chown :root a.txt

[root@xueshen65 ~]# ll a.txt

-rw-r--r--. 1 centos root 0 3月  26 12:03 a.txt



文件所有者一定可以写文件,就像root可以对shadow 强制写,引shadow的拥有者root

7.1.6  修改权限

   使用字符设定

修改权限用的命令:chmod

作用:修改文件,目录的权限

语法:chmod  对谁操作  操作符   赋予什么权限   文件名

对谁操作

u  ----->  用户user  ,表示文件或目录的所有者

g------>   用户组     。表示人家或目录所属于的用户组

o  ---->   其他用户other

a--->  所有用户all

+  :添加权限,  -  减少权限   =  直接给定一个权限


[root@xueshen65 ~]# touch 1.txt [root@xueshen65 ~]# ll 1.txt -rw-r--r--. 1 root root 0 3月 26 15:44 1.txt [root@xueshen65 ~]# chmod u-w 1.txt [root@xueshen65 ~]# ll 1.txt -r--r--r--. 1 root root 0 3月 26 15:44 1.txt

  chmod 修改权限
[root@xueshen65 ~]# touch 1.txt

[root@xueshen65 ~]# ll 1.txt

-rw-r--r--. 1 root root 0 3月  26 15:44 1.txt

[root@xueshen65 ~]# chmod u-w 1.txt

[root@xueshen65 ~]# ll 1.txt

-r--r--r--. 1 root root 0 3月  26 15:44 1.txt

[root@xueshen65 ~]# chmod g+x 1.txt

[root@xueshen65 ~]# ll 1.txt

-r--r-xr--. 1 root root 0 3月  26 15:44 1.txt

[root@xueshen65 ~]# chmod a+x 1.txt    给shell脚本一个可执行权限

[root@xueshen65 ~]# ll 1.txt

-r-xr-xr-x. 1 root root 0 3月  26 15:44 1.txt

[root@xueshen65 ~]# chmod a=rwx 1.txt

[root@xueshen65 ~]# ll 1.txt

-rwxrwxrwx. 1 root root 0 3月  26 15:44 1.txt



7.1。7  权限文件和目录的影响

有三种删除


7.1.8  补码 

为什么我们创建的文件默认权限是644

我们创建文件的默认权限是怎么来的

umask  命令允许你设定文件创建时的缺省模式,对应每一类用户   文件属主,同组用户

其他用户在存在一个相应的umask  中的数字

文件默认权限是666,目录默认权限=777

我们一般在/etc/profile.   $  [HOME]/.bash_profile  或  $[HOME]./profile  中设置umask  值

永久有效L编辑用户的配置文件vim  .bash_profile 

临时生效:umask 权限补码

umask  044  

权限的算法:一般情况下:目录默认权限-umask  值

666-022=644

777-022=755

这是一个好的记忆方法,但是不严谨






[root@xueshen65 ~]# ll /usr/bin/passwd

-rwsr-xr-x. 1 root root 27832 6月  10 2014 /usr/bin/passwd

[root@xueshen65 ~]# chmod u+s /usr/bin/le

less        lessecho    lesskey      lesspipe.sh  lex          lexgrog     

[root@xueshen65 ~]# chmod u+s /usr/bin/less     切换到root  ,给一个suid  权限

[root@xueshen65 ~]# ps -aux | grep less

root      12788  0.0  0.0 112724  984 pts/0    S+  16:57  0:00 grep --color=auto less


SGID 

限定:既可以给二进制可执行程序设置,也可以给目录设置

功能:在设置了SGID权限的目录下建立文件,新创建的所属文件级车还给你发上级目录的权限

[root@xueshen65 ~]# mkdir test

[root@xueshen65 ~]# ll -d test

drwxr-xr-x. 2 root root 6 3月  26 17:05 test

[root@xueshen65 ~]# chmod g+s test

[root@xueshen65 ~]# !ll

ll -d test

drwxr-sr-x. 2 root root 6 3月  26 17:05 test

[root@xueshen65 ~]# chown  :bin test

[root@xueshen65 ~]# touch test/a.txt 

[root@xueshen65 ~]# ll test/

总用量 0

-rw-r--r--. 1 root bin 0 3月  26 17:06 a.txt


Stickybit  

限定:只作用于目录

功能:目目录下创的文件只有root,文件创建者,目录所有者才能删除

例子:系统中tmp 目录就是这样

[root@xueshen65 ~]# ll -d /tmp/

drwxrwxrwt. 35 root root 4096 3月  26 17:12 /tmp/

[root@xueshen65 ~]# chmod o+t /opt/

[root@xueshen65 ~]# chmod /opt/ -d 

chmod:无效选项 -- d

Try 'chmod --help' for more information.

[root@xueshen65 ~]# ll /opt/ -d 

drwxr-xr-t. 3 root root 147 3月  25 10:16 /opt/

7.2.1  文件扩展权限ACL 

扩展ACL  :  access control  list

例:  设置用户centos对文件a.txt.  拥有rwx  权限  ,centos不属于a.txt 怎么做

[root@xueshen65 ~]# touch /tmp/a.txt

[root@xueshen65 ~]# getfacl  /tmp/a.txt

getfacl: Removing leading '/' from absolute path names

# file: tmp/a.txt

# owner: root

# group: root

user::rw-

group::r--

other::r--

[root@xueshen65 ~]# setfacl  -m u:centos rwx /tmp/a.txt

setfacl: Option -m incomplete

[root@xueshen65 ~]# setfacl  -m u:centos:rwx /tmp/a.txt

[root@xueshen65 ~]# ll /tmp/a.txt

-rw-rwxr--+ 1 root root 0 3月  26 17:17 /tmp/a.txt

然后就可以用centos写入  /tmp/a.txt  


7.3  实战:创建一个root都无法删除的文件



©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容