Linux中的粘滞位
Linux中的粘滯位
Sticky 位是一個(gè)訪問權(quán)限標(biāo)志位,可以用來標(biāo)示文件和路徑。
歷史:
粘滯位是在1974年Unix的第5版中引入的,用來設(shè)置可執(zhí)行文件。當(dāng)可執(zhí)行文件設(shè)置粘滯位之后,它能夠指示操作系統(tǒng)在程序退出后,保留程序的代碼段到swap空間。當(dāng)程序再次執(zhí)行時(shí),內(nèi)核只需將程序從swap搬到內(nèi)存即可,這能夠加速程序的執(zhí)行。所以,頻繁使用的程序比如編輯器能夠更快的打開。目前,這種應(yīng)用只是適用于HP-UX, NetBSD和UnixWare,Solaris在2005年放棄了這種應(yīng)用,linux沒有版本支持過這種行為。
當(dāng)今使用:
當(dāng)今,粘滯位最常用是應(yīng)用于路徑。當(dāng)路徑被設(shè)置粘滯位后,路徑下的文件只有文件的owner, 或者root 才能夠重命名、刪除文件。如果沒有粘滯位,任何用戶,不管是不是owner, 只要有路徑的寫/執(zhí)行權(quán)限就可以重命名、刪除文件。典型的應(yīng)用就是/tmp路徑,粘滯位可以阻止一般用戶刪除/重命名其他用戶的文件。這種特性首次引入是在1986年4.3BSD, 今天在現(xiàn)在的Unix系統(tǒng)中都可以找到這個(gè)特性。另外,Solaris定義了獨(dú)有的行為:當(dāng)粘滯位設(shè)置到非執(zhí)行文件時(shí),當(dāng)訪問這種文件時(shí),內(nèi)核將不會(huì)緩存。這常用于設(shè)置swap文件,用來阻止訪問這些文件時(shí)沖刷掉系統(tǒng)緩存中更重要的數(shù)據(jù)。
?
Sticky bit在不同系統(tǒng)中的異同
HP-UX:當(dāng)程序的最后一個(gè)用戶退出時(shí),阻止系統(tǒng)丟棄程序段swap-space image. 當(dāng)下一個(gè)用戶執(zhí)行這個(gè)程序時(shí),系統(tǒng)只需要swap in,而不需要重從磁盤新讀入文件到內(nèi)存,節(jié)約程序啟動(dòng)時(shí)間。
[...] prevents the system from abandoningthe swap-space image of the program-text portion of the file when its last userterminates. Then, when the next user of the file executes it, the text need notbe read from the file system but can simply be swapped in, thus saving time.
Linux: 當(dāng)粘滯位設(shè)置到文件時(shí),內(nèi)核將會(huì)忽略。當(dāng)設(shè)置路徑時(shí),路徑中的文件只能夠被root或者文件的owner重命名或者unlinked。
[...] the Linux kernel ignores the stickybit on files. [...] When the sticky bit is set on a directory, files in thatdirectory may only be unlinked or renamed by root or their owner.
舉例:
Sticky bit 設(shè)置:
chmod 命令,可以用八進(jìn)制模式1000或者它的符號(hào)t.
比如:添加粘滯位到路徑/usr/local/tmp,
1.??????chmod +t /usr/local/tmp
2.??????chmod 1777 /usr/local/tmp
在Unix 文件系統(tǒng) 符號(hào)中,sticky bit t 是在最后一位。比如:在Solaris 8, /tmp路徑默認(rèn)有粘滯位,如下:
$ ls -ld /tmp
drwxrwxrwt?? 4 root???? sys????????? 485 Nov 10 06:01 /tmp
如果粘滯位設(shè)置的路徑或者文件沒有可執(zhí)行(x)位,它的符號(hào)用T(大寫的t)
# ls -l test
-rw-r--r--?? 1 root???? other????????? 0 Nov 10 12:57 test
# chmod +t test; ls -l test
-rw-r--r-T?? 1 root???? other????????? 0 Nov 10 12:57 tes
From http://en.wikipedia.org/wiki/Sticky_bit
總結(jié)
以上是生活随笔為你收集整理的Linux中的粘滞位的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 卵巢功能低怎么调理
- 下一篇: grep 在HP-UX下的递归查找