Linux权限第4、5位含义及PHP 5位权限参数咨询
Hey there! Since you already have a solid grasp of the basic u/g/o permissions and r/w/x values, let's break down your remaining questions clearly:
First: Demystifying PHP's 02770 Permission Value
That leading 0 in 02770 isn't a permission bit at all—it's just PHP's way of saying "this is an octal number". Linux permissions use octal (base-8) because each digit neatly maps to a set of r/w/x or special permissions. If you left out the 0, PHP would treat 2770 as a decimal number, which would result in totally wrong permissions (trust me, you don't want that).
The actual permission bits are the four digits after the 0: 2770. Let's split them up:
- First digit (2): Special permission flag (this covers SUID, SGID, and Sticky Bit—we'll dive into these next)
- Second digit (7): Owner (u) permissions → Full access (
rwx= 4+2+1) - Third digit (7): Group (g) permissions → Full access (
rwx) - Fourth digit (0): Others (o) permissions → No access at all (
---)
So your example mkdir($directory, 02770); creates a directory where:
- SGID is enabled (that
2), - The owner can read/write/execute,
- The group gets full access too,
- Anyone outside the group gets nothing.
Next: SUID, SGID, and Sticky Bit - The "Special" Permissions
These are the extra permission bits that solve problems basic r/w/x can't handle. Each maps to a value that you add to that leading octal digit:
1. SUID (Set User ID) - Value: 4
- What it does: When you run an executable with SUID set, you temporarily take on the user ID of the file's owner. For example, regular users can't edit
/etc/passwddirectly, but/usr/bin/passwdhas SUID set (permissions-rwsr-xr-x—thatsreplaces the owner'sx). This lets you run it as root to change your password safely. - Octal example:
4755= SUID + ownerrwx, grouprx, othersrx.
2. SGID (Set Group ID) - Value: 2
- Two big use cases:
- For executables: Similar to SUID, but you run the file with the group ID of the file's group instead of your own primary group.
- For directories: Any new file or folder you create inside this directory inherits the directory's group ownership (not your personal group). This is perfect for team shared folders—everyone's work stays in the project group, no manual
chgrpneeded.
- Octal example: Your
2770is exactly this—SGID + full owner/group access, no others. - Permission string example: A shared directory might show
drwxr-sr-x—thatsin the group'sxposition is SGID.
3. Sticky Bit - Value: 1
- What it does: Almost exclusively used on directories. When enabled, only the owner of a file (or root) can delete/rename that file in the directory—even if other users have write access to the directory itself.
- Perfect example: The
/tmpdirectory has permissionsdrwxrwxrwt—thattin the others'xposition is the Sticky Bit. It stops random users from deleting your temporary files. - Octal example:
1777= Sticky Bit + full access for everyone, but only owners can delete their own stuff.
Finally: The "4th and 5th Bits" You Asked About
If you're referring to the human-readable permission string (like -rwsr-xr-x), let's count from left to right:
- File type (e.g.,
dfor directory,-for regular file) - Owner read (
r) - Owner write (
w) - Owner execute (or
sif SUID is set) → This is the 4th character, tied to the SUID special permission - Group read (
r) → This is the 5th character, part of the basic group permissions
If you're talking about octal digit positions, the 4th octal digit is the leading special permission digit we covered earlier. The 5th digit in PHP's 02770 is just the octal prefix (0), not an actual permission bit.
内容的提问来源于stack exchange,提问作者ACs

