Blog icon indicating copy to clipboard operation
Blog copied to clipboard

深入理解chmod xxxx 四位数字

Open codcodog opened this issue 7 years ago • 0 comments

深入理解chmod xxxx 四位数字

通常我们如果修改一个文件的权限,使用chmod命令。一般的用法是:

$ chmod 0755 FILENAME

在这里必须提及到一个知识点:
也就是文件的权限,分别为r,w,x
对用的数字分别为:
r---->4
w---->2
x---->1

如果一个文件需要全部的权限,也就是rwx,则数字为4+2+1,也就是7.

那么0755分别代表什么意思呢?
在这里,先忽略0(后面解释).

剩下的三位分别对应:
文件属主(ower,u), 文件组(group,g), 其他(others,o).

因此chmod 755 FILENAME代表的意思就是,属主具备读写执行权限,同一组的用户具备读执行权限,其他用户具备读执行权限.

回到之前的0数字这里,四位数字中的第一位数字具备什么含义呢?

在这里说明一下chmod权限选项:
4000 Sets user ID on execution
2000 Sets group ID on execution
1000 Sets the link permission to directoires or sets the save-text attribute for files.
(详细参考《mstering unix shell scripting》- Part1 - File permissons (54 of 1035))

所以当chmod 4755 my_program的时候,无论谁执行了这个文件,都相当于属主执行。

codcodog avatar May 23 '17 15:05 codcodog