LINUX文件的权限

一.权限设定的意义:系统最底层安全设定方法之,保证文件可以被可用的用户做相应操作。

二.文件权限的查看(alias)

命令:ls

     ls -l file       ## 查看文件属性

     ls -ld mkdir      ## 查看目录属性

     ll file         ## 也可查看文件属性

     ll -d dir        ## 查看目录属性

示例:文件目录属性的查看

[root@localhost mnt]# ls
file  niu

[root@localhost mnt]# mkdir niu

[root@localhost mnt]# ls -ld niu/
drwxr-xr-x 2 root root 6 Jul 25 10:16 niu/
[root@localhost mnt]# ls -l file
-rw-rw-r--+ 1 root root 13 Jul 25 10:06 file 

三.文件权限的读取(8列)

格式: -  |  rm-rw-r--  | 1 |  kiosk  |  kiosk  |  0  | Jul 21 09:18  |  file

  [1]第一列为文件的类型

    -       ## 表示空文件或者是文本

    d         ##表示目录

    l       ##表示软链接(快捷方式的文件为软链接)

    s        ##socket套接字(对外提供交互接口)    

    b        ##block块设备(ls -l /dev/sda插入u盘显示)

    c         ##字符设备(/dev/pts/0)

  [2]第二列表示文件的权限

    rw-rw-r--    ##读写执行操作

    注释:前三个【rw-】表示文件拥有者(user)对文件能做读和写操作

       中间三位【rw-】表示文件所有组(group)对文件能做的读写操作

       最后三位【r--】表示其他人(other)对文件能做读操作

  [3]第三列表示文件硬链接个数

  •   对文件内容被记录的个数(ls -li file查看文件的节点号)
  •   对目录表示目录中子目录的个数,不会记录文件的个数

  [4]第四列表示文件的所有人

  [5]第五列表示文件所有组

  [6]第六列表示文件的大小 

  •      对于文件来说是大小
  •      对于目录是目录中子文件元数据(matedate可理解为文件的属性)的大小

  注意:计算大小的时候: -|rm-rw-r--| 1 |kiosk|kiosk|0| Jul 21 09:18 | file 中含有字节数为11

            (文件名中一个字母为一个字节)

  [7]第七列为文件内容被修改的日期

  [8]为文件的名称

  【1】【3】【6】【7】【8】不能改

四.改变文件的所有人和所有组

命令:chown|chgrp

     chown username file|dir    ##改变文件或者目录的所有人

     chown user.group file|dir     ##改变文件或者目录的所有组

     chown -R user.group dir     ##改变目录及子目录的所有人

     chgrp group file|dir      ## 改变文件或目录的所有组

     chgrp -R group dir       ## 改变目录及子目录的所有组 

实例: 修改目录的用户所有人为student 

[root@localhost Desktop]# mkdir niu
[root@localhost Desktop]# ls -ld niu/
drwxr-xr-x 2 root root 6 Jul 25 08:53 niu/
[root@localhost Desktop]# chown student niu/
[root@localhost Desktop]# ls -ld niu/
drwxr-xr-x 2 student root 6 Jul 25 08:53 niu/

 

修改文件的所有人和所有组:

[root@localhost Desktop]# touch file
[root@localhost Desktop]# ls -l file
-rw-r--r-- 1 root root 0 Jul 25 09:00 file
[root@localhost Desktop]# chown student.westos file
[root@localhost Desktop]# ls -l file
-rw-r--r-- 1 student westos 0 Jul 25 09:00 file

 

修改目录组的及其子目录的组:

[root@localhost Desktop]# touch niu/file{1..3}
[root@localhost Desktop]# ls -Rl niu/
niu/:
total 0
-rw-r--r-- 1 root root 0 Jul 25 09:04 file1
-rw-r--r-- 1 root root 0 Jul 25 09:04 file2
-rw-r--r-- 1 root root 0 Jul 25 09:04 file3
[root@localhost Desktop]# ls -ld niu/
drwxr-xr-x 2 root root 42 Jul 25 09:04 niu/
[root@localhost Desktop]# chgrp -R student  niu/
[root@localhost Desktop]# ls -lr niu/
total 0
-rw-r--r-- 1 root student 0 Jul 25 09:04 file3
-rw-r--r-- 1 root student 0 Jul 25 09:04 file2
-rw-r--r-- 1 root student 0 Jul 25 09:04 file1

  注意:当改变目录属性时,不会改变他目录下的东西;若要改子目录时,使用-R来递归修改

五.如何改变文件的权限

【1】对权限的理解

    r     ##对文件:是否可以查看文件中的内容 ------>cat file

            对目录:是否可以查看目录中有什么子文件或子目录 ——-->ls dir

    w     ##对文件:是否可以改变文件里面记录的字符

          对目录:是否可以对目录中子目录或子文件的元数据进行更改

    x      ##对文件:是否可以通过文件名称调用文件内记录的程序(执行cd;查看ls)

             对目录:是否可以进入目录
