L15.linux命令每日一练 -- 第二章 文件和目录操作命令 -- chmod、chgrp和umask命令

2.23 chmod:设置及更改文件及目录权限命令

2.23.1 命令说明

【命令星级】 ★★★★★

【功能说明】

​ chmod命令是用来改变文件或目录权限的命令,但只有文件的属主和超级用户root才能够执行这个命令。

【语法格式】

chmod [option] [mode] [file]
chmod [选项] [模式] [<文件或目录>]

​ **说明:**通过chmod改变文件或目录的权限有两种方法,一种是使用数字方法(常用)来设置权限;另一种是通过字母和操作符表达式的方法来设置权限。

【选项说明】

​ 表2-24针对chmod命令的参数选项进行了说明。

​ 表2-24 chmod命令的参数选项及说明
在这里插入图片描述
图2-7是权限示意图。
在这里插入图片描述
​ 图2-7 chmod命令的权限示意图

​ 图2-7中权限对应的信息如表2-25所示。

​ 表2-25 权限对应表
在这里插入图片描述

2.23.2 使用范例

​ **范例2-96:**权限字母和操作符表达式。

[root@centos7 ~]# touch file1.txt
[root@centos7 ~]# ll file1.txt 
-rw-r--r--. 1 root root 0 Oct 21 19:28 file1.txt
[root@centos7 ~]# chmod a= file1.txt 	#设置所有(a)权限为空(等号后面不接任何字符)。
[root@centos7 ~]# ll file1.txt 
----------. 1 root root 0 Oct 21 19:28 file1.txt	#权限位都是"-",没有任何权限。
[root@centos7 ~]# chmod u+x file1.txt 	#设置user文件属主执行权限。
[root@centos7 ~]# ll file1.txt 
---x------. 1 root root 0 Oct 21 19:28 file1.txt
[root@centos7 ~]# chmod g+x file1.txt 	#设置group文件用户组可写权限。
[root@centos7 ~]# ll file1.txt 
---x--x---. 1 root root 0 Oct 21 19:28 file1.txt
[root@centos7 ~]# chmod o+r file1.txt 	#设置other其他用户可读权限。
[root@centos7 ~]# ll file1.txt 
---x--xr--. 1 root root 0 Oct 21 19:28 file1.txt
[root@centos7 ~]# chmod ug+r,o-r file1.txt 	#多个权限操作可用一起使用,以逗号分隔,ug+r是u+r、g+r的缩写。
[root@centos7 ~]# ll file1.txt 
-r-xr-x---. 1 root root 0 Oct 21 19:28 file1.txt
[root@centos7 ~]# chmod u=rwx,g=rx,o=x file1.txt 	#撤销原来所有的权限,然后赋予给出的权限。
[root@centos7 ~]# ll file1.txt 
-rwxr-x--x. 1 root root 0 Oct 21 19:28 file1.txt

​ **范例2-97:**文件的数字权限授权案例。

​ 图2-8为9位权限属性对应的数字及数字权限换算关系图。

在这里插入图片描述
​ 图2-8 字母和数字权限转换图

​ 已知r权限对应的数字为4,w权限对应的数字为2,x权限对应的数字为1,“-"权限对应的数字为0。因此rwx权限换成数字一一对应于421,然后做个加法:4+2+1=7。同理r-x的数字权限总和为5,-wx的数字权限总和为3。

[root@centos7 ~]# chmod 000 file1.txt 	#这个和上一个例子chmod a= file1.txt的效果一样。
[root@centos7 ~]# ll file1.txt 
----------. 1 root root 0 Oct 21 19:28 file1.txt
[root@centos7 ~]# chmod 753 file1.txt 	#大家一定要熟练掌握数字权限和字母权限的换算。
[root@centos7 ~]# ll file1.txt 
-rwxr-x-wx. 1 root root 0 Oct 21 19:28 file1.txt

​ **范例2-98:**使用-R参数递归授权权限案例。

