linux 文件和文件夹未继承父目录权限
linux files and folders are not inheriting parent directory permissions
我创建了一个目录 /share
并授予了 chmod 2770
权限和 chown root:stock /share
.
1) 当我在 /share
中创建 touch 文件时,我看到该文件有 rw-rw-r--
而我没有看到 rwxrws---
2) 当我在 /share/data
中创建一个目录时,我看到权限为 drwxrwsr-x
,其中父目录是 drwxrws---
如何让父子文件和子目录的固有父权限完全相同。
目录上的 setgid
位使新文件继承目录的 group
,而不是其权限。
控制创建文件时设置的位的标准方法是控制创建进程的 umask (askubuntu),而不是文件系统。
创建文件或目录时
新文件或目录的所有者将是您的有效用户 ID (euid
)。您可以使用 su other_user
命令(这将提示您输入 other_user
的密码)或 sudo su other_user
(这将允许或不允许,可能会询问您的密码,根据 /etc/sudoers*
中的设置)。创建文件或目录后,您可以使用 sudo chown other_user file_name
.
更改其所有者
新文件或目录所在的组将是您的有效组ID。您可以事先使用 newgrp other_group
命令更改您的组 ID。如果您的当前目录有 other_group
作为组并且其 setgid
位已设置,则您的有效组 ID 将为 other_group
。创建文件或目录后,您可以使用 chgrp other_group file_name
更改其组。如果您是 other_group
的成员,newgrp
、chgrp
和 setgid
将有效。如果你不是,他们就不会:理论上组密码机制仍然存在,但它在几十年前就被弃用了,我从未见过有人使用它。当然,你总是可以 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
中有奇数值,这将阻止相应的执行位。例如。 027
的 umask 值将创建 -rw-r-----
的文件和 drwxrwx---
的目录。请参阅文档以获得完整的解释。此外,如果父目录具有 setgid
位,则新目录也将具有它。默认情况下无法设置 setuid
和 sticky
位,也无法设置文件的 setgid
位。
事后,您始终可以使用命令 chmod
.
设置您想要的权限
就是说,没有标准命令可以满足您的需求。但是,您可以像下面这样轻松编写 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
}
注意:所有者、组和权限存储在inode中,在那里也是关于如何检索文件内容的其他信息; 目录条目 将 inode 与文件名相关联,ls -i
显示列出文件的 inode 编号 。当您 复制 一个文件时,您会创建一个新的目录条目并分配一个新的 inode,因此这里提到的所有内容都适用。当您 移动 一个文件时,您会在新位置创建一个新的目录条目,但让它指向旧的 inode,这样所有者、组和权限实际上不会受到影响。如果您希望它们根据新目录条目的父级进行更改,则必须按照上面的 mytouch
和 mymkdir
行创建一个 mymv
函数。
我创建了一个目录 /share
并授予了 chmod 2770
权限和 chown root:stock /share
.
1) 当我在 /share
中创建 touch 文件时,我看到该文件有 rw-rw-r--
而我没有看到 rwxrws---
2) 当我在 /share/data
中创建一个目录时,我看到权限为 drwxrwsr-x
,其中父目录是 drwxrws---
如何让父子文件和子目录的固有父权限完全相同。
目录上的 setgid
位使新文件继承目录的 group
,而不是其权限。
控制创建文件时设置的位的标准方法是控制创建进程的 umask (askubuntu),而不是文件系统。
创建文件或目录时
新文件或目录的所有者将是您的有效用户 ID (
euid
)。您可以使用su other_user
命令(这将提示您输入other_user
的密码)或sudo su other_user
(这将允许或不允许,可能会询问您的密码,根据/etc/sudoers*
中的设置)。创建文件或目录后,您可以使用sudo chown other_user file_name
. 更改其所有者
新文件或目录所在的组将是您的有效组ID。您可以事先使用
newgrp other_group
命令更改您的组 ID。如果您的当前目录有other_group
作为组并且其setgid
位已设置,则您的有效组 ID 将为other_group
。创建文件或目录后,您可以使用chgrp other_group file_name
更改其组。如果您是other_group
的成员,newgrp
、chgrp
和setgid
将有效。如果你不是,他们就不会:理论上组密码机制仍然存在,但它在几十年前就被弃用了,我从未见过有人使用它。当然,你总是可以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
中有奇数值,这将阻止相应的执行位。例如。027
的 umask 值将创建-rw-r-----
的文件和drwxrwx---
的目录。请参阅文档以获得完整的解释。此外,如果父目录具有setgid
位,则新目录也将具有它。默认情况下无法设置setuid
和sticky
位,也无法设置文件的setgid
位。事后,您始终可以使用命令
chmod
. 设置您想要的权限
就是说,没有标准命令可以满足您的需求。但是,您可以像下面这样轻松编写 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
}
注意:所有者、组和权限存储在inode中,在那里也是关于如何检索文件内容的其他信息; 目录条目 将 inode 与文件名相关联,ls -i
显示列出文件的 inode 编号 。当您 复制 一个文件时,您会创建一个新的目录条目并分配一个新的 inode,因此这里提到的所有内容都适用。当您 移动 一个文件时,您会在新位置创建一个新的目录条目,但让它指向旧的 inode,这样所有者、组和权限实际上不会受到影响。如果您希望它们根据新目录条目的父级进行更改,则必须按照上面的 mytouch
和 mymkdir
行创建一个 mymv
函数。