umask (限制新文件的权限)

嚯嚯 发表于 2020-07-05 08:17
浏览次数:
在手机上阅读

在类似Unix的操作系统上,umask命令返回或设置系统的文件模式创建掩码的值。 本文档介绍了umask的Linux版本。

查看英文版

目录

1 umask 运行系统环境

2 umask 描述

3 umask 语法

4 umask 示例

umask 运行系统环境

Linux

umask 描述

在Linux和其他类似Unix的操作系统上,将使用一组默认权限创建新文件。具体来说,可以通过应用称为umask的权限“掩码”,以特定方式限制新文件的权限。该的umask命令用来设置这种掩码,或者向你展示它的当前值。
On Linux and other Unix-like operating systems, new files are created with a default set of permissions. Specifically, a new file's permissions may be restricted in a specific way by applying a permissions "mask" called the umask. The umask command is used to set this mask, or to show you its current value.

查看英文版

查看中文版

umask 语法

umask [-S] [ mask ]
选件
-S 接受掩码的符号表示,或返回一个。
mask 如果指定了有效的掩码,则将umask设置为此值。如果未指定掩码,则返回当前的umask值。
什么是权限,它们如何工作?

正如你可能知道,您的系统上的每个文件与相关的一组的权限是用来保护文件:文件的权限决定哪些用户可以访问该文件,什么类型的访问他们拥有它。

共有三类用户:

  • 拥有文件的用户(“ User ”)。
  • 属于文件的已定义所有权组(“ Group ”)的用户。
  • 其他所有人(“ Other ”)。

反过来,对于这些用户类别,文件访问分为三种:

  • 查看文件内容的能力(“ Resd ”)。
  • 更改文件内容的能力(“ Write ”)。
  • 能够在系统上作为程序运行文件的内容(“ Execute ”)。

因此,对于这三类用户中的每一种,都有三种访问类型。这些信息共同构成了文件的权限。

权限如何表示?

有两种方法来表示文件的权限:用符号方式表示(使用诸如“ r ”表示读取,用“ w ”表示写入和使用“ x ”表示执行)之类的符号或八进制数字值。

例如,当您使用ls命令在命令行中列出目录的内容时,如下所示:

ls -l

您将(除其他信息外)看到每个文件的文件许可权信息。在这里,它用符号表示,看起来像下面的例子:

-rwxr-xr--

这里有十个符号。第一个破折号(“ - ”)表示这是一个“常规”文件,换句话说,不是目录(或设备或任何其他特殊类型的文件)。其余的9个符号代表权限:rwxr-xr--。这九个符号实际上是三组,每组三个符号,从左到右分别代表各自的特定权限:

符号 含义
rwx 文件的所有者可以读取写入执行此文件,作为系统上的进程。
r-x 文件组任何人都可以读取执行此文件,但不能对其进行写入。
r-- 任何人都可以读取该文件,但不能写入该文件或将其内容作为进程执行。
使用符号指定文件创建掩码

遮罩的一般符号形式如下:

[user class symbol(s)][permissions operator][permission symbol(s)][,]...

权限符号是r(读),w(写)或x(执行)的任意组合,如上所述。

用户类符号可以是以下一项或多项:

u 用户(文件的所有者)。
g 组(文件已定义组的任何成员)。
o 其他(其他人)。
a 全部(相当于ugo)。

权限运算符可能是以下之一:

+ 允许为指定的用户类启用指定的文件权限(未指定的权限在掩码中保持不变)。
- 禁止为指定的用户类启用指定的文件权限(未指定的权限在掩码中未更改)。
= 允许为指定的用户类别启用指定的文件权限(未指定的权限将在文件创建过程中被掩码禁止)。

因此,例如,以下umask命令:

umask u + w

设置掩码,以便在创建文件时,它们将具有允许用户(文件所有者)具有写权限的权限。该文件的其余权限将与操作系统默认值保持不变。

可以通过用逗号分隔多组符号表示法来指定多个更改(但不能用空格!)。例如:

umask ux,g = r,o + w

此命令将设置掩码,以便在创建后续文件时,它们将具有以下权限:

  1. 禁止为文件的所有者(用户)设置执行权限,而其余的所有者权限保持不变;
  2. 启用该组的读取权限,同时禁止该组的写入和执行权限;
  3. 启用其他用户的写权限,而其余其他权限保持不变。

