说明
本文是个人学习用的非官方中英对照译文,原文: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 cancdinto 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 withxpermission 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
-lcommand line option ofls:
1 | $ls -l |
ls -l 的输出采用下面的格式(以 notes 的列表为例):
The output from
ls -lis in the following format (showing the listing ofnotesas an example):
1 | type permissions links owner group size modification date name |
type 对于目录是 d,对于普通文件是 -。文件权限列表分为三组 rwx,分别指定文件的用户或所有者(u)、组(g)和其他人(o)的权限。例如,与 notes 文件相关的 -rw-r----- 表示:文件所有者 sam 对文件有读写权限(rw-),文件所属组 pals 只有读权限(r--),其他人没有访问该文件的任何权限(---)。列表中的其他字段显示了文件所有者的用户名(sam)、文件所属组的名字(pals)、文件的字节数(78)、文件最后修改的时间(Mar 26 12:51)以及文件名(notes)。列出的还有该文件的硬链接数(1),即目录结构中引用同一个文件的名字的数目。
The
typeisdfor directories and-for regular files. The file permission listings are in three groups ofrwxspecifying permissions for the file user or owner (u), group (g), and others (o). For example, the-rw-r-----associated with thenotesfile means that the file ownersamhas read and write access to the file (rw-), that the file’s group,palshas 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
classesisdrwx------, which means thatclassesis a directory, the file ownersamhas 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 usersam; no one else can access this directory. We note that a directory must have execute (x) permission set in order tocdinto it. Directories have link sizes greater than1because 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. Becauseclasseshas one subdirectory,CS31, its link value is3.
17.7.1. chmod
17.7.1. chmod
chmod 命令修改文件或目录的权限。它接收的命令行参数指定如何修改与每个文件相关的三组用户(用户/所有者、组和其他人)的权限。
The
chmodcommand 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
chmodis 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 lettersmakeslettersa private directory;7sets the user’s permissions torwx, and0set 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
1means grant the permission and0means don’t grant it for the setrwx. 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 7111set rwx permissions 5101set r x permissions only 4100set r permission only 6110set rw permissions only 0000clear all permissions…no access
例如,如果 sam 想让其他所有用户都能读 notes 文件,并给自己读写权限,他会运行下面的命令:
For example, if
samwants to give read access to thenotesfile with all other users and give himself read and write access, he would run the following command:
1 | $ chmod 644 notes |
644 把 notes 的权限设为 -rw-r--r--:6 给文件所有者 rw- 权限;4 给文件所属组 r-- 权限;4 给其他人 r-- 权限。
The
644sets the permissions onnotesto-rw-r—r--:6gives the file’s ownerrw-access;4gives the file’s groupr--access; and4gives otherr--access.
chmod 命令也可以使用 [u,g,o][+,-][permissions] 这种设置格式。例如,如果 sam 想给 users 组对 projects 目录的写权限,他可以用 g+w(给组加上写权限)来指定这个权限;如果他想去掉其他人对 notes 的读权限,可以指定 o-r(从其他人那里去掉读权限):
The
chmodcommand can also use the[u,g,o][+,-][permissions]setting format too. For example, ifsamwants to give theusersgroup write access to theprojectsdirectory, he can specify this permission usingg+w(add write permission to group), and if he wants to remove read access fromnotesto others, he can specifyo-r(remove read permission from all):
1 | $ chmod g+w projects |
当他意识到给 users 组对 projects 目录的写权限是个糟糕的主意时,可以用 g-w 去掉它:
And when he realizes that giving the
usersgroup write access to hisprojectsdirectory was a terrible idea, he can remove it usingg-w:
1 | $ chmod g-w projects |
chmod 命令还有其他命令行选项,包括一个递归选项,可以递归地修改整个子目录内容的权限。更多细节参见手册页。
The
chmodcommand 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
getfaclandsetfaclcontain 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
chgrpcommand 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
samis part of a group namedproj3with two other users,samcan change the group and set permissions on theirprojectsdirectory to give just this group elevated rights to the directory. For example:
1 | $ chgrp proj3 projects # change the projects directory's group to proj3 |
类似地,chown 命令修改文件的所有者,但你需要有 root 权限才能运行它。
Similarly, the
chowncommand 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 | $ echo $GROUP |
你的默认文件权限由 .bashrc 文件中的 umask 设置配置。可以用 umask 列出它的值:
1 | $ umask |
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 usingecho $GROUP:
1
2 $ echo $GROUP
GROUP=usersYour default file permission are configured from the
umasksetting in your.bashrcfile. You can list its value usingumask:
1
2 $ umask
77A
umaskof77(or077) 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.,7removes all permissions (rwx) inumaskbut 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)
- cheat-sheets.org 上的最常用的 Unix 命令
- most used Unix commands from cheat-sheets.org
- gnu.org 上的 Bash 参考手册。
- Bash Reference Manual from gnu.org.