Linux 是一个典型的多用户系统,这就有必要提供许可制度来控制对文件和目录的操作,其中包含了所有的系统资源和设备(在Unix 系统中,任何设备都由文件或目录来表示)。这一原则对所有Unix 系统通用,这里再提醒一下,特别是对一些有趣的相对高级应用。
9.3.1. Owners and Permissions
每个文件和文件夹有三种用户许可类型:
Three basic types of rights can be combined:
读取(使用符号 r
“read”的首字母);
写(使用符号 w
“write”的首字母);
执行(使用“eXecute”中的符号 x
)。
以文件来说,其权限较容易理解:读取就是允许读取其内容 (包括复制)、写入就是允许改变它、而运行就是执行它 (文件本身必须是程序)。
A directory is handled differently. Read access gives the right to consult the list of its entries (files and directories), write access allows creating or deleting files, and execute access allows “traversal“, the possibility crossing through it (especially to go there with the cd
command). Being able to cross through a directory without being able to read it gives permission to access the entries therein that are known by name, but not to find them if you do not know their existence or their exact name.
有三个控制文件许可权限的命令:
有两种方法表示权限。其中,符号表示是最易于理解和记忆的。它使用前述的符号链接。可以通过显示的设置(u
/g
/o
),通过设置(=
),加(+
),或者减(-
)定义每种用户的权限类型。一个 u=rwx,g+rw,o-r
格式的命令会赋予所有者读,写和执行权限,给所有组添加读写权限,移除其他用户的读权限。其他命令中未通过加或者减列出的权限保持不变。字母 a
是指“所有”,涵盖三种类型的用户,因此 a=rx
命令会赋予三种用户相同的权限(读和执行,没有写)。
与权限相关的(八进制)数字表示:4是读,2是写,1是执行。各种权限组合通过代表的数字求和得到。通过把每个值置于端到端序列不同位置关联不同的用户类型(所有者,所有组,其他用户)。
For instance, the chmod 754 file
command will set the following rights: read, write and execute for the owner (since 7 = 4 + 2 + 1); read and execute for the group (since 5 = 4 + 1); read-only for others. The 0
(zero) means no rights; thus chmod 600 file
allows for read/write rights for the owner, and no rights for anyone else. The most frequent right combinations are 755
for executable files and directories, and 644
for data files.
要表示特殊权限,可以根据同样的原则在数字上加入第四个前缀位,位 setuid
, setgid
和 sticky
分别对应4,2,和1。chmod 4754
会设置前面描述的 setuid
位权限。
八进位标记只适用于对文件的一次性设置所有权限;不能以它加入新的权限,如群组拥有者的读取,因为必须把现在的权限与计算新的数值。
9.3.2. ACLs - Access Control Lists
Many filesystems, e.g. Btrfs, Ext3, Ext4, JFS, XFS, etc., support the use of Access Control Lists (ACLs). These extend the basic features of file ownership and permission, described in the previous section, and allow for a more fine-grained control of each (file) object. For example: A user wants to share a file with another user and that user should only be able to read the file, but not write or change it.
For some of the filesystems, the usage of ACLs is enabled by default (e.g. Btrfs, Ext3, Ext4). For other filesystems or older systems it must be enabled using the acl
mount option - either in the mount
command directly or in /etc/fstab
. In the same way the usage of ACLs can be disabled by using the noacl
mount option. For Ext* filesystems one can also use the tune2fs -o [no]acl /dev/device
command to enable/disable the usage of ACLs by default. The default values for each filesystem can usually be found in their homonym manual pages in section 5 (filesystem(5)) or in mount(8).
After enabling ACLs, permissions can be set using the setfacl(1) command, while getfacl(1) allows one to retrieve the ACLs for a given object or path. These commands are part of the acl package. With setfacl
one can also configure newly created files or directories to inherit permissions from the parent directory. It is important to note that ACLs are processed in their order and that an earlier entry that fits the situation has precedence over later entries.
If a file has ACLs set, the output of the ls -l
command will show a plus-sign after the traditional permissions. When using ACLs, the chmod
command behaves slightly different, and umask
might be ignored. The extensive documentation, e.g. acl(5) contains more information.