请注意,如果使用equals运算符(“ = ”),则将特别禁止未指定的任何权限。例如,命令

umask a =

将设置文件创建掩码,以便每个人都无法访问新文件。

使用数字表示法指定文件创建掩码

文件创建掩模也可以表示数值,使用八进制值(数字0至7)。使用八进制数字表示法时,某些数字表示某些权限,并且这些数字彼此相加或相减以表示最终的组合权限值。具体地,数字12,和4表示以下权限:

数字 允许
4
2
1 执行

使用这些数字是因为这三个数字的任何组合都是唯一的。下表说明了它们的独特组合:

读取值+ 写值+ 执行值= 合并值: 等价的符号:
0 0 0 0
0 0 1 1 X
0 2 0 2 w
0 2 1 3 wx
4 0 0 4 r
4 0 1 5 rw
4 2 0 6 rw
4 2 1 7 rwx

对于每一类用户,可以使用一位数字来表示他们的权限。使用上面的示例,我们可以使用三位数的八进制数754表示rwxr-xr的符号许可。数字的顺序始终相同:UserGroupOther

其他权限数字

在文件许可权的八进制表示形式中,实际上有四个数字。我们讨论的三个重要数字是最后三个数字。第一位数字是特殊的文件许可指示符,出于讨论的目的,可以始终将其视为零。因此,从现在开始,当我们讨论文件权限777时,它也可能称为0777

那么umask实际如何工作?

umask的 面具由一定值限制他们的权限。

本质上,umask的每个数字都从操作系统的默认值中减去而得出您定义的默认值。这不是真正的减法。从技术上讲,将掩码取反(取其按位取反),然后使用逻辑AND操作将此值应用于默认权限。结果是umask告诉操作系统在创建文件时要关闭哪些权限位。

在Linux中,常规文件的默认权限值为666,目录的默认权限值为777。创建新文件或目录时,内核采用此默认值,“减去” umask值,并为新文件提供权限。

下表显示了umask值的每一位如何影响新文件和目录权限:

umask数字 默认文件权限 默认目录权限
0 rw rwx
1 rw rw
2 r rx
3 r r
4 w wx
5 w w
6 X X
7 (未经允许) (未经允许)

因此,如果我们的umask值为022,则默认情况下,任何新文件都将具有权限644(666-022)。同样,默认情况下,将使用权限755(777-022)创建任何新目录。

umask [-S] [mask]

Options

-S Accept a symbolic representation of a mask, or return one.
mask If a valid mask is specified, the umask is set to this value. If no mask is specified, the current umask value is returned.

What are permissions, and how do they work?

As you may know, each file on your system has associated with it a set of permissions that are used to protect files: a file's permissions determine which users may access that file, and what type of access they have to it.

There are three general classes of users:

  • The user who owns the file ("User").
  • Users belonging to the file's defined ownership group ("Group").
  • Everyone else ("Other").

In turn, for each of these classes of user, there are three types of file access:

  • The ability to look at the contents of the file ("Read").
  • The ability to change the contents of the file ("Write").
  • The ability to run the contents of the file as a program on the system ("Execute").

So, for each of the three classes of user, there are three types of access. Taken together, this information makes up the file's permissions.

How are permissions represented?

There are two ways to represent a file's permissions: symbolically (using symbols like "r" for read, "w" for write, and "x" for execute) or with an octal numeric value.

For example, when you list the contents of a directory at the command line using the ls command as follows:

ls -l

you will see (among other information) the file permission information for each file. Here, it is represented symbolically, which will look like the following example:

-rwxr-xr--

There are ten symbols here. The first dash ("-") means that this is a "regular" file, in other words, not a directory (or a device, or any other special kind of file). The remaining nine symbols represent the permissions: rwxr-xr--. These nine symbols are actually three sets of three symbols each, and represent the respective specific permissions, from left to right:

symbols meaning
rwx the file's owner may readwrite, or execute this file as a process on the system.
r-x anyone in the file's group may read or execute this file, but not write to it.
r-- anyone at all may read this file, but not write to it or execute its contents as a process.

