linux下文件权限管理 ls chown chgrp chmod acl列表

对文件权限的更改与管理

1、文件属性的查看

ls -l filemane
d|rw-r--r--.|3|root|root| 6 |Oct 1 05:03|dirname

第一列:文件的类型

  • -##普通文件

    d ##目录

    c ##字符设备(只能一个字节一个字节读写的设备,不能随机读取设备内存中的某一数据,常见设备如鼠标,键盘)

    s##套接字(虚拟接口,用于通信)

    p ##管道

    b ##块设备(可以从设备的任意位置读取一定长度数据的设备,如硬盘,u盘)

    l ##链接

第二列:文件读写权限

   rw-|r--|r--     ##三个为一组

r:对文件:可以查看文件中的字符;对目录:可以查看目录中文件的信息

w:对文件:可以更改文件内字符;对目录:可以在目录中添加删除文件

x:对文件:可以运行文件内记录的程序动作;对目录:可以进入目录中

前三个是所有人的权限 中间三个是所有组的权限 后三个是其他人的权限

第三列:

对文件:文件内容被系统记录的次数

对目录:目录中文件属性的字节数

第四列:

“root”:文件所有人

第五列:

“root”:文件所有组

第六列:

“6”:文件内容的大小

第七列:

“Oct 1 05:03”:文件最后一次被修改时间

第八列:

“dirname”:文件名

2、文件所有人所有组的管理

chown username file|dir ##更改文件的所有人

chgrp groupname file|dir ##更改文件的所有组

chown username.groupname file|dir ##更改所有人所有组

chown -R username dir ##更改目录本身及里面所有内容的所有人

chgrp -R groupname dir ##更改目录本身及里面所有内容的所有组

watch -n 1 ls -lR /mnt   ##监控命令

3、字符方式修改该文件权限 —-chmod

chmod u-x file    ##file拥有者去掉x权限

chmod g+w file   ##file拥有组添加w权限

chmod u-x,g+w file   ##file拥有着去掉x权,file拥有组添加w权限

chmod ugo-r file  ##file的用户组其他人去掉r权限

chmod ug+x,o-r file  ##file用户和组添加x权限,其他人去掉r权限

4、数字方式修改文件权限

r=4 , w=2 ,x=1

7=rwx 6=rw- 5=r-x 4=r–

3=-wx 2=-w- 1=–x 0=—

chmod 修改后权限值 file

chmod 777 file

5、系统默认权限的设定

**从系统存在角度来说,开放权力越大,系统存在意义越高

**从系统安全角度来说,开放权力越少,系统安全性越高

**所以系统设定新建文件或目录会去掉一些权限

1~~~~~设定方式

umask       ##查看系统保留权限,默认为022

umask 077   ##修改该系统保留权限为077,此设定为临时设定,只当前shell中生效

默认:

  • 目录 755 ##umask 0022 默认建立文件时所保留权限 022

    文件 644 ##内核拿走了111

2~~~~永久设定方式

vim /etc/bashrc   ##shell配置文件

vim /etc/profile  ##系统配置文件   ##两个必须一致


source /etc/profile

source /etc/bashrc    ##重新读取,让设定立即生效

6、文件的访问控制(acl列表)

1.acl定义

acl = accsee control

指定特殊用户对特殊文件有特殊权限

drwxrwx---+ 2 root root 17 Jul 18 01:39 /westos/

+表示acl开启
当acl权限开启时,用ls -lR看到的这些权限都会失效,需要用getfacl查看

getfacl /westos/##打开权限列表

#file:westos/ ##文件名称

#owner:root ##文件所有人

#group:root ##文件所有组

user:rwx ##拥有者权限

user:student:rwx ##特殊用户权限

group::— ##组权限

mask::rwx ##权限掩码

other::— ##其他人权限

2.设定acl列表

设定

setfacl  -m u:username:rwx file	  # 设定username对file拥有rwx权限
setfacl  -m g:group:rwx	file	  # 设定group组成员对file拥有rwx权限
setfacl  -x u:username	file	  # 从acl列表中删除username
setfacl  -b file		  # 关闭file上的acl列表	

acl的默认权限设定

setfacl -m d:u:student:rwx /mnt/westos    
        ##设定在westos目录中新建文件对student用户有rwx权限
          ##对已经存在的文件无效
             ##对目录本身无效

setfacl  -m u:student:rwx  /mnt/test/    
        ##student用户对此目录有rwx权限

setfacl  -Rm u:student:rwx /mnt/test /        
        ##student用户对此目录下已有存在的文件有rwx权限若此时新建文件不会对新建的生效

