歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux教程 >> Linux中的粘滯位

Linux中的粘滯位

日期:2017/2/28 16:06:35   编辑:Linux教程

Sticky 位是一個訪問權限標志位,可以用來標示文件和路徑。

歷史:

粘滯位是在1974年Unix的第5版中引入的,用來設置可執行文件。當可執行文件設置粘滯位之後,它能夠指示操作系統在程序退出後,保留程序的代碼段到swap空間。當程序再次執行時,內核只需將程序從swap搬到內存即可,這能夠加速程序的執行。所以,頻繁使用的程序比如編輯器能夠更快的打開。目前,這種應用只是適用於HP-UX, NetBSD和UnixWare,Solaris在2005年放棄了這種應用,linux沒有版本支持過這種行為。

當今使用:

當今,粘滯位最常用是應用於路徑。當路徑被設置粘滯位後,路徑下的文件只有文件的owner,路徑的owner, 或者root 才能夠重命名、刪除文件。如果沒有粘滯位,任何用戶,不管是不是owner, 只要有路徑的寫/執行權限就可以重命名、刪除文件。典型的應用就是/tmp路徑,粘滯位可以阻止一般用戶刪除/重命名其他用戶的文件。這種特性首次引入是在1986年4.3BSD, 今天在現在的Unix系統中都可以找到這個特性。另外,Solaris定義了獨有的行為:當粘滯位設置到非執行文件時,當訪問這種文件時,內核將不會緩存。這常用於設置swap文件,用來阻止訪問這些文件時沖刷掉系統緩存中更重要的數據。

Sticky bit在不同系統中的異同

HP-UX:當程序的最後一個用戶退出時,阻止系統丟棄程序段swap-space image. 當下一個用戶執行這個程序時,系統只需要swap in,而不需要重從磁盤新讀入文件到內存,節約程序啟動時間。

[...] 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: 當粘滯位設置到文件時,內核將會忽略。當設置路徑時,路徑中的文件只能夠被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 設置:

chmod 命令,可以用八進制模式1000或者它的符號t.

比如:添加粘滯位到路徑/usr/local/tmp,

1. chmod +t /usr/local/tmp

2. chmod 1777 /usr/local/tmp

在Unix 文件系統 符號中,sticky bit t 是在最後一位。比如:在Solaris 8, /tmp路徑默認有粘滯位,如下:

$ ls -ld /tmp
 drwxrwxrwt   4 root     sys          485 Nov 10 06:01 /tmp
如果粘滯位設置的路徑或者文件沒有可執行(x)位,它的符號用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
Copyright © Linux教程網 All Rights Reserved