My "umask" output is "022"- why can't I execute a file I just created without "chmod"?

huangapple go评论70阅读模式
英文:

My "umask" output is "022"- why can't I execute a file I just created without "chmod"?

问题

我有一个名为./foo的全新文件,内容如下:

#!/usr/bin/env bash

echo 'Hello world'

umask命令的输出如下:

$ umask -S
u=rwx,g=rx,o=rx

$ umask
022

但是,尽管我尚未运行chmod,当我尝试执行这个全新文件时,我会得到以下错误:

$ ./foo

zsh: permission denied: ./foo

值得注意的是,当我打开bash shell时,也会得到相同的错误:

$ bash

默认的交互式shell现在是zsh。
要更新您的帐户以使用zsh,请运行`chsh -s /bin/zsh`有关更多详细信息,请访问https://support.apple.com/kb/HT208050。

bash-3.2$ ./foo

bash: ./foo: Permission denied

当我使用ls -l检查文件的权限时,它似乎也不可执行:

$ ls -l foo

-rw-r--r--  1 richiethomas  staff  40 Mar  3 10:11 foo

然而,当我运行chmod命令时,它可以正常工作:

$ chmod +x foo

$ ./foo
Hello world

$ ls -l foo
-rwxr-xr-x  1 richiethomas  staff  40 Mar  3 10:11 foo

根据这个文档的信息,umask值为022表示“所有者具有所有权限。其他人可以读取和执行,但不能写入。”

如果umask告诉我文件的创建者(即我)默认情况下应该能够执行该文件,那么为什么事实上我不能执行它呢?

英文:

I have a brand-new file called ./foo, which looks like this:

#!/usr/bin/env bash

echo 'Hello world'

The output of the umask command looks like so:

$ umask -S
u=rwx,g=rx,o=rx

$ umask
022

Yet, when I try to execute the brand-new file (no chmod yet), I get the following:

$ ./foo

zsh: permission denied: ./foo

For what it's worth, I get the same thing when I open a bash shell:

$ bash

The default interactive shell is now zsh.
To update your account to use zsh, please run `chsh -s /bin/zsh`.
For more details, please visit https://support.apple.com/kb/HT208050.

bash-3.2$ ./foo

bash: ./foo: Permission denied

And when I inspect the file's permissions with ls -l, it also appears to not be executable:

$ ls -l foo

-rw-r--r--  1 richiethomas  staff  40 Mar  3 10:11 foo

When I chmod the file, however, it works as expected:

$ chmod +x foo

$ ./foo
Hello world

$ ls -l foo
-rwxr-xr-x  1 richiethomas  staff  40 Mar  3 10:11 foo

Using this document as a source, I learned that a umask value of 022 means "Owner has all permissions. Everyone else can read and execute, but not write."

If umask is telling me that a file's creator (i.e. me) should be able to execute the file by default, why am I not in fact able to do so?

答案1

得分: 2

以下是翻译好的部分:

"The only way I know to make a file executable immediately on creation without using hidden steps (like in a function or script) is to create it by copying another file that is already executable. You could use this to make a base template, then edit the file as needed."

"我知道的唯一一种在创建文件时立即使其可执行而不使用隐藏步骤(比如在函数或脚本中)的方法是通过复制另一个已经可执行的文件来创建它。您可以使用这种方法创建一个基本模板,然后根据需要编辑该文件。"

"...but in general, the default behaviors are always there for a reason. Please think about those reasons before changing them. I'm not saying you shouldn't customize your environment however is needed, just that you should be aware of the possible consequences in the specific context. This might save you a bit of time and a few spoons on a dev sandbox, but don't let it go to a company prod system without really thinking it through."

"...但总的来说,默认行为总是有其原因的。在更改它们之前,请考虑这些原因。我并不是说您不应该根据需要自定义您的环境,只是您应该了解特定背景下可能的后果。这可能会在开发沙箱中为您节省一些时间和精力,但不要不经过深思熟虑地将其应用到公司的生产系统中。"

英文:

The only way I know to make a file executable immediately on creation without using hidden steps (like in a function or script) is to create it by copying another file that is already executable. You could use this to make a base template, then edit the file as needed.

$ echo echo hi > a
$ ./a
-bash: ./a: Permission denied
$ chmod +x a
$ ./a
hi
$ cp a b
$ ./b
hi

...but in general, the default behaviors are always there for a reason. Please think about those reasons before changing them. I'm not saying you shouldn't customize your environment however is needed, just that you should be aware of the possible consequences in the specific context. This might save you a bit of time and a few spoons on a dev sandbox, but don't let it go to a company prod system without really thinking it through.

答案2

得分: 1

以下是翻译好的部分:

"您的umask值确实反映了您新创建文件的最终权限。

我了解到umask值为022表示“所有者拥有所有权限。其他人可以读取和执行,但不能写入。”

这是不正确的,umask022告诉您新创建的文件可以被所有人读取,但只能被所有者写入。

确实,文件的默认权限在创建时是666,而022会将其设置为644,这就是您最终得到的结果:

$ umask
022

$ touch file.txt
$ stat -c "%a" file.txt
644

$ ls -la
.rw-r--r-- someuser file.txt

如果您想要通过利用umask来使文件默认可执行,那是不可能的,因为创建时的默认值是666,要使文件对所有者可执行,至少需要766

要赋予其执行权限,您需要使用chmod,就像您在原始问题中所做的那样:

$ chmod 744 file.txt
$ ls -la
.rwxr--r-- someuser file.txt

如果您想要创建一个文件并使其可执行,可以创建一个像这样的函数放在~/.utils.sh文件中:

function touch_rwx() {
    touch $1
    chmod 744 $1
}

然后,您可以通过source命令来加载.sh文件,并获得以下结果:

$ touch_rwx file.txt
$ ls -la
.rwxr--r-- file.txt

如果您经常使用这个功能,建议将.utils.sh添加到您的PATH中,这样您可以在所有新创建的shell会话中随时使用它。"

英文:

The umask value that you have does reflect the final permissions of your newly created file.

> I learned that a umask value of 022 means "Owner has all permissions. Everyone else can read and execute, but not write."

This is incorrect, umask of 022 tells you that newly created files are readable by everyone, but only writable by the owner.

Indeed, default file permission is 666 upon creation and the 022 will set it to 644 which is what you end up with

$ umask
022

$ touch file.txt
$ stat -c "%a" file.txt
644

$ ls -la
.rw-r--r-- someuser file.txt

If you want to make a file executable by default by exploiting umask, then there is no way to achieve that because the default value upon creation is 666, and for a file to be executable by owner, you would need at least 766.

To give it execution rights you have to use chmod as you did in your OP

$ chmod 744 file.txt
$ ls -la
.rwxr--r-- someuser file.txt

There are some good explanations here on how umask works if you want to dig deeper into this.

If you want to create a file and make it executable with a single command, you could create a function like this in ~/.utils.sh

function touch_rwx() {
    touch $1
    chmod 744 $1
}

You could then source the .sh file and this is the outcome

$ touch_rwx file.txt
$ ls -la
.rwxr--r-- file.txt

If you use this frequently, I would suggest to add .utils.sh in your PATH so that you can use it whenever you want in all your newly created shell sessions.

huangapple
  • 本文由 发表于 2023年3月3日 21:16:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/75627561.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定