Linux 文件基本属性

ls -l 每一列的含义、rwx 权限、chmod/chown、umask 与特殊位。

Beginner
Key Points权限三组九位:属主、属组、其他人;目录的 x 表示「能进入」,没有 x 就连读都不行。

读懂 ls -l

Text
-rwxr-xr--  1 alice dev  4096 Oct 31 10:00 deploy.sh
│└┬┘└┬┘└┬┘    │     │      │
│ │  │  │     │     │      └── 大小
│ │  │  │     │     └───────── 属组
│ │  │  │     └─────────────── 属主
│ │  │  └───────────────────── 其他人:r--
│ │  └──────────────────────── 属组:r-x
│ └─────────────────────────── 属主:rwx
└───────────────────────────── 类型:- 普通文件,d 目录,l 链接

数字与符号

ValueSymbolFileDirectory
4rRead contentList names
2wModify contentCreate/delete entries
1xExecuteEnter (cd) and traverse
Bash
chmod 755 deploy.sh          # rwxr-xr-x
chmod u+x,g-w,o= deploy.sh   # 符号写法
chmod -R 750 /opt/app
chown alice:dev deploy.sh
chown -R www-data:www-data /var/www

umask

New files start at 666 and directories at 777, minus the umask. umask 022 gives 644/755; umask 077 gives 600/700 (private).

Bash
umask                 # 查看
umask 027             # 临时改(当前 shell)

特殊位

BitNumericEffectCommon use
setuid4000Run as file owner/usr/bin/passwd
setgid2000Run as group / inherit dir groupShared team dirs
sticky1000Only owner can delete/tmp
chmod -R 777 is not a fix. It lets anyone modify your code and config, and makes incidents untraceable. Grant the minimum.

看权限

Bash
ls -l deploy.sh
stat deploy.sh
namei -l /opt/app/bin/run.sh     # 逐级检查路径上每一层目录的权限

Friends & Partners