bash linux 文件和文件夹不继承父目录权限
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42241862/
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
linux files and folders are not inheriting parent directory permissions
提问by Rockshell
I created a directory /share
and gave chmod 2770
permission and chown root:stock /share
.
我创建了一个目录/share
并授予chmod 2770
权限和chown root:stock /share
.
1) When I create touch a file inside /share
, I see the file has rw-rw-r--
and I don't see rwxrws---
1)当我在里面创建一个文件时/share
,我看到文件有rw-rw-r--
,我没有看到rwxrws---
2) When I create a directory in /share/data
I see the permission as drwxrwsr-x
where are the parent directory is drwxrws---
2)当我在中创建目录时,/share/data
我看到的权限drwxrwsr-x
是父目录在哪里drwxrws---
How can I get parent child files and child directories to inherent parent permissions exactly the same.
如何让父子文件和子目录具有完全相同的固有父权限。
回答by Henk Langeveld
The setgid
bit on a directory makes new files inherit the group
from the directory, not its permissions.
setgid
目录上的位使新文件group
从目录继承,而不是其权限。
The standard way of controlling the bits that get set on the creation of a file is to control the umask(askubuntu) of the creating process, not the file system.
控制在创建文件时设置的位的标准方法是控制创建过程的umask(askubuntu),而不是文件系统。
回答by Dario
When you create a file or directory
创建文件或目录时
The owner of the new file or directory will be your effective user id (
euid
). You can change user id beforehand with thesu other_user
command (which will prompt you for the password ofother_user
), orsudo su other_user
(which will allow you or not, possibly asking for your password, according to the settings in/etc/sudoers*
). After creating the file or directory, you can change its owner withsudo chown other_user file_name
.The group of the new file or directory will be your effective group id. You can change your group id with the
newgrp other_group
command beforehand. If your current directory hasother_group
as group and itssetgid
bit is set, your effective group id will beother_group
. After creating the file or directory, you can change its group withchgrp other_group file_name
.newgrp
,chgrp
andsetgid
will work if you are a member ofother_group
. If you are not, they won't: a group password mechanism is theoretically still in place, but it was deprecated decades ago and I've never seen anybody using it. Of course, you can alwayssudo chgrp other_group file_name
, or evensudo chown other_user:other_group file_name
if you want to change both.The read and write permissions of the new file or directory will depend on your
umask
, which is normally set by your configuration files at login. The most used umask values are022
which, for files, will give you-rw-r--r--
and002
which will give you-rw-rw-r--
. The commandumask
will give you your current value. You can set another value withumask new_value
and it will be effective till you change it or exit your shell. Directories will have also all execution permissions set by default, unless you have odd values inumask
, which will block the corresponding execution bit. E.g. a umask value of027
will create files with-rw-r-----
and directories withdrwxrwx---
. Please refer to documentation for a complete explanation. Also, if the parent directory has thesetgid
bit, the new directory will have it too. There is no way of setting thesetuid
andsticky
bits by default, nor thesetgid
bit for files.After the fact, you can always set the permissions you want with the command
chmod
.
新文件或目录的所有者将是您的有效用户 ID (
euid
)。您可以使用su other_user
命令(这将提示您输入 的密码other_user
)或sudo su other_user
(根据 中的设置,允许或不允许,可能要求您输入密码)预先更改用户 ID/etc/sudoers*
。创建文件或目录后,您可以使用sudo chown other_user file_name
.新文件或目录的组将是您的有效组 ID。您可以
newgrp other_group
预先使用命令更改组 ID 。如果您的当前目录具有other_group
as 组并且其setgid
位已设置,则您的有效组 ID 将为other_group
. 创建文件或目录后,您可以使用chgrp other_group file_name
.newgrp
,chgrp
而setgid
如果你是一个成员会工作other_group
。如果你不是,他们就不会:组密码机制理论上仍然存在,但几十年前它已被弃用,我从未见过有人使用它。当然,您始终可以sudo chgrp other_group file_name
,或者即使sudo chown other_user:other_group file_name
您想同时更改两者。新文件或目录的读写权限将取决于您的
umask
,通常在登录时由您的配置文件设置。最常用的 umask 值是022
,对于文件,哪些会给你-rw-r--r--
,002
哪些会给你-rw-rw-r--
. 该命令umask
将为您提供当前值。您可以设置另一个值,umask new_value
它会一直有效,直到您更改它或退出您的 shell。目录还将默认设置所有执行权限,除非您在 中有奇数值umask
,这将阻止相应的执行位。例如,umask 值027
将创建文件-rw-r-----
和目录drwxrwx---
。请参阅文档以获得完整的解释。此外,如果父目录具有setgid
位,新目录也会有它。默认情况下无法设置setuid
和sticky
位,也无法设置setgid
文件位。事实上,您始终可以使用命令设置所需的权限
chmod
。
That said, there is no standard command which will do what you want. However, you can easily write bash functions like the following and use them (write them in a file mycreat_functions
and source mycreat_functions
when needed). This will do for manually created files and directories. For file created by programs, shell redirections and the like, you will still have to correct the permissions manually.
也就是说,没有标准命令可以执行您想要的操作。但是,你可以随便写bash的功能,如下面,并用它们(在文件中写他们mycreat_functions
,并source mycreat_functions
在需要时)。这将适用于手动创建的文件和目录。对于程序创建的文件、shell 重定向等,您仍然需要手动更正权限。
function mymkdir () {
local parentperms
for a in "$@"; do
mkdir "$a"
# This copies all permissions of the parent,
# exactly as they are
parentperms="$(stat -c%a $(dirname "$a"))"
chmod "$parentperms" "$a"
# if I'm root...
if [ $(id -u) = 0 ]; then
chown "$(stat -c%u:%g "$a")" "$a"
fi
done
}
function mytouch () {
local parentperms newperms
for a in "$@"; do
touch "$a"
# This inherits all permissions of the parent,
# but removes the excution and setgid bits, as is
# appropriate for files.
parentperms="$(stat -c%a $(dirname "$a"))"
newperms="$(printf %o $((8#$parentperms & 8#5666)))"
chmod "$newperms" "$a"
# if I'm root...
if [ $(id -u) = 0 ]; then
chown "$(stat -c%u:%g "$a")" "$a"
fi
done
}
Note:Owner, group and permissions are stored in an inode,where there is also other information on how to retrieve the file contents; the directory entryassociates the inode with the file name, and ls -i
shows the inode numbersof the listed files. When you copya file, you create a new directory entry and allocate a new inode, so everything mentioned here applies. When you movea file, you create a new directory entry in the new location, but have it point to the old inode, so that owner, group and permissions are effectively untouched. If you want them to change according to the new directory entry's parent, you have to create a mymv
function along the lines of mytouch
and mymkdir
above.
注意:所有者、组和权限存储在一个inode 中,其中还有关于如何检索文件内容的其他信息;该目录条目关联的文件名的索引节点,并ls -i
显示了inode编号列出的文件。当你复制一个文件时,你会创建一个新的目录条目并分配一个新的 inode,所以这里提到的所有内容都适用。当你移动一个文件时,你会在新位置创建一个新的目录条目,但让它指向旧的 inode,这样所有者、组和权限实际上不会受到影响。如果您希望它们根据新目录条目的父项进行更改,则必须按照以下方式创建一个mymv
函数mytouch
及mymkdir
以上。