You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Linux权限第4、5位含义及PHP 5位权限参数咨询

Understanding Linux Special Permissions & 5-Digit Permissions in PHP

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/passwd directly, but /usr/bin/passwd has SUID set (permissions -rwsr-xr-x—that s replaces the owner's x). This lets you run it as root to change your password safely.
  • Octal example: 4755 = SUID + owner rwx, group rx, others rx.

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 chgrp needed.
  • Octal example: Your 2770 is exactly this—SGID + full owner/group access, no others.
  • Permission string example: A shared directory might show drwxr-sr-x—that s in the group's x position 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 /tmp directory has permissions drwxrwxrwt—that t in the others' x position 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:

  1. File type (e.g., d for directory, - for regular file)
  2. Owner read (r)
  3. Owner write (w)
  4. Owner execute (or s if SUID is set) → This is the 4th character, tied to the SUID special permission
  5. 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 07:02:38