控制 UNIX 目录内容用户组所有权

控制 UNIX 目录内容用户组所有权

我想创建一个目录,任何用户都可以在其中创建文件,但创建文件后,该文件的用户和组所有权将设置为预定义值。我知道有一种方法可以使用 GUID、UID 来实现这一点,但我总是忘记 chmod 组合。

答案1

您可以对 GID 执行此操作:

chgrp users /path/to/directory
chmod g+s /path/to/directory

您无法直接对 UID 执行此操作。

您可以编写一个 cron 条目,每隔一段时间运行以下命令:

chown -R user:group /path/to/directory
chmod -R 644 /path/to/directory

(确保将 /path/to/directory 上的文件系统挂载在 nosuid 上,以避免出现有人将 setuid shell 放入目录中的竞争条件)

答案2

chown user:group file

答案3

我很抱歉这么说,但请阅读全文。来自chmod(1)

       Modes may be absolute or symbolic.  An absolute mode is an octal number
       constructed from the sum of one or more of the following values:

       4000    (the setuid bit).  Executable files with this bit set will
               run with effective uid set to the uid of the file owner.
               Directories with this bit set will force all files and sub-
               directories created in them to be owned by the directory
               owner and not by the uid of the creating process, if the
               underlying file system supports this feature: see chmod(2)
               and the suiddir option to mount(8).
       2000    (the setgid bit).  Executable files with this bit set will
               run with effective gid set to the gid of the file owner.
       1000    (the sticky bit).  See chmod(2) and sticky(8).
       0400    Allow read by owner.
       0200    Allow write by owner.
       0100    For files, allow execution by owner.  For directories,
               allow the owner to search in the directory.
       0040    Allow read by group members.
       0020    Allow write by group members.
       0010    For files, allow execution by group members.  For directo-
               ries, allow group members to search in the directory.
       0004    Allow read by others.
       0002    Allow write by others.
       0001    For files, allow execution by others.  For directories
               allow others to search in the directory.

相关内容