linux中权限765啥意思,Linux中的文件权限
Linux系統(tǒng)中的每一個文件都與多種權(quán)限類型相關(guān)聯(lián)。在這些權(quán)限中,我們主要和三類權(quán)限打交道:用戶(user)、用戶組(group)和其他用戶(others)。用戶是文件的所有者;用戶組是指和文件所有者在同一組的其他多個用戶的集合;其他用戶是除用戶或用戶組之外的任何用戶。
ls -l命令可以列出文件的權(quán)限,如:
-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個字符代表文件的權(quán)限信息:第一個字符代表文件的類型(-表示普通文件,d表示目錄,c表示字符設(shè)備,b表示塊設(shè)備,l表示符號鏈接,s表示套接字,p表示管道),剩下的部分可以劃分成三組(第一組的三個字符對應(yīng)用戶權(quán)限,第二組的三個字符對應(yīng)用戶組權(quán)限,第三組的三個字符對應(yīng)其他用戶權(quán)限。這9個字符中的每一個字符指明是否設(shè)置了某種權(quán)限,如果設(shè)置了權(quán)限,對應(yīng)位置上就會出現(xiàn)一個字符,否則就一個'-'表明沒有設(shè)置對應(yīng)的權(quán)限)。其中r代表讀權(quán)限,w代表寫權(quán)限,x代表執(zhí)行權(quán)限,比如第一行中的file-authority.txt文件屬于用戶lfqy,該用戶對其擁有讀寫權(quán)限,而沒有執(zhí)行權(quán)限,和lfqy在同一組的其他用戶也擁有對該文件的讀寫權(quán)限,而其他用戶對其只有讀權(quán)限。
1、文件的權(quán)限
1.1 文件的基本權(quán)限
rwx分別對應(yīng)文件的讀權(quán)限、寫權(quán)限和可執(zhí)行權(quán)限,然而,對于目錄來說,這三種權(quán)限有不同的含義。目錄的讀權(quán)限允許讀取目錄中文件和子目錄的列表,目錄的寫權(quán)限允許在目錄中創(chuàng)建或刪除文件或目錄,目錄的可執(zhí)行權(quán)限指明是否可以訪問目錄中的文件和子目錄。
1.2 setuid、setgid和sticky bit
實際上,除了最基本的讀、寫和執(zhí)行權(quán)限之外,Linux中還有setuid、setgid和sticky bit等三種權(quán)限。下面分別解釋這三種權(quán)限。
關(guān)于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權(quán)限
setuid可以設(shè)置使文件在執(zhí)行階段具有文件所有者的權(quán)限。setuid屬性出往往用用戶權(quán)限的第三個字符表示:如果用戶權(quán)限的第三個字符是s,則表示該文件的屬主對該文件有可執(zhí)行權(quán)限的同時,該文件還有setuid權(quán)限;如果用戶權(quán)限的第三個字符是S,則表示該文件的屬主對該文件沒有可執(zhí)行權(quán)限,但是該文件有setuid權(quán)限(實際上,從下文也可一看出,這種情況,沒有可執(zhí)行權(quán)限只有setuid權(quán)限無任何意義)。setuid權(quán)限允許用戶以其文件擁有者的權(quán)限來執(zhí)行可執(zhí)行文件,即使這個可執(zhí)行文件是由其他用戶運行的。從下面的例子中可以看出setuid權(quán)限的意思。
Linux中的密碼通常是保存在"/etc/paswd"和"/etc/shadow"文件中,這兩個文件對系統(tǒng)安全至關(guān)重要,因此只有Root用戶才能對其執(zhí)行讀寫操作。以管理員的身份登陸系統(tǒng),在Linux提示符下執(zhí)行"ls /etc/passwd /etc/shadow"命令,在返回信息中可以看到普通用戶對上述這兩個文件并沒有寫權(quán)限,因此從文件屬性的角度看,普通用戶在更改自身密碼時,是無法將密碼信息寫入到上述文件中的,哪么用戶是怎樣成功的更改密碼的呢?實際上,問題的關(guān)鍵不在于密碼文件本身,而在于密碼更改命令"passwd"。在提示符下執(zhí)行命令"ls /usr/bin/passwd",在返回信息中的文件所有者執(zhí)行權(quán)限位上顯示"s"字樣,表示"passwd"命令具setuid權(quán)限,其所有者為root,這樣普通用戶在執(zhí)行"passwd"命令時,實際上以有效用戶root的身份來執(zhí)行的,并具有了相應(yīng)的權(quán)限(包括讀寫passwd和shadow文件的權(quán)限),從而將新的密碼寫入到"/etc/passwd"和"/etc/shadow"文件中,當命令執(zhí)行完畢,該用戶的身份立即消失。這樣,通過setuid權(quán)限,普通用戶在執(zhí)行passwd程序時,也能獲得passwd程序?qū)僦?root)一樣的權(quán)限(也可以對文件passwd和shadow進行讀寫)。這樣,普通用戶也可以通過passwd工具來修改自身密碼。
1.2.2 setgid權(quán)限
setgid權(quán)限的含義和setuid類似,它允許用戶以其擁有者所在的組的權(quán)限來執(zhí)行可執(zhí)行文件。setgid屬性往往用用戶組權(quán)限的第三個字符表示:如果用戶權(quán)限的第三個字符是s,則表示該文件的屬主對該文件有可執(zhí)行權(quán)限的同時,該文件還有setgid權(quán)限;如果用戶組權(quán)限的第三個字符是S,則表示該文件的屬主對該文件沒有可執(zhí)行權(quán)限,但是該文件有setgid權(quán)限(實際上,從下文也可一看出,這種情況,沒有可執(zhí)行權(quán)限只有setuid權(quán)限無任何意義)。setgid權(quán)限允許用戶以其文件擁有者所在組的權(quán)限來執(zhí)行可執(zhí)行文件,即使這個可執(zhí)行文件是由其他用戶運行的。
1.2.3 sticky bit
關(guān)于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之后,盡管其他用戶有寫權(quán)限,也必須由屬主執(zhí)行刪除、移動等操作。對一個目錄設(shè)置了sticky-bit之后,存放在該目錄的文件僅準許其屬主執(zhí)行刪除、移動等操作。
sticky bit出現(xiàn)在其他用戶權(quán)限中的執(zhí)行權(quán)限(x)位置,使用t或T表示。t表示既有可執(zhí)行權(quán)限,又設(shè)置了sticky bit,T表示只設(shè)置了sticky bit而沒有設(shè)置可執(zhí)行權(quán)限。
2、管理文件權(quán)限
2.1 設(shè)置基本權(quán)限
2.1.1 用助記符的方式設(shè)置文件基本權(quán)限
可以采用"chmod u=rwx g=rw o=r filename"來設(shè)置文件的權(quán)限。其中u代表用戶的權(quán)限,g代表用戶所帶組的權(quán)限,o代表其它用戶的權(quán)限。如果要添加權(quán)限可以"chmod a+x filename"(給所有的用戶添加可執(zhí)行權(quán)限),"chmod o+x filename"給其他用戶增加可執(zhí)行權(quán)限。如果要刪除相應(yīng)的權(quán)限,可以"chmod a-x filename"(刪除所有用戶的可執(zhí)行權(quán)限)。
2.1.2 用八進制數(shù)的方式設(shè)置文件的基本權(quán)限
也可以采用八進制數(shù)的形式。讀、寫和執(zhí)行權(quán)限都有與之對應(yīng)的唯一的8進制數(shù):r(4),w(2),x(1)。我們可以將權(quán)限序列的八進制值相加來獲得所需的權(quán)限組合:rwx(4+2+1=7),rw-(4+2=6),r-x(4+1=5)等。因此,用8進制設(shè)置權(quán)限的命令為"chmod 765 filename"。
2.2 設(shè)置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 采用八進制方式
對一般文件通過三組八進制數(shù)字來置標志, 如 666, 777, 644等. 如果設(shè)置這些特殊標志, 則在這組數(shù)字之外外加一組八進制數(shù)字. 如 4666, 2777等. 這一組八進制數(shù)字三位的意義如下:
abc
a - setuid位, 如果該位為1, 則表示設(shè)置setuid 4xxx
b - setgid位, 如果該位為1, 則表示設(shè)置setgid ?2xxx
c - sticky位, 如果該位為1, 則表示設(shè)置sticky ? 1xxx
設(shè)置完這些標志后, 可以用 ls -l 來查看. 如果有這些標志, 則會在原來的執(zhí)行標志位置上顯示. 如
rwsrw-r-- 表示有setuid標志
rwxrwsrw- 表示有setgid標志
rwxrw-rwt 表示有sticky標志
對于setuid,setgid和sticky bit,如果表示這寫權(quán)限的位上本來有x, 則這些特殊標志顯示為小寫字母 (s, s, t);若無執(zhí)行權(quán)限則顯示為大寫字母(S, S, T)。
3、其它
文件的權(quán)限是為了使用的安全性和方便性而設(shè)置的,了解這寫權(quán)限的含義能是我們更加方便的使用Linux,防制由于文件權(quán)限管理不善而帶來的安全問題。實際上,在設(shè)置文件的權(quán)限時,應(yīng)該慎重考慮,尤其是在使用setuid、setgid和sticky bit等權(quán)限的時候。
來自 “ ITPUB博客 ” ,鏈接:http://blog.itpub.net/29366942/viewspace-1062231/,如需轉(zhuǎn)載,請注明出處,否則將追究法律責任。
總結(jié)
以上是生活随笔為你收集整理的linux中权限765啥意思,Linux中的文件权限的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 霞飞坦克装上了交错式负重轮
- 下一篇: 阿玛莱特AR50螺栓式反坦克