Sunday, December 14, 2008

chmod和sticky位

上次那了关于set-user-ID, 这一次一起说linux下两个特殊的位:
1. set-user-ID
2. sticky bit
set-user-ID的作用这就不说了(上次写了), 说一下sticky的作用:

Sticky Bit-The Sticky Bit is only valid on directories. When the Sticky Bit is set on a directory, users are only allowed to delete files they own. Without this setting, users are allowed to delete files owned by other users as long as they have the write (w) permission on the directory. Special permissions are stored as part of the regular mode of the file, but there is no room for three more permission bits for these permissions. Because of this, special permissions are actually included within the execute (x) bit of user, group and other permissions. Figure 3.13 and Figure 3.14 demonstrate how these permissions are displayed when the corresponding execute bit is set and not set, respectively.

* 意思就是有sticky位的文件只能被owner删除和rename.
* linux下不起作用.
* -_-! 写本文前, 不知道linux下是不行的. 无法往下写了! 下面就说chmod的使用吧

下面是linux下, 文件的permission的基本信息:









chmod有两种方式:
1. 使用字符串方式指定文件的权限
2. 使用数字方式指定文件的权限.

先提出两个常看权限问题:
1. 把文件的权限设置成owner可读写,group只读,other只读
1. 把文件的group权限修改为可读写

字符串的方式:
常用来修改文件其中的一组权限.(文件的权限分为三组)
数字的方式:
常用来一次设置文件的三组权限

使用字符串的方式去设置上面的第一个问题:
# chmod u=rw,g=r,o=r FileName
上面的方式中, 一次修改了文件的三组权限.
使用数据的方式去设置:
# chmod 0644 FileName

看起来, 都可以完成. 其实, 字符串的方式比较麻烦, 数字的方式比较精炼.

重点在第二个问题!对于第二个问题如下方法:
# chmod g+w FileName

这只能使用字符串的方式, 如可要使用数字的方式, 需要有逻辑介入

所为, 字符串的方式重点在于修改文件的一组权限.而字符串的方式重点在于设定文件的三组权限

字符串的方式还有一种用法:
1. -X(大写) 如果文件或目录指定的一组权限有x位, 就去掉, 没有就保留原状
2. +X(大写) 如果目录指定的一组权限没有x位,就增加, 有就保留原状, 对正规文件不起作用

下面是man手册中chmod使用八进制方式表达:
A numeric mode is from one to four octal digits (0-7), derived by
adding up the bits with values 4, 2, and 1. Omitted digits are assumed
to be leading zeros. The first digit selects the set user ID (4) and
set group ID (2) and restricted deletion or sticky (1) attributes. The
second digit selects permissions for the user who owns the file: read
(4), write (2), and execute (1); the third selects permissions for
other users in the file's group, with the same values; and the fourth
for other users not in the file's group, with the same values.

数字的方式没有其它的特点了吗? 也不是的, 至少sticky位只能使用数字的方式. 如下:
set-user-ID和sticky位的情况比较特殊:
* set-user-ID只有user和group两种情况, 没有other的情况
* 对于sticky位只有一种情况:有与没有.
那么, 就算这两种情况都出现, 刚好为 4+2+1=7

如: chmod 7755 file

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.