为Linux目录下新创建的文件和子目录设置默认权限?
我有一堆长时间运行的脚本和应用程序,它们将输出结果存储在几个用户共享的目录中。 我想要一种方法来确保在此共享目录下创建的每个文件和目录自动具有 u=rwxg=rwxo=r
权限。
我知道我可以在各种脚本的开头使用 umask 006,但我不喜欢这种方法,因为许多用户编写自己的脚本,并且可能忘记自己设置 umask。
我真的只是希望文件系统将新创建的文件和目录设置为具有特定权限(如果它位于某个文件夹中)。 这是可能吗?
更新:我认为可以通过POSIX ACL,使用默认 ACL 功能,但目前这一切都有点超出我的理解范围。 如果有人可以解释如何使用默认 ACL,它可能会很好地回答这个问题。
I have a bunch of long-running scripts and applications that are storing output results in a directory shared amongst a few users. I would like a way to make sure that every file and directory created under this shared directory automatically had u=rwxg=rwxo=r
permissions.
I know that I could use umask 006
at the head off my various scripts, but I don't like that approach as many users write their own scripts and may forget to set the umask themselves.
I really just want the filesystem to set newly created files and directories with a certain permission if it is in a certain folder. Is this at all possible?
Update: I think it can be done with POSIX ACLs, using the Default ACL functionality, but it's all a bit over my head at the moment. If anybody can explain how to use Default ACLs it would probably answer this question nicely.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
要获得正确的所有权,您可以在目录上设置组 setuid 位,
这将确保在目录中创建的文件归该组所有。 然后,您应该确保每个人都使用 umask 002 或 007 或类似的东西运行——这就是为什么 Debian 和许多其他 Linux 系统默认配置了每用户组。
如果用户的 umask 太强,我不知道有什么方法可以强制获得您想要的权限。
To get the right ownership, you can set the group setuid bit on the directory with
This will ensure that files created in the directory are owned by the group. You should then make sure everyone runs with umask 002 or 007 or something of that nature---this is why Debian and many other linux systems are configured with per-user groups by default.
I don't know of a way to force the permissions you want if the user's umask is too strong.
以下是使用默认 ACL 的方法(至少在 Linux 下)。
首先,您可能需要在文件系统上启用 ACL 支持。 如果您使用的是 ext4,那么它已经启用。 其他文件系统(例如 ext3)需要使用
acl
选项挂载。 在这种情况下,请将该选项添加到您的/etc/fstab
中。 例如,如果该目录位于您的根文件系统上:然后重新挂载它:
现在,使用以下命令设置默认 ACL:
/shared/directory
中的所有新文件现在应该获得所需的权限。 当然,这也取决于创建该文件的应用程序。 例如,大多数文件从一开始就无法被任何人执行(取决于 open(2) 或 creat(2) 调用的模式参数),就像使用 umask 时一样。 一些实用程序(例如 cp、tar 和 rsync)将尝试保留源文件的权限,这将屏蔽您的默认 ACL如果源文件不可组写。希望这可以帮助!
Here's how to do it using default ACLs, at least under Linux.
First, you might need to enable ACL support on your filesystem. If you are using ext4 then it is already enabled. Other filesystems (e.g., ext3) need to be mounted with the
acl
option. In that case, add the option to your/etc/fstab
. For example, if the directory is located on your root filesystem:Then remount it:
Now, use the following command to set the default ACL:
All new files in
/shared/directory
should now get the desired permissions. Of course, it also depends on the application creating the file. For example, most files won't be executable by anyone from the start (depending on the mode argument to the open(2) or creat(2) call), just like when using umask. Some utilities likecp
,tar
, andrsync
will try to preserve the permissions of the source file(s) which will mask out your default ACL if the source file was not group-writable.Hope this helps!
在您的 shell 脚本(或
.bashrc
)中,您可以使用如下内容:umask 022
umask
是一个命令,用于确定掩码的设置,控制如何为新创建的文件设置文件权限。in your shell script (or
.bashrc
) you may use somthing like:umask 022
umask
is a command that determines the settings of a mask that controls how file permissions are set for newly created files.它很难看,但您可以使用 setfacl 命令来实现您想要的效果。
在 Solaris 计算机上,我有一个包含用户和组的 acl 的文件。 不幸的是,您必须列出所有用户(至少我找不到其他方法来实现此目的):
将文件命名为 acl.lst 并填写您的真实用户名而不是 user_X。
现在,您可以通过发出以下命令在目录上设置这些 acl:
It's ugly, but you can use the setfacl command to achieve exactly what you want.
On a Solaris machine, I have a file that contains the acls for users and groups. Unfortunately, you have to list all of the users (at least I couldn't find a way to make this work otherwise):
Name the file acl.lst and fill in your real user names instead of user_X.
You can now set those acls on your directory by issuing the following command:
我认为这不会完全满足您的要求,但我只是想把它扔在那里,因为我没有在其他答案中看到它。
我知道您可以使用
-m
选项一次性创建具有权限的目录:并且您还可以使用
install
命令:I don't think this will do entirely what you want, but I just wanted to throw it out there since I hadn't seen it in the other answers.
I know you can create directories with permissions in a one-liner using the
-m
option:and you can also use the
install
command: