• Linux-文件权限


    Linux文件权限管理

    基本权限 UGO

    文件权限设置: 可以赋于某个用户或组 能够以何种方式 访问某个文件

    文件权限管理之: UGO设置基本权限(r、w、x)
    
    rw-r--r-- alice hr install.log
    权限对象:
    属主:         u
    属组:         g
    其他人:       o
    
    权限类型:
    读:r          4
    写:w         2
    执行: x        1

    ==设置权限

    1. 更改文件是属主、属组

    =chown:
    [root@localhost ~]# chown alice.hr file1              //改属主、属组
    [root@localhost ~]# chown alice     file1             //只改属主
    [root@localhost ~]# chown        .hr file1            //只改属组
    =chgrp:
    [root@localhost ~]# chgrp it file1                    //改文件属组
    [root@localhost ~]# chgrp -R it dir1                  //改文件属组

    2. 更改权限

    =a. 使用符号
                        对象        赋值符        权限类型
                        u                +                r
    chmod             g                 -                w          file1
                        o                =                x
                        a
    [root@localhost ~]# chmod u+x file1                 //属主增加执行
    [root@localhost ~]# chmod a=rwx file1               //所有人等于读写执行
    [root@localhost ~]# chmod a=- file1                 //所有人没有权限
    [root@localhost ~]# chmod ug=rw,o=r file1           //属主属组等于读写,其他人只读
    [root@localhost ~]# ll file1                        //以长模式方式查看文件权限
    -rw-rw-r-- 1 alice it 17 10-25 16:45 file1          //显示的结果
    
    =b. 使用数字
    [root@localhost ~]# chmod 644 file1
    [root@localhost ~]# ll file1
    -rw-r--r-- 1 alice it 17 10-25 16:45 file1

    ===设置权限示例

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

    [root@localhost ~]# groupadd hr
    [root@localhost ~]# useradd hr01 -G hr
    [root@localhost ~]# useradd hr02 -G hr
    [root@localhost ~]# mkdir /home/hr
    
    [root@localhost ~]# chgrp hr /home/hr
    [root@localhost ~]# chmod 770 /home/hr
    [root@localhost ~]# ll -d /home/hr/
    drwxrwx---. 2 root hr 4096 3月  13 14:26 /home/hr/

    重要: r、w、x权限对文件和目录的意义

    示例1: 对文件的影响

    [root@localhost ~]# mkdir /dir10
    [root@localhost ~]# touch /dir10/file1
    [root@localhost ~]# chmod 777 /dir10/file1 
    
    [root@localhost ~]# ll -d /dir10/
    drwxr-xr-x. 2 root root 4096 3月  11 18:37 /dir10/
    [root@localhost ~]# ll /dir10/file1 
    -rwxrwxrwx. 1 root root 0 3月  11 18:37 /dir10/file1
    
    [alice@tianyun ~]$ cat /dir10/file1 
    [alice@tianyun ~]$ rm -rf /dir10/file1 
    rm: 无法删除"/dir10/file1": 权限不够

    示例2: 对目录有w权限

    [root@localhost ~]# chmod 777 /dir10/
    [root@localhost ~]# chmod 000 /dir10/file1 
    [root@localhost ~]# ll -d /dir10/
    drwxrwxrwx. 2 root root 4096 3月  11 18:37 /dir10/
    [root@localhost ~]# ll /dir10/file1 
    ----------. 1 root root 0 3月  11 18:37 /dir10/file1
    
    [alice@tianyun ~]$ cat /dir10/file1 
    cat: /dir10/file1: 权限不够
    [alice@tianyun ~]$ rm -rf /dir10/file1 
    [alice@tianyun ~]$ touch /dir10/file2

    问题1:

    [root@localhost ~]# ll /root/install.log
    -rw-r--r--. 1 root root 46571 6月   1 23:37 /root/install.log
    [alice@tianyun ~]$ cat /root/install.log
    cat: /root/install.log: 权限不够

    问题2: alice能删除/下的任何文件吗?

    [root@localhost ~]# chmod 777 /
    [root@localhost ~]# ll -d /
    drwxrwxrwx. 27 root root 4096 6月   4 11:32 /
    [alice@tianyun ~]$ rm -rf /etc

    再次认识一下文件和目录:

    基本权限 ACL

    文件权限管理之: ACL设置基本权限(r、w、x)
    UGO设置基本权限: 只能一个用户,一个组和其他人
    ACL 设置基本权限: r,w,x

    =ACL基本用法=

    设置:
    [root@localhost ~]# touch /home/test.txt
    [root@localhost ~]# ll /home/test.txt    
    -rw-r--r-- 1 root root 0 10-26 13:59 /home/test.txt
    
    [root@localhost ~]# getfacl /home/test.txt
    [root@localhost ~]# setfacl -m u:alice:rw /home/test.txt              //增加用户alice权限
    [root@localhost ~]# setfacl -m u:jack:- /home/test.txt                //增加用户jack权限
    [root@localhost ~]# setfacl -m o::rw /home/test.txt
    
    查看/删除:
    [root@localhost ~]# ll /home/test.txt 
    -rw-rw-r--+ 1 root root 0 10-26 13:59 /home/test.txt
    [root@localhost ~]# getfacl /home/test.txt
    
    [root@localhost ~]# setfacl -m g:hr:r /home/test.txt
    [root@localhost ~]# setfacl -x g:hr /home/test.txt                     //删除组hr的acl权限
    [root@localhost ~]# setfacl -b /home/test.txt                          //删除所有acl权限
    

    =查看帮助=

    [root@localhost ~]# man setfacl
    /EXAMPLES
    [root@localhost ~]# getfacl file1 |setfacl  --set-file=- file2       //复制file1的ACL权限给file2

    =ACL高级用法=

    mask:
    用于临时降低用户或组(除属主和其他人)的权限
    建议:为了方便管理文件权限,其他人的权限置为空
    [root@localhost ~]# setfacl -m m::--- /home/file100.txt
    
    
    default: 继承(默认)
    要求: 希望alice能够对/home以及以后在/home下新建的文件有读、写、执行权限
    
    思路:
    步骤一: 赋予alice对/home读、写、执行权限
    [root@localhost ~]# setfacl -m u:alice:rwx /home
    
    步骤二: 赋予alice对以后在/home下新建的文件有读、写、执行权限 (使alice的权限继承)
    [root@localhost ~]# setfacl -m d:u:alice:rwx /home

    高级权限 suid,sgid,sticky

    问题1: 为什么会失败!

    [root@localhost ~]# ll /root/install.log
    -rw-r--r--. 1 root root 46571 6月   1 23:37 /root/install.log
    [alice@tianyun ~]$ cat /root/install.log
    cat: /root/install.log: 权限不够
    
    分析:
    alice           /usr/bin/cat (alice)            /root/install.log
    alice           /usr/bin/passwd (root)      /etc/shadow

    高级权限的类型
    suid 4
    sgid 2
    sticky 1 粘滞位

    设置特殊权限
    a、字符
    chmod u+s file
    chmod g+s file
    chmod g+s dir
    chmod o+t dir

    b、数字
    chmod 4777 file
    chmod 7777 file
    chmod 2770 dir
    chmod 3770 dir

    示例1:suid 普通用户通过suid提权 <针对文件>

    在进程文件(二进制,可执行)上增加suid权限
    [root@localhost ~]# chmod u+s /bin/cat
    [root@localhost ~]# chmod u+s /bin/rm
    [alice@tianyun ~]$ cat /root/install.log
    普通用户可以修改密码:
    alice           /usr/bin/passwd      /etc/shadow
    
    [alice@tianyun ~]$ ll /etc/shadow
    ---------- 1 root root 1487 6月   4 13:43 /etc/shadow
    
    [alice@tianyun ~]$ ll /usr/bin/passwd
    -rwsr-xr-x. 1 root root 30768 2月  17 2012 /usr/bin/passwd
    
    [alice@tianyun ~]$ passwd 
    更改用户 alice 的密码 。
    为 alice 更改 STRESS 密码。
    (当前)UNIX 密码:
    
    [root@localhost ~]# ps aux |grep passwd
    root      3674  0.0  0.0 165764  1884 pts/1    S+   14:34   0:00 passwd

    示例2:sticky 用户只能删除自己的文件 <针对目录>

    [root@localhost ~]# mkdir /home/dir1
    [root@localhost ~]# chmod 777 /home/dir1
    测试:user1在/home/dir1建立文件, user2尝试删除!
    
    [root@localhost ~]# chmod o+t /home/dir1
    [root@localhost ~]# ll -d /home/dir1
    rwxrwxrwt 2 root root 4096 09-02 02:26 /home/dir1
    谁可以删除:
    root
    文件的所有者
    目录的所有者

    示例3:sgid 新建文件继承目录属组 <针对目录>

    [root@localhost ~]# mkdir /home/hr
    [root@localhost ~]# chgrp hr /home/hr/
    [root@localhost ~]# chmod g+s /home/hr
    [root@localhost ~]# ll -d /home/hr/
    drwxr-sr-x. 2 root hr 4096 Dec  5 16:03 /home/hr/
    
    [root@localhost ~]# touch /home/hr/file9
    [root@localhost ~]# ll /home/hr/
    -rw-r--r--. 1 root hr   0 Dec  5 16:03 file9
    
    =================================================================
    小知识:注意以下目录的正确权限,否则会导致程序不能正常运行
    [root@wangcy ~]# ll -d /tmp /var/tmp/
    drwxrwxrwt 14 root root 4096 07-26 10:15 /tmp
    drwxrwxrwt  2 root root 4096 07-24 19:02 /var/tmp/
    =================================================================
                    文件                                                 目录
    suid      执行的时候以所有者身份执行
    
    sqid                                                                继承属组 
    
    sticky                                                              用户只能删除自己的文件

    进程掩码 mask umask

    mask;
    用于临时降低用户或组(除属主和其他人)的权限
    mask决定了他们的最高权限
    建议:为了方便文件管理,其他人的权限置为空

    文件权限管理之: 进程umask

    进程 新建文件、目录的默认权限会受到umask的影响,umask表示要减掉的权限

    shell (vim,touch)      =======umask======>    新文件或目录权限
    vsftpd                  =======umask======>    新文件或目录权限 
    samba                   =======umask======>    新文件或目录权限 
    useradd                 =======umask======>    用户HOME

    示例1: 在shell进程中创建文件

    [root@localhost ~]# umask                                     //查看当前用户的umask权限
    0022
    [root@localhost ~]# touch file800
    [root@localhost ~]# mkdir dir800
    [root@localhost ~]# ll -d dir800 file800 
    drwxr-xr-x. 2 root root 4096 3月  11 19:40 dir800
    -rw-r--r--. 1 root root    0 3月  11 19:40 file800

    示例2:修改shell umask值(临时)

    [root@localhost ~]# umask 000
    [root@localhost ~]# mkdir dir900
    [root@localhost ~]# touch file900
    [root@localhost ~]# ll -d dir900 file900 
    drwxrwxrwx. 2 root root 4096 3月  11 19:44 dir900
    -rw-rw-rw-. 1 root root    0 3月  11 19:44 file900

    示例3:修改shell umask值(永久)

    [root@localhost ~]# vim /etc/profile   
    if [ $UID -gt 199 ] && [ "`id -gn`" = "`id -un`" ]; then
        umask 002
    else
        umask 022
    fi
    [root@localhost ~]# source /etc/profile       //立即在当前shell中生效

    示例4:通过umask决定新建用户HOME目录的权限

    [root@localhost ~]# vim /etc/login.defs 
    UMASK           077
    [root@localhost ~]# useradd gougou
    [root@localhost ~]# ll -d /home/gougou/
    drwx------. 4 gougou gougou 4096 3月  11 19:50 /home/gougou/
    
    [root@localhost ~]# vim /etc/login.defs
    UMASK           000
    [root@localhost ~]# useradd yangyang
    [root@localhost ~]# ll -d /home/yangyang/
    drwxrwxrwx. 4 yangyang yangyang 4096 3月  11 19:53 /home/yangyang/

    文件属性 chattr

    文件权限管理之: 文件属性
    注:设置文件属性(权限),针对所有用户,包括root

    [root@localhost ~]# touch file100 file200 file300
    [root@localhost ~]# lsattr file100 file200 file300 
    -------------e- file100
    -------------e- file200
    -------------e- file300
    
    [root@localhost ~]# man chattr
    [root@localhost ~]# chattr +a file100 
    [root@localhost ~]# chattr +i file200 
    [root@localhost ~]# chattr +A file300
    
    [root@localhost ~]# lsattr file100 file200 file300 
    -----a-------e- file100
    ----i--------e- file200
    -------A-----e- file300
    
    [root@localhost ~]# echo 111 > file100                    //以覆盖的方式写入
    bash: file100: Operation not permitted
    [root@localhost ~]# rm -rf file100 
    rm: cannot remove `file100': Operation not permitted
    [root@localhost ~]# echo 111 >> file100                  //以追加的方式写入,例如日志文件
    
    [root@localhost ~]# echo 111 > file200
    bash: file200: Permission denied
    [root@instructor ~]# echo 111 >> file200
    bash: file200: Permission denied
    [root@localhost ~]# rm -rf file200 
    rm: cannot remove `file200': Operation not permitted
    
    [root@localhost ~]# chattr -a file100
    [root@localhost ~]# chattr -i file200
    [root@localhost ~]# chattr -A file300
  • 相关阅读:
    信号量进程同步,王明学learn
    信号量互斥,王明学learn
    信号通讯编程,王明学learn
    管道通信,王明学learn
    多进程程序设计,王明学learn
    进程控制理论,王明学learn
    分布式系统间的通信框架
    qrcode.js生成二维
    浅谈spring 声明式事物
    索引失效浅谈
  • 原文地址:https://www.cnblogs.com/yanjieli/p/9541636.html
Copyright © 2020-2023  润新知