chmod详解

摘要:本文介绍 chmod 修改权限,处理 Linuxchmod 命令外,还介绍了 rsync--chmod 选项的使用方法。

Linux chmod 命令

理解这个命令只需要理解 Linux 的 UGO 的授权管理系统。

U:user,G:group,O:other

所以我们在授权的时候需要制定这三种用户的权限。

而权限包括:rwxXst

r:read,w:write,x:execute,另外三个不常用:Xst

X:权限设定,如果目标文件是可执行文件或目录,可给其设置可执行权限

s:权限设定,设置权限suid和sgid,使用权限组合“u+s”设定文件的用户的ID位,“g+s”设置组ID位。表示setUID 和setGID 。位于user和group权限组的第三位置。如果在user权限组中设置了s权限。那么当该文件被执行时,以文件所有者的GID,而不是用户的GID执行文件

t:权限设定,只有目录或文件的所有者才可以删除目录下的文件。 表示站着位(sticky)。位于other权限组的第三位,具有该位的文件和目录只有创建该文件的user才能删除。

参考该文章该文章

The letters rwxXst select file mode bits for the affected users: read (r), write (w), execute (or search for directories) (x), execute/search only if the file is a directory or already has execute permission for some user (X), set user or group ID on execution (s), restricted deletion flag or sticky bit (t). Instead of one or more of these letters, you can specify exactly one of the letters ugo: the permissions granted to the user who owns the file (u), the permissions granted to other users who are members of the file’s group (g), and the permissions granted to users that are in neither of the two preceding categories (o).

rsync —chmod 选项

—chmod
This option tells rsync to apply one or more comma-separated lqchmodrq strings to the permission of the files in the transfer. The resulting value is treated as though it was the permissions that the sending side supplied for the file, which means that this option can seem to have no effect on existing files if —perms is not enabled.
In addition to the normal parsing rules specified in the chmod(1)
manpage, you can specify an item that should only apply to a directory by prefixing it with a oqDcq, or specify an item that should only apply to a file by prefixing it with a oqFcq. For example:
—chmod=Dg+s,ug+w,Fo-w,+X
It is also legal to specify multiple —chmod options, as each
additional option is just appended to the list of changes to make.
See the —perms and —executability options for how the resulting
permission value can be applied to the files in the transfer.

该选项主要是用于告诉 rsync 在文件传输的时候设定权限。

除了类似 Linux 的规则,还支持跨操作系统的设定。这时候就用到了 F(ile) 和 D(irectory),比如从 Windows 拷贝文件到 Linux,可以使用如下命令设定权限

参考该 Stack Overflow 回答

1
--chmod=Du=rwx,Dgo=rx,Fu=rw,Fog=r

将会设定 Directories 755 权限和 Files 644 权限,当然同样支持8进制模式。

the —chmod also accepts octal mode numbers, which in my opinion is easier to visualize the permission given: —chmod=D2775,F664

关于 D2775 解释

参考该文章

s for the group - When somebody is allowed to execute a script (user or other) then it’s executed as if it was executed by the group. This is called a “set group id bit” or “sgid” and can be set by appending a 2 to a permission set. For example: chmod 2775 object. This bit on a directory means all files in that directory that will be created, will be owned by the group that owns the directory.