[root@centos7 ~]# ll -d /data/dir2/
drwxr-xr-x. 2 neteagle neteagle 26 Oct 21 19:15 /data/dir2/	#目录权限755。
[root@centos7 ~]# ll /data/dir2/
total 4
-rw-r--r--. 1 neteagle neteagle 9 Oct 21 19:15 neteagle.txt	#文件权限644。
[root@centos7 ~]# chmod -R 777 /data/dir2/	#递归授予文件目录777的权限。
[root@centos7 ~]# ll -d /data/dir2/
drwxrwxrwx. 2 neteagle neteagle 26 Oct 21 19:15 /data/dir2/	#目录权限777。
[root@centos7 ~]# ll /data/dir2/
total 4
-rwxrwxrwx. 1 neteagle neteagle 9 Oct 21 19:15 neteagle.txt	#文件权限777。

2.23.3 Linux普通文件的读、写、执行权限说明

​ 为了让大家清晰地了解文件的权限属性,特列表2-26详细说明。

​ 表2-26 普通文件的权限说明
在这里插入图片描述

2.23.4 Linux目录的读、写、执行权限说明

​ 同样为了让大家清晰地了解目录的权限属性,特列表2-27详细说明。

​ 表2-27 目录的权限说明

在这里插入图片描述

2.24 chgrp:更改文件用户组

2.24.1 命令详解

【命令星级】 ★★★☆☆

【功能说明】

​ chgrp命令只用于更改文件的用户组,功能被chown取代了,了解一下即可。

【语法格式】

chgrp [option] [group] [file]
chgrp [选项] [用户组] [<文件或目录>]

​ **说明:**chgrp命令以及后面的选项和文件,每个元素之间都至少要有一个空格。

【选项说明】

​ 表2-28针对该命令的参数选项进行了说明。

​ 表2-28 chgrp命令的参数选项及说明

在这里插入图片描述

2.24.2 使用范例

​ **范例2-99:**修改文件的用户组属性信息。

[root@centos7 ~]# touch file2.txt
[root@centos7 ~]# ll file2.txt
-rw-r--r--. 1 root root 0 Oct 21 20:03 file2.txt	#当前用户组为root。
[root@centos7 ~]# chgrp neteagle file2.txt 	#修改file2.txt文件的用户组为neteagle。
[root@centos7 ~]# ll file2.txt
-rw-r--r--. 1 root neteagle 0 Oct 21 20:03 file2.txt

​ **范例2-100:**递归修改文件的用户组属性信息。

[root@centos7 ~]# ll -d /data/dir1
drwxr-xr-x. 2 root root 23 Oct 21 17:43 /data/dir1
[root@centos7 ~]# ll /data/dir1
total 0
-rw-r--r--. 1 root root 0 Oct 21 17:43 file1.txt
[root@centos7 ~]# chgrp -R neteagle /data/dir1		#参数-R递归授权。
[root@centos7 ~]# ll -d /data/dir1
drwxr-xr-x. 2 root neteagle 23 Oct 21 17:43 /data/dir1
[root@centos7 ~]# ll /data/dir1
total 0
-rw-r--r--. 1 root neteagle 0 Oct 21 17:43 file1.txt

2.25 umask:显示或设置权限掩码

2.25.1 命令详解

【命令星级】 ★★★☆☆

【功能说明】

​ umask是通过八进制的数值来定义用户创建文件或目录的默认权限。

【语法格式】

umask [option] [mode]
umask [选项] [模式]

​ **说明:**umask命令以及后面的选项和文件,每个元素之间都至少要有一个空格。

【选项说明】

​ 表2-29针对该命令的参数选项进行了说明。

​ 表2-29 umask命令的参数选项及说明

在这里插入图片描述

2.25.2 通过umask计算文件目录权限

2.25.2.1 文件权限计算

​ 创建文件默认最大的权限为666(-rw-rwxrwx)。其默认创建的文件没有可执行权限x位。

