您将主要在UNIX / Linux中的目录上设置粘性位。
如果将粘性位设置为目录,则其他用户无法删除或重命名该目录中的文件(或子目录)。
在目录上设置粘滞位后,只有所有者和root用户才能删除/重命名该目录中的文件或目录。
1. Set the 黏bit 上 Directory
下面的示例在目录上启用粘性位。
使用chmod命令设置粘性位。如果您使用的是 chmod中的八进制数,请先指定1,然后再指定其他编号的特权,如下所示。下面的示例向用户,组和其他用户授予rwx权限(并将粘性位添加到目录中)。
$ chmod 1777 dir
或者,您可以使用以下方式仅将粘性位分配给现有目录(不涉及任何其他用户,组和其他特权) chmod命令 如下所示。
$ chmod +t dir
将粘性位分配给目录后,您可以’将(t)视为许可中的最后一个字符。在此示例中,它是drwxrwxrwt。
$ ls -ld /home/bala/dir drwxrwxrwt 2 bala bala 4096 2011-01-28 14:09 /home/bala/dir $ ls -l dir total 8 -rwxrwxrwx 1 bala bala 20 2011-01-28 14:12 bala.txt -rwxrwxrwx 1 guest guest 41 2011-01-28 14:13 guest.txt
在上面的示例中,由于dir对所有人都有rwx权限,因此所有其他用户都可以在此目录下创建其文件或目录。但是,即使dir下的子目录或文件具有所有人的rwx权限,也只有这些所有者或目录的所有者才能删除或重命名那些文件和目录。其他用户由于粘性位无法删除或重命名它。
在上面的示例中,bala.txt对用户,组和其他用户具有rwx。但是,当来宾用户尝试删除文件bala.txt时,他’ll see the “禁止操作”消息如下所示。
$ su guest Password: $ cd /home/bala/dir1 $ rm bala.txt rm: cannot remove `bala.txt': Operation not permitted
请注意,/ tmp默认情况下启用了粘性位。您可能直到现在都没有注意到。现在您知道了为什么/ tmp目录应该启用了粘性位。
$ ls -ld /tmp drwxrwxrwt 3 root root 4096 Jan 31 08:29 /tmp
要从目录中删除粘性位,请执行以下操作。
$ chmod -t dir
2. Set the 黏bit 上 File
在文件上设置粘性位几乎没有用,并且没有’什么都不做。在某些较旧的* nix风格上,启用粘滞位的可执行文件将在第一次执行后加载到交换存储器,这会加快所有后续执行的速度。这不再是真的。
从 维基百科:
Currently, this behavior is 上 ly operative in HP-UX, NetBSD, and UnixWare. Solaris appears to have abandoned this in 2005.[citation needed] The 4.4-Lite release of BSD retained the old 黏bit behavior but it has been subsequently dropped from OpenBSD (as of release 3.7) and FreeBSD (as of release 2.2.1); it remains in NetBSD. No version of Linux has ever supported the traditional behavior.
如果您喜欢这篇文章,您可能还会喜欢..
![]() |
![]() |
![]() |
![]() |
谢谢,很棒的文章。我知道那很可怕,但对刷新记忆很有帮助ðŸ™,