【2】更改方式  

 格式:chmod <u|g|o><+|-|=><r|w|x> file|dir

 实例:chomd u+x  /mnt/file1  

       chomd g-r /mnt/file2

       chomd ug-r /mnt/file3

       chomd u-r,g+x /mnt/file4

       chomd -r /mnt/file5(只有w特殊;若为-+w,则只有user减加;否则全加)

       chomd o=r-x /mnt/file6 ##修改o为r-x加的时候,x是有问题

 把file1的权限改为rwxr-xr-x:

[root@localhost niu]# ls -l
total 0
-rw-r--r-- 1 root student 0 Jul 25 09:04 file1
-rw-r--r-- 1 root student 0 Jul 25 09:04 file2
-rw-r--r-- 1 root student 0 Jul 25 09:04 file3
[root@localhost niu]# chmod ugo+x file1
[root@localhost niu]# ls -l file1
-rwxr-xr-x 1 root student 0 Jul 25 09:04 file1

 

另一种更改方式:rwx (以二进制的方式表示:r=4,w=2,x=1)

例子:r-x|r--|--x(541)把文件file2的属性修改为541

[root@localhost niu]# ls -l file2
-rw-r--r-- 1 root student 0 Jul 25 09:04 file2
[root@localhost niu]# chmod 541 file2
[root@localhost niu]# ls -l file2
-r-xr----x 1 root student 0 Jul 25 09:04 file2    

 

六.umask的使用

含义: 系统建立文件时默认保留的权力

命令 : umask 查看

修改: umask 077 临时设定系统预留权限为077(关闭窗口会复原)

永久更改步骤: vim /etc/profile --->修改/etc/profile 系统配置文件62行,把umask值改为077 ------> wq保存退出

          vim  /etc/bashrc-->修改/etc/bashrc shell配置文件73行,把umask值改为077------>wq保存退出

           source /etc/profile  ##重新加载配置文件

         source /etc/bashrc

七、特殊权限

【1】sticky    ##粘制位(对于其他用户的限制)

   作用:只针对目录生效,当一个目录上有sticky权限时,在这个目录中的文件只能被文件的所有者删除
  设定方式:chmod o+t dir

       chmod 1XXX dir

 示例: 当没有设置时,在westos用户下建立文件file,然后切换到sutent用户下删除file

[root@localhost mnt]# mkdir niu
[root@localhost mnt]# chmod 777 niu/
[root@localhost mnt]# ls -ld niu/
drwxrwxrwx 2 root root 6 Jul 25 09:21 niu/
[root@localhost mnt]# su - westos
[westos@localhost ~]$ cd /mnt/niu/
[westos@localhost niu]$ touch file
[westos@localhost niu]$ exit
[student@localhost ~]$ cd /mnt/niu/
[student@localhost niu]$ rm -rf file

 

设置之后,student用户不可以对file文件进行删除

[root@localhost mnt]# chmod 1777 niu/
[root@localhost mnt]# ls -ld niu/
drwxrwxrwt 2 root root 6 Jul 25 09:24 niu/
[root@localhost mnt]# su - westos
Last login: Wed Jul 25 09:22:44 EDT 2018 on pts/0
[westos@localhost ~]$ cd /mnt/niu/
[westos@localhost niu]$ touch file
[westos@localhost niu]$ exit
logout
[root@localhost mnt]# su - student
Last login: Wed Jul 25 09:24:08 EDT 2018 on pts/0
[student@localhost ~]$ cd /mnt/niu/
[student@localhost niu]$ ls
file
[student@localhost niu]$ rm -rf file
rm: cannot remove ‘file’: Operation not permitted 

 

【2】sgid     ##强制位(默认情况下,用户执行文件产生的进程是属于该用户的,与文件的属性无关)

  作用: 对于二进制可执行文件,若有sigd标志,则任何人执行此文件时产生的进程都属于文件的所有组

       对于目录:若有sgid时,任何人在此目录中建立的文件都属于目录的所有组

  设置方式:chmod g+s file|dir

       chmod 2XXX file|dir

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

//修改sgid之后结果如下:

[root@localhost Desktop]# chmod g+s /bin/watch
[root@localhost Desktop]# chgrp westos /bin/watch
[root@localhost Desktop]# ls -l /bin/watch
-rwxr-sr-x. 1 root westos 24704 Feb 27  2014 /bin/watch
[student@localhost Desktop]# watch -n 1 tail file 

 

【3】suid     ##冒险位(权力的提升和下降)

  作用:只针对二进制可执行文件,当文件上有suid时,任何人执行这个文件中所的进程均属于文件的所有人

  设置方式:chmod u+s file|dir
       chmod 4XXX file|dir

  (任何人在运行这条命令时成为这个用户)

