歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> 關於Linux >> Linux中的文件基本權限

Linux中的文件基本權限

日期:2017/3/1 16:21:07   编辑:關於Linux
Linux中的文件基本權限 Linux系統中的每一個文件都與多種權限類型相關聯。在這些權限中,我們主要和三類權限打交道:用戶(user)、用戶組(group)和其他用戶(others)。用戶是文件的所有者;用戶組是指和文件所有者在同一組的其他多個用戶的集合;其他用戶是除用戶或用戶組之外的任何用戶。 ls -l命令可以列出文件的權限,如: -rw-rw-r-- 1 lfqy lfqy 529 6月 11 20:21 file-authority.txt -rw-rw-r-- 1 lfqy lfqy 0 6月 11 19:02 helloworld drwxrwxr-x 2 lfqy lfqy 4096 6月 11 20:21 try 可以看出,每一行輸出代表一個文件。每行輸出的前10個字符代表文件的權限信息:第一個字符代表文件的類型(-表示普通文件,d表示目錄,c表示字符設備,b表示塊設備,l表示符號鏈接,s表示套接字,p表示管道),剩下的部分可以劃分成三組(第一組的三個字符對應用戶權限,第二組的三個字符對應用戶組權限,第三組的三個字符對應其他用戶權限。這9個字符中的每一個字符指明是否設置了某種權限,如果設置了權限,對應位置上就會出現一個字符,否則就一個'-'表明沒有設置對應的權限)。其中r代表讀權限,w代表寫權限,x代表執行權限,比如第一行中的file-authority.txt文件屬於用戶lfqy,該用戶對其擁有讀寫權限,而沒有執行權限,和lfqy在同一組的其他用戶也擁有對該文件的讀寫權限,而其他用戶對其只有讀權限。 1、文件的權限 1.1 文件的基本權限 rwx分別對應文件的讀權限、寫權限和可執行權限,然而,對於目錄來說,這三種權限有不同的含義。目錄的讀權限允許讀取目錄中文件和子目錄的列表,目錄的寫權限允許在目錄中創建或刪除文件或目錄,目錄的可執行權限指明是否可以訪問目錄中的文件和子目錄。 1.2 setuid、setgid和sticky bit 實際上,除了最基本的讀、寫和執行權限之外,Linux中還有setuid、setgid和sticky bit等三種權限。下面分別解釋這三種權限。 關於setuid和setgid維基百科上的解釋如下: setuid and setgid (short for "set user ID upon execution" and "set group ID upon execution", respectively) are Unix access rights flags that allow users to run an executable with the permissions of the executable's owner or group respectively and to change behaviour in directories. They are often used to allow users on a computer system to run programs with temporarily elevated privileges in order to perform a specific task. The setuid and setgid flags, when set on a directory, have an entirely different meaning. Setting the setgid permission on a directory (chmod g+s) causes new files and subdirectories created within it to inherit its group ID, rather than the primary group ID of the user who created the file (the owner ID is never affected, only the group ID). Newly created subdirectories inherit the setgid bit. Thus, this enables a shared workspace for a group without the inconvenience of requiring group members to explicitly change their current group before creating new files or directories. Note that setting the setgid permission on a directory only affects the group ID of new files and subdirectories created after the setgid bit is set, and is not applied to existing entities. Setting the setgid bit on existing subdirectories must be done manually, with a command such as the following: [root@foo]# find /path/to/directory -type d -exec chmod g+s {} \; The setuid permission set on a directory is ignored on UNIX and Linux systems. FreeBSD can be configured to interpret it analogously to setgid, namely, to force all files and sub-directories to be owned by the top directory owner. 1.2.1 setuid權限 setuid可以設置使文件在執行階段具有文件所有者的權限。setuid屬性出往往用用戶權限的第三個字符表示:如果用戶權限的第三個字符是s,則表示該文件的屬主對該文件有可執行權限的同時,該文件還有setuid權限;如果用戶權限的第三個字符是S,則表示該文件的屬主對該文件沒有可執行權限,但是該文件有setuid權限(實際上,從下文也可一看出,這種情況,沒有可執行權限只有setuid權限無任何意義)。setuid權限允許用戶以其文件擁有者的權限來執行可執行文件,即使這個可執行文件是由其他用戶運行的。從下面的例子中可以看出setuid權限的意思。 Linux中的密碼通常是保存在"/etc/paswd"和"/etc/shadow"文件中,這兩個文件對系統安全至關重要,因此只有Root用戶才能對其執行讀寫操作。以管理員的身份登陸系統,在Linux提示符下執行"ls /etc/passwd /etc/shadow"命令,在返回信息中可以看到普通用戶對上述這兩個文件並沒有寫權限,因此從文件屬性的角度看,普通用戶在更改自身密碼時,是無法將密碼信息寫入到上述文件中的,哪麼用戶是怎樣成功的更改密碼的呢?實際上,問題的關鍵不在於密碼文件本身,而在於密碼更改命令"passwd"。在提示符下執行命令"ls /usr/bin/passwd",在返回信息中的文件所有者執行權限位上顯示"s"字樣,表示"passwd"命令具setuid權限,其所有者為root,這樣普通用戶在執行"passwd"命令時,實際上以有效用戶root的身份來執行的,並具有了相應的權限(包括讀寫passwd和shadow文件的權限),從而將新的密碼寫入到"/etc/passwd"和"/etc/shadow"文件中,當命令執行完畢,該用戶的身份立即消失。這樣,通過setuid權限,普通用戶在執行passwd程序時,也能獲得passwd程序屬主(root)一樣的權限(也可以對文件passwd和shadow進行讀寫)。這樣,普通用戶也可以通過passwd工具來修改自身密碼。 1.2.2 setgid權限 setgid權限的含義和setuid類似,它允許用戶以其擁有者所在的組的權限來執行可執行文件。setgid屬性往往用用戶組權限的第三個字符表示:如果用戶權限的第三個字符是s,則表示該文件的屬主對該文件有可執行權限的同時,該文件還有setgid權限;如果用戶組權限的第三個字符是S,則表示該文件的屬主對該文件沒有可執行權限,但是該文件有setgid權限(實際上,從下文也可一看出,這種情況,沒有可執行權限只有setuid權限無任何意義)。setgid權限允許用戶以其文件擁有者所在組的權限來執行可執行文件,即使這個可執行文件是由其他用戶運行的。 1.2.3 sticky bit 關於sticky,維基百科上的解釋如下: In computing, the sticky bit is a user ownership access-right flag that can be assigned to files and directories on Unix systems. The most common use of the sticky bit today is on directories. When the sticky bit is set, only the item's owner, the directory's owner, or the superuser can rename or delete files. Without the sticky bit set, any user with write and execute permissions for the directory can rename or delete contained files, regardless of owner. Typically this is set on the /tmp directory to prevent ordinary users from deleting or moving other users' files. This feature was introduced in 4.3BSD in 1986 and today it is found in most modern Unix systems. sticky-bit之後,盡管其他用戶有寫權限, 也必須由屬主執行刪除、移動等操作。對一個目錄設置了sticky-bit之後,存放在該目錄的文件僅准許其屬主執行刪除、 移動等操作。 sticky bit出現在其他用戶權限中的執行權限(x)位置,使用t或T表示。t表示既有可執行權限,又設置了sticky bit,T表示只設置了sticky bit而沒有設置可執行權限。 2、管理文件權限 2.1 設置基本權限 2.1.1 用助記符的方式設置文件基本權限 可以采用"chmod u=rwx g=rw o=r filename"來設置文件的權限。其中u代表用戶的權限,g代表用戶所帶組的權限,o代表其它用戶的權限。如果要添加權限可以"chmod a+x filename"(給所有的用戶添加可執行權限),"chmod o+x filename"給其他用戶增加可執行權限。如果要刪除相應的權限,可以"chmod a-x filename"(刪除所有用戶的可執行權限)。 2.1.2 用八進制數的方式設置文件的基本權限 也可以采用八進制數的形式。讀、寫和執行權限都有與之對應的唯一的8進制數:r(4),w(2),x(1)。我們可以將權限序列的八進制值相加來獲得所需的權限組合:rwx(4+2+1=7),rw-(4+2=6),r-x(4+1=5)等。因此,用8進制設置權限的命令為"chmod 765 filename"。 2.2 設置setuid,setgid和sticky bit 2.2.1 用助記符的方式 chmod u +s temp -- 為temp文件加上setuid標志 chmod g +s tempdir -- 為tempdir目錄加上setgid標志 chmod o +t temp -- 為temp文件加上sticky標志 2.2.2 采用八進制方式 對一般文件通過三組八進制數字來置標志, 如 666, 777, 644等. 如果設置這些特殊標志, 則在這組數字之外外加一組八進制數字. 如 4666, 2777等. 這一組八進制數字三位的意義如下: abc a - setuid位, 如果該位為1, 則表示設置setuid 4xxx b - setgid位, 如果該位為1, 則表示設置setgid 2xxx c - sticky位, 如果該位為1, 則表示設置sticky 1xxx 設置完這些標志後, 可以用 ls -l 來查看. 如果有這些標志, 則會在原來的執行標志位置上顯示. 如 rwsrw-r-- 表示有setuid標志 rwxrwsrw- 表示有setgid標志 rwxrw-rwt 表示有sticky標志 對於setuid,setgid和sticky bit,如果表示這寫權限的位上本來有x, 則這些特殊標志顯示為小寫字母 (s, s, t);若無執行權限則顯示為大寫字母(S, S, T)。 3、其它 文件的權限是為了使用的安全性和方便性而設置的,了解這寫權限的含義能是我們更加方便的使用Linux,防制由於文件權限管理不善而帶來的安全問題。實際上,在設置文件的權限時,應該慎重考慮,尤其是在使用setuid、setgid和sticky bit等權限的時候。
Copyright © Linux教程網 All Rights Reserved