• 运维基础-文件权限管理


    Linux是一个多用户操作系统,在多用户操作系统上我们需要一种方法来允许或者拒绝访问特定的文件和目录。文件有所属人和相关的单个组。我们可以设置所属人或者租的权限,以及所有其他人的权限。

    文件只具有三个应用权限的用户类别。文件的所有者,通常是创建文件的用户。文件的所有组,通常是创建改文件的用户所在的主要组,但是可以进行更改。可以为文件的所属人、所属组和系统中除所属人和所属组的其它用户设置不同的权限。

    id命令可以查看用户的主要组和补充组成员

    可以使用三种权限:

    权限,也就是对文件和目录的影响

    r(读取):可以读取文件的内容,可以累出目录的内容(文件名)

    w(写入):可以更改文件的内容,可以创建或者是删除目录中的任一文件

    x(执行):可以作为命令执行的文件,可以访问目录的内容(取决于目录中文件的权限)

    使用ls -l 和ls -ld命令来查看所有权、类型,和文件的权限

    [root@localhost ~]# ls -l
    总用量 8
    -rw-------. 1 root root 1635 8月  20 19:18 anaconda-ks.cfg
    -rw-r--r--. 1 root root 1666 8月  20 19:21 initial-setup-ks.cfg
    [root@localhost ~]# ls -ld
    dr-xr-x---. 5 root root 260 8月  23 08:18 .

    一个文件的常见权限是:无,读 或者读/写

    一个目录的时候,他们想要的一般是ls读取和cd执行

    一个文件的写权限是允许修改文件(nano或者重定向),一个目录的写权限是允许修改目录的内容(touch,mkdir,cp,mv,rm)

    通常用户对只读目录具有read和exec权限,因此他们可以列出目录并访问其中的内容。如果用户对目录具有read访问的权限,可以列出其中文件的名称,但是其他信息都不可以访问。如果用户对某个目录具有exec访问的权限,则他们不能列出该目录中文件的名称,但是他们如果已经知道具有读取权限的问价名称,那么可以通过绝对路径的方式来访问文件的内容。

    对问价的所在目录具有写入权限的任何人都可以删除该目录下的文件,不论文件的所属人和权限如何。

    从命令行管理文件系统的权限

    更改文件/目录的权限

    命令行更改权限可以使用chmod(change mode 更改命令)。

    符号法:

    chmod whowhatwhich file|directory

    -who 是指u、g、o、a(表示用户,组,其她,全部)

    -what是指+、-、=(表示添加,删除,精确设置)

    -which 是指r、w、x(表示读取,写入,执行)

    数值法:

    chmod ### file|directory

    -每个数字代表一个访问的级别:用户、组、其他

    -#是r=4,w=2,x=1的组合

    更改目录/文件的所属人和所属组

    默认情况下,我虚拟键文件的所属人为创建该文件的用户,所属组为该用户的主要组

    使用chown命令可以更改文件的所有权

    配合-R选项,可以递归更改这个目录树的所有权

    chown也可以用于更改文件的所属组,只需在组名称之前加上冒号(:)

    练习:

    一个目录可以让ateam组中的所有成员访问,alex创建的文件可以让zhang修改

    1,在/home总中创建ateam-text目录

    [root@localhost ~]# groupadd ateam
    [root@localhost ~]# mkdir /home/ateam-text

    2、将ateam-text目录的所有权更改为ateam

    [root@localhost ~]# chown :ateam /home/ateam-text/

    3、确保组成员在ateam-text中可以创建和删除文件

    [root@localhost ~]# chmod g+w /home/ateam-text/

    4、去报ateam-text禁止他人访问

    [root@localhost ~]# chmod 770 /home/ateam-text/
    [root@localhost ~]# ls -ld /home/ateam-text/
    drwxrwx---. 2 root ateam 6 8月  23 08:39 /home/ateam-text/

    5、退出root,切换到alex

    [root@localhost ~]# su alex
    [alex@localhost root]$ uname 
    Linux
    [alex@localhost root]$ id
    uid=1001(alex) gid=1001(alex) 组=1001(alex),30000(shakespeare) 环境=unconfined_u:unconfined_r:unconf
    ined_t:s0-s0:c0.c1023

    6、进入/home/ateam-text

    [alex@localhost root]$ cd /home/ateam-text/
    bash: cd: /home/ateam-text/: 权限不够
    [alex@localhost root]$ su 
    密码:
    [root@localhost ~]# useradd -G ateam alex
    useradd:用户“alex”已存在
    [root@localhost ~]# usermod -G ateam alex
    [root@localhost ~]# su alex
    [alex@localhost root]$ cd /home/ateam-text/
    [alex@localhost ateam-text]$ 

    7、创建文件alexfile3

    [alex@localhost ateam-text]$ touch alexfile3
    [alex@localhost ateam-text]$ ls
    alexfile3
    [alex@localhost ateam-text]$ 

    8、观察新文件默认的所属人和所属组

    [alex@localhost ateam-text]$ ls -l alexfile3 
    -rw-rw-r--. 1 alex alex 0 8月  23 08:55 alexfile3
    [alex@localhost ateam-text]$

    9、将文件的所属组更改为ateam

    [alex@localhost ateam-text]$ chown :ateam alexfile3 
    [alex@localhost ateam-text]$ ls -l alexfile3 
    -rw-rw-r--. 1 alex ateam 0 8月  23 08:55 alexfile3

    10、切换到zhang

    [alex@localhost ateam-text]$ su - zhang
    密码:
    上一次登录:四 8月 23 08:18:24 CST 2018pts/1 上
    [zhang@localhost ~]$ id
    uid=1000(zhang) gid=1000(zhang) 组=1000(zhang),10(wheel) 环境=unconfined_u:unconfined_r:unconfined_t
    :s0-s0:c0.c1023

    11、进入/home/ateam-text,编辑alexfile3

    [root@localhost zhang]# usermod -G ateam zhang
    [root@localhost zhang]# su zhang
    [zhang@localhost ~]$ cd /home/ateam-text/
    [zhang@localhost ateam-text]$ echo 'text' >> alexfile3 
    [zhang@localhost ateam-text]$ cat alexfile3 
    text
    [zhang@localhost ateam-text]$ 

    管理默认权限和文件访问

    配置一个目录,在这个目录下新创建文件,目录的所属成员自动具有写权限,使用特殊权限,和默认的umask设置

    特殊权限

    对于可执行的文件setuid(或setgid)权限表示以文件所属人(或者所属组)的身份运行命令,而不是以执行者的身份运行,例如passwd命令

    [zhang@localhost ateam-text]$ ls -l /usr/bin/passwd 
    -rwsr-xr-x. 1 root root 27832 6月  10 2014 /usr/bin/passwd
    [zhang@localhost ateam-text]$ 

    目录的粘滞位(sticky)可以设置删除文件的特殊权限:仅文件的所有者(和root)可以删除目录中的文件,例如/tmp目录:

    [zhang@localhost ateam-text]$ ls -ld /tmp/
    drwxrwxrwt. 19 root root 4096 8月  23 09:05 /tmp/
    [zhang@localhost ateam-text]$ 

    目录的setgid权限表示在该目录中新建的文件将继承该目录的所属组,额不是创建用户的主要组。

    设置特殊权限

    用符号表示:setuid=u+s;setgid=g+s;sticky=o+t

    用数值表示(第一位数字):setuid=4;setgid=2;sticky=1

    在一个目录总创建的文件自动继承目录的组

    默认设置允许系统上的素有用户组查看公共区域的文件,调整使得创建的文件拥有更多限制的权限。

    默认文件权限

    在系统中创建文件和目录的时候都会有默认权限,文件时666,目录是777.

    但是我们会发现创建出的文件和目录不是666和777,这是因为其中一些权限被shell的umask屏蔽了。

    bash shell的系统默认umask在/etc/profile 和/etc/bashrc文件中定义。用户可以通过自己的家目录中的.bash_profile 和.bashrc 文件的设置来覆盖系统默认值

    创建新文件,查看默认umask对权限的影响

    [root@localhost ateam-text]# touch newfile1
    [root@localhost ateam-text]# ls -l newfile1 
    -rw-r--r--. 1 root root 0 8月  23 09:12 newfile1
    [root@localhost ateam-text]# mkdir newdir1
    [root@localhost ateam-text]# ls -ld newdir1/
    drwxr-xr-x. 2 root root 6 8月  23 09:12 newdir1/
    [root@localhost ateam-text]# 

    将umask设置为0,次设置不会屏蔽新文件的任何权限

    [root@localhost ateam-text]# umask 0
    [root@localhost ateam-text]# touch newfile2
    [root@localhost ateam-text]# ls -l newfile
    ls: 无法访问newfile: 没有那个文件或目录
    [root@localhost ateam-text]# ls -l newfile2
    -rw-rw-rw-. 1 root root 0 8月  23 09:13 newfile2
    [root@localhost ateam-text]# mkdir newdir2
    [root@localhost ateam-text]# ls -ld newdir2
    drwxrwxrwx. 2 root root 6 8月  23 09:13 newdir2
    [root@localhost ateam-text]# 

    将umask设置为007,此设置将屏蔽其他人的权限

    [root@localhost ateam-text]# umask 007
    [root@localhost ateam-text]# ls
    alexfile3  newdir1  newdir2  newfile1  newfile2  newfile3
    [root@localhost ateam-text]# ls -l newfile3
    -rw-rw----. 1 root root 0 8月  23 09:15 newfile3
    [root@localhost ateam-text]# mkdir newdir3
    [root@localhost ateam-text]# ls -ld newdir3
    drwxrwx---. 2 root root 6 8月  23 09:16 newdir3
    [root@localhost ateam-text]# 

    将umask设置为027,次设置将屏蔽组的写权限和其他人的所有权限

    [root@localhost ateam-text]# umask 027
    [root@localhost ateam-text]# touch newfile4
    [root@localhost ateam-text]# ls -l newfile4
    -rw-r-----. 1 root root 0 8月  23 09:17 newfile4
    [root@localhost ateam-text]# mkdir newdir4
    [root@localhost ateam-text]# ls -ld newdir4
    drwxr-x---. 2 root root 6 8月  23 09:17 newdir4
    [root@localhost ateam-text]# 

    使用root用户登录更改费特权用户默认的umask,拒绝非组中的用户所有访问。

    修改/etc/bshrc 和/etc/profile更改bash shell 用户的默认umask,因为非特权用户的默认umask是002,在文件中找到umask命令设置umask值,更改他们,所属组为007.

    umask:Linux中新建文件或者目录的初始权限

    练习:

    1、切换到alex用户,确定用户的umask

    [root@localhost ateam-text]# su alex
    [alex@localhost ateam-text]$ umask 
    0002
    [alex@localhost ateam-text]$ 

    2、创建新目录/tmp/shared 和新文件/tmp/shared/defaults,查看默认umask对权限的影响

    [alex@localhost ateam-text]$ mkdir /tmp/shared
    [alex@localhost ateam-text]$ ls -ld /tmp/shared/
    drwxrwxr-x. 2 alex alex 6 8月  23 09:25 /tmp/shared/
    [alex@localhost ateam-text]$ touch /tmp/shared/defaults
    [alex@localhost ateam-text]$ ls -l /tmp/shared/defaults 
    -rw-rw-r--. 1 alex alex 0 8月  23 09:25 /tmp/shared/defaults
    [alex@localhost ateam-text]$ 

    3、将/tmp/share目录所属组改为ateam

    [alex@localhost ateam-text]$ chown :ateam /tmp/shared/
    [alex@localhost ateam-text]$ ls -ld /tmp/shared/
    drwxrwxr-x. 2 alex ateam 22 8月  23 09:25 /tmp/shared/
    [alex@localhost ateam-text]$ 

    4、在/tmp/shared目录中创建一个新的文件

    [alex@localhost ateam-text]$ touch /tmp/shared/alex3
    [alex@localhost ateam-text]$ ls -l /tmp/shared/alex3 
    -rw-rw-r--. 1 alex alex 0 8月  23 09:27 /tmp/shared/alex3
    [alex@localhost ateam-text]$ 

    5、确保在/tmp/shared目录中新建的文件能够继承ateam组

    [alex@localhost ateam-text]$ chmod g+s /tmp/shared/
    [alex@localhost ateam-text]$ ls -ld /tmp/shared/
    drwxrwsr-x. 2 alex ateam 35 8月  23 09:27 /tmp/shared/
    [alex@localhost ateam-text]$ touch /tmp/shared/alex4
    [alex@localhost ateam-text]$ ls -l /tmp/shared/alex4
    -rw-rw-r--. 1 alex ateam 0 8月  23 09:28 /tmp/shared/alex4
    [alex@localhost ateam-text]$ 

    6、更改Alex的umask,使得创建的新文件所属组拥有只读权限,其他人,没有访问权限

    [alex@localhost ateam-text]$ umask 
    0002
    [alex@localhost ateam-text]$ umask 027
    [alex@localhost ateam-text]$ touch /tmp/shared/alex5
    [alex@localhost ateam-text]$ ls -l /tmp/shared/alex5
    -rw-r-----. 1 alex ateam 0 8月  23 09:30 /tmp/shared/alex5
    [alex@localhost ateam-text]$ 

    7、切换到alex,打开一个新的shell查看umask

    [alex@localhost ateam-text]$ su - alex
    密码:
    上一次登录:四 8月 23 09:23:41 CST 2018pts/1 上
    [alex@localhost ~]$ umask
    0002
    [alex@localhost ~]$ 

    8、将Alex默认的umask更改为禁止不属于组的用户的所有访问权限

    [alex@localhost ~]$ echo 'umask 007' >> ~/.bashrc 
    [alex@localhost ~]$ cat ~/.bashrc 
    # .bashrc
    
    # Source global definitions
    if [ -f /etc/bashrc ]; then
        . /etc/bashrc
    fi
    
    # Uncomment the following line if you don't like systemctl's auto-paging feature:
    # export SYSTEMD_PAGER=
    
    # User specific aliases and functions
    umask 007
    [alex@localhost ~]$ 

    从新使用Alex登录

    [alex@localhost ~]$ umask
    0007
    [alex@localhost ~]$ 
  • 相关阅读:
    shell 编程 如何实现 比较两个整数的大小
    从Mysql某一表中随机读取n条数据的SQL查询语句
    AS3中UTF8、GB2312、BIG5、GBK编码转换类
    Google Map API V3 离线版
    linux下解压命令大全
    PHP 5.3无法安装Memcached解决方案
    根据淘宝商品 num_iid 批量生成淘宝客链接的 PHP 函数
    Linux curl使用简单介绍
    TCP/IP UDP用户数据报协议 运输层
    TCP/IP 应用层
  • 原文地址:https://www.cnblogs.com/52-qq/p/9521893.html
Copyright © 2020-2023  润新知