[root@localhost Desktop]# chown westos /bin/watch
[root@localhost Desktop]# chmod u+s /bin/watch
[root@localhost Desktop]# ls -l /bin/watch
-rwsr-xr-x. 1 westos root 24704 Feb 27  2014 /bin/watch
[root@localhost Desktop]# su - student
Last login: Wed Jul 25 11:41:06 EDT 2018 on pts/1
[student@localhost ~]$ watch -n 1 tail /etc/passwd

八.权限列表

【1】作用:让特定的用户对特定的文件拥有特定权限

【2】命令:acl  ##进行列表的查看 -rw-rwxr--+ 1 root root 3 Jul 21 15:45 file

    getfacl file   ## 查看acl开启的文件权限

    #file:file    ##文件名称

【3】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

示例:使用户student拥有rwx的权力,可以对文件进行修改
[root@localhost mnt]# ls -l file
-rw-r--r-- 1 root root 7 Jul 25 09:36 file
[root@localhost mnt]# setfacl -m u:student:rwx file
[root@localhost mnt]# ls -l file
-rw-rwxr--+ 1 root root 7 Jul 25 09:36 file

[root@localhost mnt]# getfacl file

[root@localhost mnt]# su - stutent 
[student@localhost ~]$ cd /mnt/
[student@localhost mnt]$ vim file

【4】mask值列表

    在权限列表中mask表示能生效的权力值

    当用chmod减小开启acl的文件权限时mask值会发生改变

    chmod g-r file

    如果要恢复:setfacl -m m:rw file

示例:权限修改减小之后,使mask出错

[root@localhost mnt]# ls
file  niu
[root@localhost mnt]# chmod 774 file
[root@localhost mnt]# ls -l file
-rwxrwxr--+ 1 root root 13 Jul 25 10:06 file
[root@localhost mnt]# getfacl file
# file: file
# owner: root
# group: root
user::rwx
user:student:rwx
group::r--
mask::rwx
other::r--
//修改之后,mask会出错
[root@localhost mnt]# chmod g-wr file
[root@localhost mnt]# getfacl file
# file: file
# owner: root
# group: root
user::rwx
user:student:rwx        #effective:--x
group::r--            #effective:---
mask::--x
other::r--
//对文件权限进行恢复
[root@localhost mnt]# setfacl -m m:rwx file
[root@localhost mnt]# getfacl file
# file: file
# owner: root
# group: root
user::rwx
user:student:rwx
group::r--
mask::rwx
other::r-- 

【5】acl默认权限设定

    针对目录设定(在mnt下创建文件,先不设定默认,在root中westos下添加新目录niu后,不会在studentd下的westos/niu中进行操作;设置后即可)

    针对设定完成之后新建立的文件或目录生效,已存在的文件不会继承默认权限

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

示例: 

[root@localhost mnt]# mkdir niu
[root@localhost mnt]# chmod 774 niu/
[root@localhost mnt]# ls -ld niu/
drwxrwxr-- 2 root root 6 Jul 25 10:41 niu/
[root@localhost mnt]# setfacl -m u:student:rwx niu/
[root@localhost mnt]# getfacl niu/
# file: niu/
# owner: root
# group: root
user::rwx
user:student:rwx
group::rwx
mask::rwx
other::r--
//在student用户下添加文件,文件是没有特殊权限的
[root@localhost mnt]# su - student
Last login: Wed Jul 25 10:39:25 EDT 2018 on pts/0
[student@localhost ~]$ cd /mnt/
[student@localhost mnt]$ touch niu/file{1..2}
[student@localhost mnt]$ ls -l niu/
total 0
-rw-rw-r-- 1 student student 0 Jul 25 10:42 file1
-rw-rw-r-- 1 student student 0 Jul 25 10:42 file2
//设置文件的默认权限
[root@localhost mnt]# setfacl -m d:u:student:rwx niu/
[root@localhost mnt]# getfacl niu/
# file: niu/
# owner: root
# group: root
user::rwx
user:student:rwx
group::rwx
mask::rwx
other::r--
default:user::rwx
default:user:student:rwx
default:group::rwx
default:mask::rwx
default:other::r--
//在sudent用户下建立的文件file{1..3}是有特殊权限的
[root@localhost mnt]# su - student
Last login: Wed Jul 25 10:42:16 EDT 2018 on pts/0
[student@localhost ~]$ touch /mnt/niu/file{3..5}
[student@localhost ~]$ ls -l /mnt/niu/
total 0
-rw-rw-r--  1 student student 0 Jul 25 10:42 file1
-rw-rw-r--  1 student student 0 Jul 25 10:42 file2
-rw-rw-r--+ 1 student student 0 Jul 25 10:45 file3
-rw-rw-r--+ 1 student student 0 Jul 25 10:45 file4
-rw-rw-r--+ 1 student student 0 Jul 25 10:45 file5

 

 

 

posted @ 2018-07-25 19:03  UTHN_B  阅读(708)  评论(0编辑  收藏  举报