bash 为文件和文件夹设置不同的 Umask

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12445527/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 22:41:45  来源:igfitidea点击:

Set Different Umask For Files And Folders

bashpermissionsumask

提问by cryptic ツ

I am writing a bash script to update some files/directories and I want to run umask in the script to set default permissions for files/directories created by the script. I know I can umask to set the permissions for both files and directories however I need the permissions to be different for files and folders.

我正在编写一个 bash 脚本来更新一些文件/目录,我想在脚本中运行 umask 来为脚本创建的文件/目录设置默认权限。我知道我可以使用 umask 来设置文件和目录的权限,但是我需要文件和文件夹的权限不同。

I want files to be:   -rw----r-- (0604)
I want folders to be: drwx-----x (0701)

Can I do this using umask? If so how do I go about doing it? If not what is the best way to achieve it? Thank you in advance.

我可以使用 umask 做到这一点吗?如果是这样,我该怎么做?如果不是,实现它的最佳方法是什么?先感谢您。

回答by paxdiablo

Interesting requirement. Currently (at least in bash), umaskis a global setting and you cannot set it based on object type.

有趣的要求。当前(至少在 中bashumask是全局设置,您不能根据对象类型进行设置。

One solution that comes to mind would be to set the umaskto the file variant and then intercept calls to mkdir(such as with a user-created mkdirscript earlier in the path) to do:

想到的一种解决方案是将 设置umask为文件变体,然后拦截调用mkdir(例如使用mkdir路径中较早的用户创建的脚本)执行以下操作:

umask 0701 ; /path/to/real/mkdir  ; umask 0604

That way, assuming all your directory creations are done with mkdir, you can ensure they use a different umasksetting.

这样,假设您的所有目录创建都使用 完成mkdir,您可以确保它们使用不同的umask设置。

Note that the script should probably be a little more robust such as restoring the previous umaskrather than forcing it to 0604, and adding some better error checking and possibly the ability to handle multiple arguments.

请注意,脚本可能应该更健壮一些,例如恢复以前的umask而不是强制它0604,并添加一些更好的错误检查和可能处理多个参数的能力。

But that's all detail, the framework above should be enough to get you started.

但这就是所有细节,上面的框架应该足以让您入门。

回答by cdarke

The umaskis an attribute of the process not of a file - that is part of UNIX architecture and is nothing todo with Bash, or any other shell program.

umask是进程的一个属性,而不是一个文件——它是 UNIX 体系结构的一部分,与 Bash 或任何其他 shell 程序无关。

The real issue is that the programs you are using do not allow the permissions to be changed on creation. In C, for example, mkdirhas a second parameter, the mode.

真正的问题是您使用的程序不允许在创建时更改权限。例如,在 C 中,mkdir有第二个参数mode

You don't need to write C though, Python and Perl allow you to use the low-level interfaces. The permissions will be modified by the process's umaskso, if you don't want any modification, set unmaskto zero.

不过,您不需要编写 C,Python 和 Perl 允许您使用低级接口。权限将被进程修改umask,如果您不想进行任何修改,请设置unmask为零。

/home/user1> umask 000
/home/user1> python -c 'import os;os.mkdir("mydir",0701)'
/home/user1> ls -ld mydir
drwx-----x 2 user1 QAPLADV 4096 Sep 16 10:28 mydir

/home/user1> python -c 'import os;os.open("myfile",os.O_CREAT,0604)'
/home/user1> ls -l myfile
-rw----r-- 1 user1 QAPLADV 0 Sep 16 10:32 myfile

Don't forget that umaskis still 000 at this point, you might want to set it back to its previous value if you are doing any other work in the same process.

不要忘记此时umask仍为 000,如果您在同一进程中执行任何其他工作,您可能希望将其设置回之前的值。

Here is a Perl version if you prefer:

如果您愿意,这里有一个 Perl 版本:

perl -e "mkdir mydir,0701"
perl -MFcntl -e 'sysopen(my $h,"myfile",O_EXCL|O_CREAT,0604)'

Of course if you have a large number of files, and you are likely to be running this often, then you would be much better off writing a Perl or Python program to do the job - calling perl or python for each file is a tad inefficient.

当然,如果您有大量文件,并且您可能经常运行它,那么最好编写一个 Perl 或 Python 程序来完成这项工作 - 为每个文件调用 perl 或 python 有点低效.

回答by user3674179

Put following in the user's .bashrc:

将以下内容放在用户的 .bashrc 中:

umask 0222

乌面具 0222

alias mkdir='mkdir -m u=rx,g=rx,o='

别名 mkdir='mkdir -mu=rx,g=rx,o='

The file will be created with umask 0222, whereas mkdir will use umask 0227.

该文件将使用 umask 0222 创建,而 mkdir 将使用 umask 0227。