Supondo que você esteja usando o GNU chmod
, isso está documentado na página do manual :
chmod
preserves a directory's set-user-ID and set-group-ID bits unless you explicitly specify otherwise. You can set or clear the bits with symbolic modes likeu+s
andg-s
, and you can set (but not clear) the bits with a numeric mode.
Isso é permitido em POSIX :
For each bit set in the octal number, the corresponding file permission bit shown in the following table shall be set; all other file permission bits shall be cleared. For regular files, for each bit set in the octal number corresponding to the set-user-ID-on-execution or the set-group-ID-on-execution, bits shown in the following table shall be set; if these bits are not set in the octal number, they are cleared. For other file types, it is implementation-defined whether or not requests to set or clear the set-user-ID-on-execution or set-group-ID-on-execution bits are honored.
O raciocínio para o comportamento no GNU chmod
é dado na notas de lançamento para coreutils
6.0 :
chmod
,install
, andmkdir
now preserve a directory's set-user-ID and set-group-ID bits unless you explicitly request otherwise. E.g.,chmod 755 DIR
andchmod u=rwx,go=rx DIR
now preserveDIR
's set-user-ID and set-group-ID bits instead of clearing them, and similarly formkdir -m 755 DIR
andmkdir -m u=rwx,go=rx DIR
. To clear the bits, mention them explicitly in a symbolic mode, e.g.,mkdir -m u=rwx,go=rx,-s DIR
. To set them, mention them explicitly in either a symbolic or a numeric mode, e.g.,mkdir -m 2755 DIR
,mkdir -m u=rwx,go=rx,g+s DIR
. This change is for convenience on systems where these bits inherit from parents. Unfortunately other operating systems are not consistent here, and portable scripts cannot assume the bits are set, cleared, or preserved, even when the bits are explicitly mentioned. For example, OpenBSD 3.9mkdir -m 777 D
preservesD
's setgid bit butchmod 777 D
clears it. Conversely, Solaris 10mkdir -m 777 D
,mkdir -m g-s D
, andchmod 0777 D
all preserveD
's setgid bit, and you must use something likechmod g-s D
to clear it.
Há mais sobre o assunto em # 8391 , incluindo a justificativa adicional de que o líder 0 é ambíguo (pode indicar bits limpos ou um valor octal na mente do usuário). O manual coreutils
também tem uma seção dedicada, Diretórios e os Set-User-ID e Set-Group-ID Bits ; isso revela que existem extensões GNU para permitir a limpeza dos bits em questão:
chmod =700 ~/testdir
chmod 00700 ~/testdir
limpam os bits (mas não são portáteis).