​ 对于文件来说,umask的设置是在假定文件拥有八进制666的权限上进行的,文件的权限就是666减umask(umask的各个位数也不能大于6,比如077讲究不符合条件)的掩码数值。如果得到的3位数字每一位都是偶数,那么这就是最终结果;如果有若干位的数字是奇数,那么这个奇数需要加1变成偶数,最后得到全是偶数的结果。

​ 下面列举几个示例进行说明。

​ 1)假设umask值为022(所有位均为偶数),那么文件的对应权限计算式为:

6 6 6		#文件的其实权限值。
2 2 2 -		#umask的值。
--------
6 4 4

​ 2)假设umask值为045(其他用户组位为奇数),那么文件的对应权限计算式为:

6 6 6 		#文件的起始权限值。
0 4 5 -		#umask的值。
----------
6 2 2 		#计算出来的权限。由于umask的最后一位数字是奇数5,所以,在其他用户组位再加1。
0 0 1 +
----------
6 2 2		#真实文件权限。

2.25.2.2 目录默认权限计算(umask没有奇偶之分)

​ 创建目录默认最大的权限为777(drwxrwxrwx),默认创建的目录属主是有x权限的,即允许用户进入。

​ 对于目录来说,umask的设置是在假定文件拥有八进制777权限上进行的,目录八进制权限777减去umask的掩码数值,即为:

7 7 7		#目录的起始权限值。
0 2 2 -		#umask的值。
----------
7 5 5

2.25.3 使用范例

​ **范例2-101:**系统默认的umask值。

[root@centos7 ~]# umask 	#超级用户root的umask默认值。
0022
[root@centos7 ~]# su - neteagle
[neteagle@centos7 ~]$ umask	#普通用户neteagle的umask默认值。
0002
#上面的结果是由下面的配置文件决定的。
[neteagle@centos7 ~]$ logout
[root@centos7 ~]# sed -n '70,74p' /etc/bashrc	#sed命令取出文件的第70行到74行,打印输出。
    if [ $UID -gt 199 ] && [ "`/usr/bin/id -gn`" = "`/usr/bin/id -un`" ]; then	#必须同时满足2个条件,条件1是当前登录用户的UID大于199,条件2是当前登录用户的用户名等于用户组名。
       umask 002	#2个条件均满足,则设置umask值为002。
    else
       umask 022	#若2个条件不满足,则设置umask值为022。
    fi

[root@centos6 ~]# sed -n '65,69p' /etc/bashrc	#CentOS 6是第65行到69行。
    if [ $UID -gt 199 ] && [ "`/usr/bin/id -gn`" = "`/usr/bin/id -un`" ]; then
       umask 002
    else
       umask 022
    fi
    
[root@centos7 ~]# id -gn	#使用id命令查看用户组的名称。
root
[root@centos7 ~]# id -un	#使用id命令查看用户名。
root
[root@centos7 ~]# echo $UID		#显示当前登录用户的UID值。
0    

​ **注意:**普通用户的umask未必是002,比如满足以下条件,neteagle用户属于incahome组的时候,由于id -gn的执行结果不等于id -un的执行结果,所以umask值为022。

范例2-102:-p与-S参数的使用。

[root@centos7 ~]# umask -p	#输出的权限掩码可直接作为命令来执行。
umask 0022
[root@centos7 ~]# umask -S	#采用字符权限格式输出umask对应的默认权限。
u=rwx,g=rx,o=rx

​ **范例2-103:**验证修改umask值对文件权限的影响。

[root@centos7 ~]# umask		#查看当前umask值。
0022
[root@centos7 ~]# touch file1;ll file1		#2条命令通过分号连接在一起可以在一起执行。
-rw-r--r--. 1 root root 0 Oct 21 20:39 file1	#创建的文件对应的数字权限为644。
[root@centos7 ~]# umask 044		#设置umask值为044。
[root@centos7 ~]# touch file2;ll file2
-rw--w--w-. 1 root root 0 Oct 21 20:40 file2	#创建的文件对应的数字权限为622。
[root@centos7 ~]# umask 034		#设置umask值为034。
[root@centos7 ~]# touch file3;ll file3
-rw-r---w-. 1 root root 0 Oct 21 20:40 file3	#创建的文件对应的数字权限为642。