[root@localhost Desktop]# mkdir /mnt/test
[root@localhost Desktop]# chmod 775 /mnt/test
[root@localhost Desktop]# touch /mnt/test/file
[root@localhost Desktop]# setfacl -m u:student:rwx /mnt/test
[root@localhost Desktop]# ls -lR /mnt
-rw-rwx—+ 1 root root 0 Oct 2 03:27 file
drwxrwxr-x+ 2 root root 17 Oct 2 04:07 test
[root@localhost Desktop]# getfacl /mnt/test
getfacl: Removing leading ‘/’ from absolute path names
#file: mnt/test
#owner: root
#group: root
user::rwx
user:student:rwx
group::rwx
mask::rwx
other::r-x
[root@localhost Desktop]# su – student
[student@localhost ~]$ touch /mnt/test/file1
[student@desktop ~]$ ls -lR /mnt
[root@localhost Desktop]# mkdir /mnt/test/linux
[root@localhost Desktop]# cd /mnt/test/
[root@localhost test]# ls file linux
[root@localhost test]# setfacl -m d:u:student:rwx /mnt/test
[root@localhost test]# mkdir /mnt/test/hello
[root@localhost test]# getfacl /mnt/test/hello/
getfacl: Removing leading ‘/’ from absolute path names
# file: mnt/test/hello/
# owner: root
# group: root

#默认权限
user::rwx
user:student:rwx
group::rwx mask::rwx
other::r-x

#hello目录继承了test的权限,但继承权限的前提是,默认权限必须存在,继承的权限才生效
default:user::rwx
default:user:student:rwx
default:group::rwx
default:masK::rwx
default:other::r-x
[root@localhost test]# setfacl -x u:student /mnt/test

删除列表中的用户或者组

  setfacl -x <u|g>:<username|groupname> 文件|目录

关闭列表

 setfacl -b 文件|目录

acl mask值

mask权限掩码

mask用来标识能够赋予用户最大权限

当用chmod改变文件普通权限时可能会被破坏

修复

setfacl -m m:rwx 文件名称

setfacl -m d:u:student:rwx /westos

setfacl -m u:student: rw /mnt/file

6、特殊权限
1————————————–

    t 权限 :
    
    sticky ##粘制位

!!!只针对目录,当一个目录上有 t 权限,那么目录中的文件只能被文件的拥有者删除

1~~~~~设定方式

   chmod o+t directory

t=1,也可写成:

   chmod 1xxx file|dir 

[root@desktop Desktop]# mkdir /westos
[root@desktop Desktop]# ls -ld /westos/
drwx—— 2 root root 6 Sep 28 11:52 /westos/
[root@desktop Desktop]# chmod 777 /westos/ # 给westos目录赋予最大权限
[root@desktop Desktop]# ls -ld /westos/
drwxrwxrwx 2 root root 6 Sep 28 11:52 /westos/
[root@desktop Desktop]# su – student
Last login: Fri Sep 28 11:52:20 EDT 2018 on pts/0
[student@desktop ~]$ cd /westos/
[student@desktop westos]$ ls
[student@desktop westos]$ touch studentfile # 以student用户身份在westos目录下建立文件
[student@desktop westos]$ ls
studentfile
[student@desktop westos]$ logout
[root@desktop Desktop]# su – haha
Last login: Fri Sep 28 11:49:21 EDT 2018 on pts/0
[haha@desktop ~]$ cd /westos/
[haha@desktop westos]$ lsstudentfile
[haha@desktop westos]$ touch westosfile # 以haha用户身份在westos目录下建立文件
[haha@desktop westos]$ ls /westos/studentfile westosfile
[haha@desktop westos]$ rm -fr studentfile # 无论在什么用户下都可以删除不属于自己建立的文件
[haha@desktop westos]$ ls
westosfile
[haha@desktop westos]$ rm -fr westosfile
[haha@desktop westos]$ ls
[haha@desktop westos]$ logout
[root@desktop Desktop]# chmod 1777 /westos/ # 给目录赋予t权限
[root@desktop Desktop]# su – student
Last login: Fri Sep 28 11:53:02 EDT 2018 on pts/0
[student@desktop ~]$ cd /westos/
[student@desktop westos]$ ls
[student@desktop westos]$ touch studentfile
[student@desktop westos]$ ls
studentfile
[student@desktop westos]$ logout
[root@desktop Desktop]# su – haha
Last login: Fri Sep 28 11:53:58 EDT 2018 on pts/0
[haha@desktop ~]$ cd /westos/
[haha@desktop westos]$ ls
studentfile
[haha@desktop westos]$ touch westosfile
[haha@desktop westos]$ ls
studentfile westosfile
[haha@desktop westos]$ rm -fr studentfile # 其他用户不能删除不属于自己建立的文件
rm: cannot remove ‘studentfile’: Operation not permitted
[haha@desktop westos]$ rm -fr westosfile # 可以删除自己建立的文件
[haha@desktop westos]$ ls
studentfile

2—————————————

   s 所有组权限:    
   sgid ##强制位

对文件:只针对二进制可执行文件,
任何人运行二进制文件程序时,程序产生的进程的所有组都是文件的所有组,

和程序发起人组的身份无关

设定方式

chmod g+s file|dir

sgid=2

chmod 2xxx file|dir

