第 178 / 187 篇

17.7. 查看与修改文件权限

说明

本文是个人学习用的非官方中英对照译文,原文:https://diveintosystems.org/book/Appendix2/chmod.html。
原书采用 CC BY-NC-ND 4.0 许可(禁止演绎),请勿公开传播本译文。
标为「译注」「现代更新」的内容是译者补充,不代表原书作者观点;代码与原文逐字一致,未作改动。

17.7. Viewing and Changing File Permissions

每个 Unix 文件(和目录)都受一组权限(permission)的约束,权限定义了对它的访问权利。与文件相关的权限有:

Every Unix file (and directory) is governed by a set of permissions, which defines its access rights. The permissions associated with a file are:

  • r:对文件或目录的读权限
  • r: read access to the file or directory
  • w:对文件或目录的写权限
  • w: write access to the file or directory
  • x:对文件或目录的执行权限(对于目录,这意味着你可以 cd 进入它;对于文件,这意味着你可以在命令行上执行它(例如 ./filename))。编译器会自动为生成的可执行文件设置所有者的 x 权限。
  • x: execute access to the file or directory (for a directory, this means you can cd into it, for a file it means you can execute it at the command line (e.g., ./filename). The compiler automatically sets the target executable file with x permission for the owner.

每个文件的权限还指定了三组用户:文件的用户/所有者(u);与文件所属组同组的用户(g);以及其他人(o),即其他所有人。通过修改文件的权限,你可以针对这几组不同的用户,以不同的方式保护或共享文件。例如,可以把文件权限设置为:所有者拥有读写权限,文件所属组中的用户拥有读权限,而其他所有人既不能读也不能写。

Each file’s permissions also specifies three sets of users: the file user/owner (u); users in the same group as the file’s group (g); and other (o) or everybody else. By changing the file permissions with a file, you can protect or share files in different ways these different sets of users. For example, file permission can be set to give the owner read and write access, users in the file’s group read access, and everybody else no read or write access.

要列出文件和目录的权限,使用 ls 的 -l 命令行选项:

To list file permissions with files and directories use the -l command line option of ls:

1
2
3
4
5
$ls -l
drwx------ 3 sam users 4096 Mar 26 12:49 classes/
drwx------ 2 sam users 4096 Mar 26 12:54 letters/
-rw-r----- 1 sam pals 78 Mar 26 12:51 notes
drwx------ 2 sam users 4096 Mar 26 12:48 projects/

ls -l 的输出采用下面的格式(以 notes 的列表为例):

The output from ls -l is in the following format (showing the listing of notes as an example):

1
2
3
4
type   permissions  links  owner  group  size  modification date   name
u g o
- rw- r-- --- 1 sam pals 78 Mar 26 12:51 notes
d rwx --- --- 3 sam users 4096 Mar 26 12:49 classes/

type 对于目录是 d,对于普通文件是 -。文件权限列表分为三组 rwx,分别指定文件的用户或所有者(u)、组(g)和其他人(o)的权限。例如,与 notes 文件相关的 -rw-r----- 表示:文件所有者 sam 对文件有读写权限(rw-),文件所属组 pals 只有读权限(r--),其他人没有访问该文件的任何权限(---)。列表中的其他字段显示了文件所有者的用户名(sam)、文件所属组的名字(pals)、文件的字节数(78)、文件最后修改的时间(Mar 26 12:51)以及文件名(notes)。列出的还有该文件的硬链接数(1),即目录结构中引用同一个文件的名字的数目。

The type is d for directories and - for regular files. The file permission listings are in three groups of rwx specifying permissions for the file user or owner (u), group (g), and others (o). For example, the -rw-r----- associated with the notes file means that the file owner sam has read and write access to the file (rw-), that the file’s group, pals has read permission only (r--), and others have no permissions to access the file (---). The other fields in the listing show the username of the file owner (sam), the name of the file’s group (pals), the size of the file in bytes (78), the last time the file was modified (Mar 26 12:51) and the name of the file (notes). Also listed are the number of hard links to the file (1), which is the number of names in the directory structure that refer to this same file.

classes 的列表是 drwx------,这表示 classes 是一个目录,文件所有者 sam 有读、写和执行权限(rwx),而目录所属组(users)和其他人没有任何访问权限(---)。换句话说,这是用户 sam 的私有目录;其他任何人都不能访问这个目录。我们注意到,目录必须设置了执行(x)权限才能 cd 进入。目录的链接数大于 1,因为目录名(例如 classes)和目录中的名字 ./(以及它的任何子目录中的名字 ../)都引用同一个目录。由于 classes 有一个子目录 CS31,它的链接数是 3。

The listing for classes is drwx------, which means that classes is a directory, the file owner sam has read, write, and execute permissions (rwx), and that the directory’s group (users) and others have no access permissions (---). In other words this is a private directory to the user sam; no one else can access this directory. We note that a directory must have execute (x) permission set in order to cd into it. Directories have link sizes greater than 1 because both the directory name (e.g., classes) and the name ./ in the directory (and the name ../ in any of its subdirectories) refer to the same directory. Because classes has one subdirectory, CS31, its link value is 3.

17.7.1. chmod

17.7.1. chmod

chmod 命令修改文件或目录的权限。它接收的命令行参数指定如何修改与每个文件相关的三组用户(用户/所有者、组和其他人)的权限。

The chmod command changes the permissions on a file or directory. It takes command line arguments that specify how to change the permissions for the three sets of users associated with every file: user/owner, group, and other.

运行 chmod 的一种方式,是给出一个三位数字作为命令行参数,它编码了如何设置权限。第一位数字指定文件用户/所有者的权限,第二位指定文件所属组的权限,第三位指定其他所有用户的权限。例如,chmod 700 letters 把 letters 变成一个私有目录;7 把用户的权限设为 rwx,0 把组和其他人的权限设为 ---。

One way to run chmod is to give a three digit number as a command line argument that encodes how to set the permissions. The first digit specifies the permissions for the file user/owner, the second digit specifies the permissions for the file group, and the third for all other users. For example, chmod 700 letters makes letters a private directory; 7 sets the user’s permissions to rwx, and 0 set permissions to --- for group and others.

要理解这些数字的含义,把每一位数字看作一个三位二进制数,对于 rwx 这三项,1 表示授予该权限,0 表示不授予。表 1 给出了一些例子。

To understand what the digits mean, consider each digit represented as a three bit binary number where a 1 means grant the permission and 0 means don’t grant it for the set rwx. Table 1 shows some examples.

表 1. chmod 权限编码示例

Table 1. chmod Permission Encoding Examples

数字 二进制 权限
7 111 设置 rwx 权限
5 101 只设置 r 和 x 权限
4 100 只设置 r 权限
6 110 只设置 rw 权限
0 000 清除所有权限…​无法访问
Digit Binary Permission
7 111 set rwx permissions
5 101 set r x permissions only
4 100 set r permission only
6 110 set rw permissions only
0 000 clear all permissions…​no access

例如,如果 sam 想让其他所有用户都能读 notes 文件,并给自己读写权限,他会运行下面的命令:

For example, if sam wants to give read access to the notes file with all other users and give himself read and write access, he would run the following command:

1
2
3
4
5
6
$ chmod 644 notes
$ ls -l
drwx------ 3 sam users 4096 Mar 26 12:49 classes/
drwx------ 2 sam users 4096 Mar 26 12:54 letters/
-rw-r--r-- 1 sam pals 78 Mar 26 12:51 notes
drwx------ 2 sam users 4096 Mar 26 12:48 projects/

644 把 notes 的权限设为 -rw-r--r--:6 给文件所有者 rw- 权限;4 给文件所属组 r-- 权限;4 给其他人 r-- 权限。

The 644 sets the permissions on notes to -rw-r—​r--: 6 gives the file’s owner rw- access; 4 gives the file’s group r-- access; and 4 gives other r-- access.

chmod 命令也可以使用 [u,g,o][+,-][permissions] 这种设置格式。例如,如果 sam 想给 users 组对 projects 目录的写权限,他可以用 g+w(给组加上写权限)来指定这个权限;如果他想去掉其他人对 notes 的读权限,可以指定 o-r(从其他人那里去掉读权限):

The chmod command can also use the [u,g,o][+,-][permissions] setting format too. For example, if sam wants to give the users group write access to the projects directory, he can specify this permission using g+w (add write permission to group), and if he wants to remove read access from notes to others, he can specify o-r (remove read permission from all):

1
2
3
4
5
6
7
$ chmod g+w projects
$ chmod o-r notes
$ ls -l
drwx------ 3 sam users 4096 Mar 26 12:49 classes/
drwx------ 2 sam users 4096 Mar 26 12:54 letters/
-rw-r----- 1 sam pals 78 Mar 26 12:51 notes
drwx-w---- 2 sam users 4096 Mar 26 12:48 projects/

当他意识到给 users 组对 projects 目录的写权限是个糟糕的主意时,可以用 g-w 去掉它:

And when he realizes that giving the users group write access to his projects directory was a terrible idea, he can remove it using g-w:

1
2
3
$ chmod g-w projects
$ ls -l projects
drwx------ 2 sam users 4096 Mar 26 12:48 projects/

chmod 命令还有其他命令行选项,包括一个递归选项,可以递归地修改整个子目录内容的权限。更多细节参见手册页。

The chmod command has other command line options, including a recursive flag to recursively change permissions on a full subdirectory’s contents. See the man page for more details.

Linux 还支持访问控制列表(access control list,ACL),它允许把丰富得多的权限集合与文件和目录关联起来。getfacl 和 setfacl 的手册页包含关于 ACL 的信息,不过细节超出了我们的介绍范围。

Linux also supports access control lists (ACLs), which allow a much richer set of permissions to be associated with files and directories. The man page for getfacl and setfacl contain information about ACLs, but the details are beyond the scope of our coverage.

17.7.2. chgrp

17.7.2. chgrp

chgrp 命令可以用来修改与文件或目录关联的组。Unix 组通常需要由拥有系统 root 权限的系统管理员来创建(root 用户是 Unix 的超级用户账户,它拥有普通用户所没有的文件系统访问权利)。

The chgrp command can be used to change the group associated with a file or directory. Unix groups typically need to be created by a system administrator who has root access on the system (the root user is the Unix superuser account that has access rights to the filesystem that a regular user does not have).

如果用户 sam 和另外两个用户同属于一个名为 proj3 的组,sam 就可以修改他的 projects 目录所属的组并设置权限,只给这个组对该目录更高的权利。例如:

If the user sam is part of a group named proj3 with two other users, sam can change the group and set permissions on their projects directory to give just this group elevated rights to the directory. For example:

1
2
3
4
5
6
7
8
9
10
11
12
13
$ chgrp proj3 projects  # change the projects directory's group to proj3
$ ls -l
drwx------ 3 sam users 4096 Mar 26 12:49 classes/
drwx------ 2 sam users 4096 Mar 26 12:54 letters/
-rw-r----- 1 sam pals 78 Mar 26 12:51 notes
drwx------ 2 sam proj3 4096 Mar 26 12:48 projects/

$ chmod 750 projects # give proj3 group members r and x access to directory
$ ls -l
drwx------ 3 sam users 4096 Mar 26 12:49 classes/
drwx------ 2 sam users 4096 Mar 26 12:54 letters/
-rw-r----- 1 sam pals 78 Mar 26 12:51 notes
drwxr-x--- 2 sam proj3 4096 Mar 26 12:48 projects/

类似地,chown 命令修改文件的所有者,但你需要有 root 权限才能运行它。

Similarly, the chown command changes the owner of a file, but you need to have root privileges to run it.

原书注 · 文件所属组的设置与默认权限(File group setting and default permissions)

每个用户都有默认的权限和默认的组,它们会自动与该用户创建的所有新文件和目录关联起来。两者都可以在 shell 的配置文件(例如 .bashrc)中修改。关于点文件的更多信息,参见第 17.14 节。

你的默认组由名为 GROUP 的 shell 环境变量指定。可以用 echo $GROUP 列出它的值:

1
2
$ echo $GROUP
GROUP=users

你的默认文件权限由 .bashrc 文件中的 umask 设置配置。可以用 umask 列出它的值:

1
2
$ umask
77

umask 为 77(即 077)会让我所有的文件和目录都成为私有的(它把组和其他人的权限设为 00,即 ------)。可以把 umask 编码看作 chmod 编码的某种反向(例如,在 umask 中 7 去掉所有权限(rwx),而在 chmod 中 7 授予所有权限)。

Each user has default permissions and a default group that will automatically be associated with all new files and directories the user creates. Both of these can be modified in your shell’s config file (e.g., .bashrc). See Section 17.14 for more information about dot files.

Your default group is specified in the shell environment variable named GROUP. You can list its value using echo $GROUP:

1
2
$ echo $GROUP
GROUP=users

Your default file permission are configured from the umask setting in your .bashrc file. You can list its value using umask:

1
2
$ umask
77

A umask of 77 (or 077) makes all my files and directories private (it sets the group and other permissions to 00 or ------). You can think of the umask encoding as being sort of like the inverse of the chmod encoding (e.g., 7 removes all permissions (rwx) in umask but grants all in chmod).

17.7.3. 参考资料

17.7.3. References

更多信息参见:

For more information see:

  • 这些命令的手册页(例如 man chmod)
  • The man pages for these commands (e.g., man chmod)