​ **范例2-104:**验证修改umask值对目录权限的影响。

[root@centos7 ~]# umask	#查看当前的umask值,如果不是0022,则可用使用umask 022设置。
0034
[root@centos7 ~]# umask 022
[root@centos7 ~]# umask
0022
[root@centos7 ~]# mkdir dir1;ll -d dir1
drwxr-xr-x. 2 root root 6 Oct 21 20:44 dir1		#创建的目录对应的数字权限为755。
[root@centos7 ~]# umask 044		#设置umask值为044。
[root@centos7 ~]# mkdir dir2;ll -d dir2
drwx-wx-wx. 2 root root 6 Oct 21 20:44 dir2		#创建的目录对应的数字权限为733。
[root@centos7 ~]# umask 055		#设置umask值为055。
[root@centos7 ~]# mkdir dir3;ll -d dir3
drwx-w--w-. 2 root root 6 Oct 21 20:45 dir3		#创建的目录对应的数字权限为722。

​ 上面的修改都是临时生效的,要想永久生效就需要修改配置文件。

​ **范例2-105:**修改配置文件使得umask永久生效。

​ 通过命令查看umask的永久配置情况,配置文件可用是/etc/bashrc或/etc/profile。

[root@centos7 ~]# sed -n '66,74p' /etc/bashrc		#sed命令取出文件的第66到第74行,打印输出。
    # By default, we want umask to get set. This sets it for non-login shell.	#/etc/bashrc配置的是非登录shell的umask默认值。
    # Current threshold for system reserved uid/gids is 200
    # You could check uidgid reservation validity in
    # /usr/share/doc/setup-*/uidgid file
    if [ $UID -gt 199 ] && [ "`/usr/bin/id -gn`" = "`/usr/bin/id -un`" ]; then
       umask 002
    else
       umask 022
    fi

[root@centos7 ~]# sed -n '55,63p' /etc/profile		#sed命令取出文件的第55到第63行,打印输出。
# By default, we want umask to get set. This sets it for login shell	#/etc/profile配置的是登录shell(例如用户登录系统)的umask默认值。
# Current threshold for system reserved uid/gids is 200
# You could check uidgid reservation validity in
# /usr/share/doc/setup-*/uidgid file
if [ $UID -gt 199 ] && [ "`/usr/bin/id -gn`" = "`/usr/bin/id -un`" ]; then
    umask 002
else
    umask 022
fi

​ 以上是系统默认的umask配置情况,我们在工作中可以修改/etc/bashrc和/etc/profile实现对umask的永久修改,但是几乎没有什么需求必须要修改umask,默认的umask是系统安全的临界点,是最合适的,这点请大家注意。

​ 下面之间将修改umask的命令放在/etc/profile文件的最后一行,配置完成后,查看代码如下:

#以下为永久设置umask方法
[root@centos7 ~]# echo "umask 033" >>/etc/profile
[root@centos7 ~]# . /etc/profile
[root@centos7 ~]# tail -1 /etc/profile	#使用tail命令查看文件的最后一行。
umask 033
[root@centos7 ~]# umask
0033

#以下恢复系统初始umask值
[root@centos7 ~]# sed -i '/umask 033/d' /etc/profile 
[root@centos7 ~]# tail -1 /etc/profile
unset -f pathmunge
[root@centos7 ~]# . /etc/profile
[root@centos7 ~]# umask
0022

​ 上面的方法是一刀切的方法,所有用户的umask值统一设置为033.

猜你喜欢

转载自blog.csdn.net/qq_25599925/article/details/125351712