Specifying the file creation mask using symbols

The general symbolic form of a mask is as follows:

[user class symbol(s)][permissions operator][permission symbol(s)][,]...

permission symbol is any combination of r (read), w (write), or x (execute), as described above.

user class symbol may be one or more of the following:

u User (the owner of the file).
g Group (any member of the file's defined group).
o Other (anyone else).
a All (equivalent to ugo).

permissions operator may be one of the following:

+ allow the specified file permissions to be enabled for the specified user classes (permissions that are not specified are unchanged in the mask).
- prohibit the specified file permissions from being enabled for the specified user classes (permissions that are not specified are unchanged in the mask).
= allow the specified file permissions to be enabled for the specified user classes (permissions not specified will be prohibited by the mask during file creation).

So, for example, the following umask command:

umask u+w

sets the mask so that when files are created, they will have permissions which allow write permission for the user (file owner). The rest of the file's permissions would be unchanged from the operating system default.

Multiple changes can be specified by separating multiple sets of symbolic notation with commas (but not spaces!). For example:

umask u-x,g=r,o+w

This command will set the mask so that when subsequent files are created, they will have permissions that:

  1. prohibit the execute permission from being set for the file's owner (user), while leaving the rest of the owner permissions unchanged;
  2. enable read permission for the group, while prohibiting write and execute permission for the group;
  3. enable write permission for others, while leaving the rest of the other permissions unchanged.

Note that if you use the equals operator ("="), any permissions not specified will be specifically prohibited. For example, the command

umask a=

Will set the file creation mask so that new files are inaccessible to everyone.

Specifying the file creation mask using numeric representation

The file creation mask can also be represented numerically, using octal values (the digits from 0 to 7). When using octal numeric representation, certain numbers represent certain permissions, and these numbers are added or subtracted from each other to represent the final, combined permissions value. Specifically, the numbers 12, and 4 represent the following permissions:

number permission
4 read
2 write
1 execute

These numbers are used because any combination of these three numbers will be unique. The following table illustrates their unique combinations:

read value + write value + execute value = combined value: symbolic equivalent:
0 0 0 0
0 0 1 1 x
0 2 0 2 w
0 2 1 3 wx
4 0 0 4 r
4 0 1 5 rx
4 2 0 6 rw
4 2 1 7 rwx

For each class of user, one digit can be used to represent their permissions; using the example above, we could represent the symbolic permission of rwxr-xr-- using the three-digit octal number 754. The order of the digits is always the same: UserGroupOther.

The other permission digit

In octal representations of file permissions, there are actually four digits. The three important digits we've discussed are the last three digits. The first digit is a special file permission indicator, and for the purposes of this discussion can be considered always to be zero. So from here on out, when we discuss file permission 777, it may also be referred to as 0777.

So how does the umask actually work?

The umask masks permissions by restricting them by a certain value.

Essentially, each digit of the umask is "subtracted" from the OS's default value to arrive at the default value that you define. It's not really subtraction; technically, the mask is negated (its bitwise compliment is taken) and this value is then applied to the default permissions using a logical AND operation. The result is that the umask tells the operating system which permission bits to "turn off" when it creates a file.

In Linux, the default permissions value is 666 for a regular file, and 777 for a directory. When creating a new file or directory, the kernel takes this default value, "subtracts" the umask value, and gives the new files the resulting permissions.

This table shows how each digit of the umask value affects new file and directory permissions:

umask digit default file permissions default directory permissions
0 rw rwx
1 rw rw
2 r rx
3 r r
4 w wx
5 w w
6 x x
7 (no permission allowed) (no permission allowed)

So if our umask value is 022, then any new files will, by default, have the permissions 644 (666 - 022). Likewise, any new directories will, by default, be created with the permissions 755 (777 - 022).

查看英文版

查看中文版

umask 示例

要查看系统的当前umask值,请输入以下命令:

umask

这将以四位数的八进制数返回系统的umask,例如:

0002

同样,第一个零是特殊的权限数字,可以忽略;就我们的目的而言,0002002相同。

要将其视为符号表示形式,请使用-S标志:

umask -S

它将象征性地返回相同的值,例如:

u = rwx,g = rwx,o = rx

其中u代表用户g代表o代表其他。因此,如果我们创建一个新文件,它将具有默认权限664,即666(文件的默认权限)被002(我们的umask值)掩盖。

让我们通过使用touch命令创建一个新文件来进行测试:

touch testfile

现在让我们获得该文件的目录清单:

ls -l testfile
-rw-rw-r-- 1 myusername myusername 0 Jan 7 14:29 testfile

如预期的那样,新文件具有-rw-rw-r--0664权限:所有者和组可以读取或写入文件,而其他人只能读取它。

现在,让我们更改umask。要将umask设置为022,请使用以下命令:

umask 022

这与运行umask 0022相同;如果仅指定三位数字,则假定第一位数字为零。让我们验证更改是否发生:

umask
0022

现在让我们创建一个新文件:

touch testfile2

现在,我们使用星号通配符(“ * ”)查看其目录列表以及我们创建的第一个文件,以查看所有名称以“ testfile ” 开头的文件:

ls -l testfile*
-rw-rw-r-- 1 myusername myusername 0 Jan 7 14:29 testfile 
-rw-rw-r-- 1  myusername myusername 0 Jan 7 14:39 testfile2

如您所见,testfile2具有权限644

这是一些其他umask命令示例:

umask a + r

设置掩码,以便新文件允许所有用户读取它们;其他权限将与默认权限保持不变。

umask a-x

设置掩码,以便新文件最初不会被任何用户执行;其他默认权限与默认值保持不变。

umask u=rw,go=

设置掩码,以便拥有该文件的用户可以读取和写入新文件,但不能执行它们;组成员和其他人将无权访问该文件。

umask 777

使所有人都无法访问新文件-没有人可以读取,写入或执行它们。

umask 000

使新文件对所有人绝对完全可用(读取,写入和执行)。但是,这是一个坏主意。不要这样

o view your system's current umask value, enter the command:

umask

which will return your system's umask as a four-digit octal number, for example:

0002

Again, the first zero is a special permission digit and can be ignored; for our purposes, 0002 is the same as 002.

To view this as a symbolic representation, use the -S flag:

umask -S

Which will return the same value symbolically, for example:

u=rwx,g=rwx,o=rx

where u stands for userg stands for group, and o stands for other. This is telling us the So if we create a new file, it will have the default permissions 664, which is 666 (the default permissions for files) masked by 002 (our umask value).

Let's test this by creating a new file with the touch command:

touch testfile

And now let's get a directory listing for that file:

ls -l testfile
-rw-rw-r-- 1 myusername myusername 0 Jan 7 14:29 testfile

As expected, the new file has permissions -rw-rw-r--, or 0664: The owner and group may read or write the file, and others may only read it.

Now let's change the umask. To set a umask of 022, use the command:

umask 022

This is the same as running umask 0022; if you specify only three digits, the first digit will be assumed to be zero. Let's verify that the change took place:

umask
0022

And now let's create a new file:

touch testfile2

And now let's view its directory listing, along with the first file we created, using the asterisk wildcard ("*") to view all files whose name start with "testfile":

ls -l testfile*
-rw-rw-r-- 1 myusername myusername 0 Jan  7 14:29 testfile
-rw-r--r-- 1 myusername myusername 0 Jan  7 14:39 testfile2

As you can see, testfile2 has the permissions 644.

Here are some other example umask commands:

umask a+r

Sets the mask so that new files will allow all users to read them; other permissions will be unchanged from the default.

umask a-x

Sets the mask so that new files will not initially be executable by any user; other default permissions unchanged from defaults.

umask u=rw,go=

Sets the mask so that new files will be readable and writable by the user who owns the file, but may not be executed; group members and others will have no permissions to access the file.

umask 777

Make new files inaccessible to everyone - no one can read, write, or execute them.

umask 000

Make new files completely accessible (read, write, and execute) to absolutely everyone. However, this is a bad idea. Don't do this.

查看英文版

查看中文版

其他命令行

userdel | usermod | ul | unalias | uname | uncompress | unhash | uniq | until | unlink | unpack | uptime | unzip | useradd |

如此好文,分享给朋友
发表评论
验证码:
评论列表
共0条