[root@localhost ~]# touch file
[root@localhost ~]# ll file
-rw-r–r– 1 root root 0 Oct 1 23:50 file
[root@localhost ~]# su – student
Last login: Mon Oct 1 23:29:19 EDT 2018 on pts/1
[student@localhost ~]$ touch file1
[student@localhost ~]$ ll file1 # 谁在执行这个进程,进程就属于谁,即studet用户建立的文件属于student用户
-rw-rw-r– 1 student student 0 Oct 1 23:51 file1
[student@localhost ~]$ logout
[root@localhost ~]# chgrp student /bin/touch
[root@localhost ~]# chmod g+s /bin/touch # 对touch这个进程加s的权限,使得所有用户执行的进程都属于student用户,这叫权力下放
[root@localhost ~]# ls -l /bin/touch
-rwxr-sr-x. 1 root student 62432 Jan 24 2014 /bin/touch
[root@localhost ~]# touch file3
[root@localhost ~]# ll file3
-rw-r–r– 1 root student 0 Oct 1 23:56 file3
[root@localhost ~]# su – tom
Last login: Mon Oct 1 23:34:38 EDT 2018 on pts/1
[tom@localhost ~]$ touch file5
[tom@localhost ~]$ ll file5
-rw-rw-r– 1 tom student 0 Oct 1 23:58 file5

3——————————————

   s 所有用户权限:
    
   suid ##冒险位

只针对二进制可执行文件

文件内记录的程序产生的进程的所有人为文件所有人

和进程发起人身份无关

设定方式

chmod u+s file

suid=4

chmod 4xxx file

[root@localhost Desktop]# ls -l /bin/watch
-rwxr-xr-x. 1 root root 24704 Feb 27 2014 /bin/watch

#重新打开一个shell登录student用户
[root@localhost Desktop]# su – student
Last login: Mon Oct 1 23:57:43 EDT 2018 on pts/1
[student@localhost ~]$ watch -n 1 date

#在root用户上
[root@localhost Desktop]# ps ax -o comm,user,group | grep watch
watchdog/0 root root
abrt-watch-log root root
abrt-watch-log root root
watch student student
[root@localhost Desktop]# chmod g+s /bin/watch # 给watch赋予g+s权限
[root@localhost Desktop]# ls -l /bin/watch # 可以看到g上多了一个s权限
-rwxr-sr-x. 1 root root 24704 Feb 27 2014 /bin/watch

#在student用户上
[student@localhost ~]$ watch -n 1 date

#在root用户上
[root@localhost Desktop]# ps ax -o comm,user,group | grep watch # 此时watch的所有人改变为student
watchdog/0 root root
abrt-watch-log root root
abrt-watch-log root root
watch student root
[root@localhost Desktop]# chmod u+s /bin/watch # 给watch赋予u+s权限
[root@localhost Desktop]# ls -l /bin/watch # 可以看到u上多了一个s权限
-rwsr-sr-x. 1 root root 24704 Feb 27 2014 /bin/watch

#在student用户上
[student@localhost ~]$ watch -n 1 date

#在root用户上
[root@localhost Desktop]# ps ax -o comm,user,group | grep watch #
watchdog/0 root root
abrt-watch-log root root
abrt-watch-log root root
watch root root

#练习sudo原理
#suid+sgid的结合:实现当目录的所在组对文件没有w权限时,不能删除目录里的文件

[root@localhost Desktop]# useradd -s /sbin/nologin norm # 建立一个系统用户
[root@localhost Desktop]# ls -l /bin/rm
-rwxr-xr-x. 1 root root 62808 Jan 24 2014 /bin/rm
[root@localhost Desktop]# chown norm.norm /bin/rm # 使rm命令属于系统用户
[root@localhost Desktop]# ls -l /bin/rm
-rwxr-xr-x. 1 norm norm 62808 Jan 24 2014 /bin/rm
[root@localhost Desktop]# chmod 6755 /bin/rm #给rm命令赋予冒险位和强制位权限
[root@localhost Desktop]# ls -l /bin/rm
-rwsr-sr-x. 1 norm norm 62808 Jan 24 2014 /bin/rm
[root@localhost Desktop]# chmod 777 /mnt
[root@localhost Desktop]# ls -ld /mnt
drwxrwxrwx. 2 root root 6 Oct 2 02:39 /mnt
[root@localhost Desktop]# chmod 775 /mnt
[root@localhost Desktop]# ls -ld /mnt
drwxr-xr-x. 2 root root 17 Oct 2 03:11 /mnt
[root@localhost Desktop]# touch /mnt/file
[root@localhost Desktop]# ls /mntfile
[root@localhost Desktop]# rm -fr /mnt/file
rm: cannot remove ‘/mnt/file’: Permission denied

#做完实验记得还原环境
[root@localhost Desktop]# chown root.root /bin/rm
[root@localhost Desktop]# ls -l /bin/rm
-rwxr-xr-x. 1 root root 62808 Jan 24 2014 /bin/rm

    原文作者:若无其事的苹果
    原文地址: https://blog.csdn.net/qq_36016375/article/details/88904012
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