I can change a directory to setgid with chmod 2775. Shouldn't I be able to unset it with chmod 0775?
1 Answer 1
This behavior is documented for the GNU implementation of chmod - and has apparently been replicated (although not similarly documented) in the newer uutils implementation.
From man gnuchmod:
For directories chmod preserves set-user-ID and set-group-ID bits unless you explicitly specify otherwise. You can set or clear the bits with symbolic modes like u+s and g-s. To clear these bits for directories with a numeric mode requires an additional leading zero like 00755, leading minus like -6000, or leading equals like =755.
So for example
$ ls -ld dir1
drwxrwxr-x 3 steeldriver steeldriver 4096 May 2 17:24 dir1
$ chmod --version
chmod (uutils coreutils) 0.8.0
$ chmod --verbose 2775 dir1
mode of 'dir1' changed from 0775 (rwxrwxr-x) to 2775 (rwxrwsr-x)
$ chmod --verbose 0775 dir1
mode of 'dir1' retained as 2775 (rwxrwsr-x)
but
$ chmod --verbose 00775 dir1
mode of 'dir1' changed from 2775 (rwxrwsr-x) to 0775 (rwxrwxr-x)
See also Permissions remain 2700 after chmod 0700.
-
unexpected but so be it. Does this mean the short answer to the question is that instead of chmod 0775, you need to prefix the 775 with two digits, as in 00775? I just did a quick test on Xubuntu 26.04 and indeed 00775 reversed the permissions of chmod 2775quill– quill2026-05-29 02:08:34 +00:00Commented May 29 at 2:08
-
-
@quill or use symbolic mode
g-ssteeldriver– steeldriver2026-05-29 02:10:53 +00:00Commented May 29 at 2:10 -
This is compliant with POSIX, by the way. POSIX states that the behavior is implementation-defined [bold italic emphasis mine]: "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."Jörg W Mittag– Jörg W Mittag2026-05-30 13:23:03 +00:00Commented May 30 at 13:23