Skip to main content
Typo, quotes.
Source Link
Stephen Kitt
  • 482.9k
  • 60
  • 1.2k
  • 1.4k

I'llI’ll answer your questions in three parts: file types, permissions, and use cases for the various forms of chmod.

The first character in ls -l output represents the file type; d means it'sit’s a directory. It can'tcan’t be set or unset, it depends on how the file was created. You can find the complete list of file types in the ls documentation; those you'reyou’re likely to come across are

  • -: "regular"“regular” file, created with any program which can write a file
  • b: block special file, typically disk or partition devices, can be created with mknod
  • c: character special file, can also be created with mknod (see /dev for examples)
  • d: directory, can be created with mkdir
  • l: symbolic link, can be created with ln -s
  • p: named pipe, can be created with mkfifo
  • s: socket, can be created with nc -U
  • D: door, created by some server processes on Solaris/openindiana.
  • suid, sgid and "sticky"“sticky” (see below)
  • user permissions
  • group permissions
  • "other"“other” permissions
  • "read"“read” is 4
  • "write"“write” is 2
  • "execute"“execute” is 1
  • suid is 4; binaries with this bit set run as their owner user (commonly root)
  • sgid is 2; binaries with this bit set run as their owner group (this was used for games so high scores could be shared, but it'sit’s often a security risk when combined with vulnerabilities in the games), and files created in directories with this bit set belong to the directory'sdirectory’s owner group by default (this is handy for creating shared folders)
  • "sticky"“sticky” (or "restricted deletion"“restricted deletion”) is 1; files in directories with this bit set can only be deleted by their owner, the directory'sdirectory’s owner, or root (see /tmp for a common example of this).

See the chmod manpage for details. Note that in all this I'mI’m ignoring other security features which can alter users'users’ permissions on files (SELinux, file ACLs...).

Special bits are handled differently depending on the type of file (regular file or directory) and the underlying system. (This is mentioned in the chmod manpage.) On the system I used to test this (with coreutils 8.23 on an ext4 filesystem, running Linux kernel 3.16.7-ckt2), the behaviousbehaviour is as follows. For a file, the special bits are always cleared unless explicitly set, so chmod 0777 is equivalent to chmod 777, and both commands clear the special bits and give everyone full permissions on the file. For a directory, the special bits are never fully cleared using the four-digit numeric form, so in effect chmod 0777 is also equivalent to chmod 777 but it'sit’s misleading since some of the special bits will remain as-is. (A previous version of this answer got this wrong.) To clear special bits on directories you need to use u-s, g-s and/or o-t explicitly or specify a negative numeric value, so chmod -7000 will clear all the special bits on a directory.

In ls -l output, suid, sgid and "sticky"“sticky” appear in place of the x entry: suid is s or S instead of the user'suser’s x, sgid is s or S instead of the group'sgroup’s x, and "sticky"“sticky” is t or T instead of others'others’ x. A lower-case letter indicates that both the special bit and the executable bit are set; an upper-case letter indicates that only the special bit is set.

Because of the behaviour described above, using the full four digits in chmod can be confusing (at least it turns out I was confused). It'sIt’s useful when you want to set special bits as well as permission bits; otherwise the bits are cleared if you'reyou’re manipulating a file, preserved if you'reyou’re manipulating a directory. So chmod 2750 ensures you'llyou’ll get at least sgid and exactly u=rwx,g=rx,o=; but chmod 0750 won'twon’t necessarily clear the special bits.

Using numeric modes instead of text commands ([ugo][=+-][rwxXst]) is probably more a case of habit and the aim of the command. Once you'reyou’re used to using numeric modes, it'sit’s often easier to just specify the full mode that way; and it'sit’s useful to be able to think of permissions using numeric modes, since many other commands can use them (install, mknod...).

I'll answer your questions in three parts: file types, permissions, and use cases for the various forms of chmod.

The first character in ls -l output represents the file type; d means it's a directory. It can't be set or unset, it depends on how the file was created. You can find the complete list of file types in the ls documentation; those you're likely to come across are

  • -: "regular" file, created with any program which can write a file
  • b: block special file, typically disk or partition devices, can be created with mknod
  • c: character special file, can also be created with mknod (see /dev for examples)
  • d: directory, can be created with mkdir
  • l: symbolic link, can be created with ln -s
  • p: named pipe, can be created with mkfifo
  • s: socket, can be created with nc -U
  • D: door, created by some server processes on Solaris/openindiana.
  • suid, sgid and "sticky" (see below)
  • user permissions
  • group permissions
  • "other" permissions
  • "read" is 4
  • "write" is 2
  • "execute" is 1
  • suid is 4; binaries with this bit set run as their owner user (commonly root)
  • sgid is 2; binaries with this bit set run as their owner group (this was used for games so high scores could be shared, but it's often a security risk when combined with vulnerabilities in the games), and files created in directories with this bit set belong to the directory's owner group by default (this is handy for creating shared folders)
  • "sticky" (or "restricted deletion") is 1; files in directories with this bit set can only be deleted by their owner, the directory's owner, or root (see /tmp for a common example of this).

See the chmod manpage for details. Note that in all this I'm ignoring other security features which can alter users' permissions on files (SELinux, file ACLs...).

Special bits are handled differently depending on the type of file (regular file or directory) and the underlying system. (This is mentioned in the chmod manpage.) On the system I used to test this (with coreutils 8.23 on an ext4 filesystem, running Linux kernel 3.16.7-ckt2), the behavious is as follows. For a file, the special bits are always cleared unless explicitly set, so chmod 0777 is equivalent to chmod 777, and both commands clear the special bits and give everyone full permissions on the file. For a directory, the special bits are never fully cleared using the four-digit numeric form, so in effect chmod 0777 is also equivalent to chmod 777 but it's misleading since some of the special bits will remain as-is. (A previous version of this answer got this wrong.) To clear special bits on directories you need to use u-s, g-s and/or o-t explicitly or specify a negative numeric value, so chmod -7000 will clear all the special bits on a directory.

In ls -l output, suid, sgid and "sticky" appear in place of the x entry: suid is s or S instead of the user's x, sgid is s or S instead of the group's x, and "sticky" is t or T instead of others' x. A lower-case letter indicates that both the special bit and the executable bit are set; an upper-case letter indicates that only the special bit is set.

Because of the behaviour described above, using the full four digits in chmod can be confusing (at least it turns out I was confused). It's useful when you want to set special bits as well as permission bits; otherwise the bits are cleared if you're manipulating a file, preserved if you're manipulating a directory. So chmod 2750 ensures you'll get at least sgid and exactly u=rwx,g=rx,o=; but chmod 0750 won't necessarily clear the special bits.

Using numeric modes instead of text commands ([ugo][=+-][rwxXst]) is probably more a case of habit and the aim of the command. Once you're used to using numeric modes, it's often easier to just specify the full mode that way; and it's useful to be able to think of permissions using numeric modes, since many other commands can use them (install, mknod...).

I’ll answer your questions in three parts: file types, permissions, and use cases for the various forms of chmod.

The first character in ls -l output represents the file type; d means it’s a directory. It can’t be set or unset, it depends on how the file was created. You can find the complete list of file types in the ls documentation; those you’re likely to come across are

  • -: “regular” file, created with any program which can write a file
  • b: block special file, typically disk or partition devices, can be created with mknod
  • c: character special file, can also be created with mknod (see /dev for examples)
  • d: directory, can be created with mkdir
  • l: symbolic link, can be created with ln -s
  • p: named pipe, can be created with mkfifo
  • s: socket, can be created with nc -U
  • D: door, created by some server processes on Solaris/openindiana.
  • suid, sgid and “sticky” (see below)
  • user permissions
  • group permissions
  • “other” permissions
  • “read” is 4
  • “write” is 2
  • “execute” is 1
  • suid is 4; binaries with this bit set run as their owner user (commonly root)
  • sgid is 2; binaries with this bit set run as their owner group (this was used for games so high scores could be shared, but it’s often a security risk when combined with vulnerabilities in the games), and files created in directories with this bit set belong to the directory’s owner group by default (this is handy for creating shared folders)
  • “sticky” (or “restricted deletion”) is 1; files in directories with this bit set can only be deleted by their owner, the directory’s owner, or root (see /tmp for a common example of this).

See the chmod manpage for details. Note that in all this I’m ignoring other security features which can alter users’ permissions on files (SELinux, file ACLs...).

Special bits are handled differently depending on the type of file (regular file or directory) and the underlying system. (This is mentioned in the chmod manpage.) On the system I used to test this (with coreutils 8.23 on an ext4 filesystem, running Linux kernel 3.16.7-ckt2), the behaviour is as follows. For a file, the special bits are always cleared unless explicitly set, so chmod 0777 is equivalent to chmod 777, and both commands clear the special bits and give everyone full permissions on the file. For a directory, the special bits are never fully cleared using the four-digit numeric form, so in effect chmod 0777 is also equivalent to chmod 777 but it’s misleading since some of the special bits will remain as-is. (A previous version of this answer got this wrong.) To clear special bits on directories you need to use u-s, g-s and/or o-t explicitly or specify a negative numeric value, so chmod -7000 will clear all the special bits on a directory.

In ls -l output, suid, sgid and “sticky” appear in place of the x entry: suid is s or S instead of the user’s x, sgid is s or S instead of the group’s x, and “sticky” is t or T instead of others’ x. A lower-case letter indicates that both the special bit and the executable bit are set; an upper-case letter indicates that only the special bit is set.

Because of the behaviour described above, using the full four digits in chmod can be confusing (at least it turns out I was confused). It’s useful when you want to set special bits as well as permission bits; otherwise the bits are cleared if you’re manipulating a file, preserved if you’re manipulating a directory. So chmod 2750 ensures you’ll get at least sgid and exactly u=rwx,g=rx,o=; but chmod 0750 won’t necessarily clear the special bits.

Using numeric modes instead of text commands ([ugo][=+-][rwxXst]) is probably more a case of habit and the aim of the command. Once you’re used to using numeric modes, it’s often easier to just specify the full mode that way; and it’s useful to be able to think of permissions using numeric modes, since many other commands can use them (install, mknod...).

Flip upper/lower for the special bit.
Source Link
Stephen Kitt
  • 482.9k
  • 60
  • 1.2k
  • 1.4k

In ls -l output, suid, sgid and "sticky" appear in place of the x entry: suid is s or S instead of the user's x, sgid is s or S instead of the group's x, and "sticky" is t or T instead of others' x. An upperA lower-case letter indicates that both the special bit and the executable bit are set; a loweran upper-case letter indicates that only the special bit is set.

In ls -l output, suid, sgid and "sticky" appear in place of the x entry: suid is s or S instead of the user's x, sgid is s or S instead of the group's x, and "sticky" is t or T instead of others' x. An upper-case letter indicates that both the special bit and the executable bit are set; a lower-case letter indicates that only the special bit is set.

In ls -l output, suid, sgid and "sticky" appear in place of the x entry: suid is s or S instead of the user's x, sgid is s or S instead of the group's x, and "sticky" is t or T instead of others' x. A lower-case letter indicates that both the special bit and the executable bit are set; an upper-case letter indicates that only the special bit is set.

add doors
Source Link
Stéphane Chazelas
  • 586.3k
  • 96
  • 1.1k
  • 1.7k
  • -: "regular" file, created with any program which can write a file
  • b: block special file, typically disk or partition devices, can be created with mknod
  • c: character special file, can also be created with mknod (see /dev for examples)
  • d: directory, can be created with mkdir
  • l: symbolic link, can be created with ln -s
  • p: named pipe, can be created with mkfifo
  • s: socket, can be created with nc -U
  • D: door, created by some server processes on Solaris/openindiana.
  • -: "regular" file, created with any program which can write a file
  • b: block special file, typically disk or partition devices, can be created with mknod
  • c: character special file, can also be created with mknod (see /dev for examples)
  • d: directory, can be created with mkdir
  • l: symbolic link, can be created with ln -s
  • p: named pipe, can be created with mkfifo
  • s: socket, can be created with nc -U
  • -: "regular" file, created with any program which can write a file
  • b: block special file, typically disk or partition devices, can be created with mknod
  • c: character special file, can also be created with mknod (see /dev for examples)
  • d: directory, can be created with mkdir
  • l: symbolic link, can be created with ln -s
  • p: named pipe, can be created with mkfifo
  • s: socket, can be created with nc -U
  • D: door, created by some server processes on Solaris/openindiana.
Bounty Awarded with 100 reputation awarded by Peter
Mention that special bits handling is system-dependent.
Source Link
Stephen Kitt
  • 482.9k
  • 60
  • 1.2k
  • 1.4k
Loading
Correct the description of special bits' handling with numeric modes.
Source Link
Stephen Kitt
  • 482.9k
  • 60
  • 1.2k
  • 1.4k
Loading
Add file types, split into sections, explain the various forms of chmod.
Source Link
Stephen Kitt
  • 482.9k
  • 60
  • 1.2k
  • 1.4k
Loading
Explain sgid/suid etc. in ls -l output.
Source Link
Stephen Kitt
  • 482.9k
  • 60
  • 1.2k
  • 1.4k
Loading
Source Link
Stephen Kitt
  • 482.9k
  • 60
  • 1.2k
  • 1.4k
Loading