Linux 文件基本屬性
Linux系統(tǒng)是一種典型的多用戶系統(tǒng)怖竭,不同的用戶處于不同的地位绝葡,擁有不同的權(quán)限带族。為了保護(hù)系統(tǒng)的安全性拗窃,Linux系統(tǒng)對(duì)不同的用戶訪問(wèn)同一文件(包括目錄文件)的權(quán)限做了不同的規(guī)定。
在Linux中我們可以使用 ll 或者 ls –l 命令來(lái)顯示一個(gè)文件的屬性以及文件所屬的用戶和組锌畸,如:
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="shell" cid="n4" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">[root@www /]# ls -l
total 64
dr-xr-xr-x 2 root root 4096 Dec 14 2012 bin
dr-xr-xr-x 4 root root 4096 Apr 19 2012 boot
……</pre>
實(shí)例中勇劣,bin文件的第一個(gè)屬性用"d"表示。"d"在Linux中代表該文件是一個(gè)目錄文件潭枣。
在Linux中第一個(gè)字符代表這個(gè)文件是目錄比默、文件或鏈接文件等等。
當(dāng)為[ d ]則是目錄
當(dāng)為[ - ]則是文件盆犁;
若是[ l ]則表示為鏈接文檔(link file)命咐;
若是[ b ]則表示為裝置文件里面的可供儲(chǔ)存的接口設(shè)備(可隨機(jī)存取裝置);
若是[ c ]則表示為裝置文件里面的串行端口設(shè)備谐岁,例如鍵盤(pán)醋奠、鼠標(biāo)(一次性讀取裝置)。
接下來(lái)的字符中伊佃,以三個(gè)為一組窜司,且均為『rwx』 的三個(gè)參數(shù)的組合。其中航揉,[ r ]代表可讀(read)塞祈、[ w ]代表可寫(xiě)(write)、[ x ]代表可執(zhí)行(execute)帅涂。 要注意的是议薪,這三個(gè)權(quán)限的位置不會(huì)改變,如果沒(méi)有權(quán)限媳友,就會(huì)出現(xiàn)減號(hào)[ - ]而已笙蒙。
每個(gè)文件的屬性由左邊第一部分的10個(gè)字符來(lái)確定(如下圖)。接下來(lái)的字符中庆锦,以三個(gè)為一組,且均為『rwx』 的三個(gè)參數(shù)的組合轧葛。其中搂抒,[ r ]代表可讀(read)、[ w ]代表可寫(xiě)(write)尿扯、[ x ]代表可執(zhí)行(execute)求晶。 要注意的是,這三個(gè)權(quán)限的位置不會(huì)改變衷笋,如果沒(méi)有權(quán)限芳杏,就會(huì)出現(xiàn)減號(hào)[ - ]而已。
每個(gè)文件的屬性由左邊第一部分的10個(gè)字符來(lái)確定(如下圖)。
從左至右用0-9這些數(shù)字來(lái)表示爵赵。
第0位確定文件類型吝秕,第1-3位確定屬主(該文件的所有者)擁有該文件的權(quán)限。
第4-6位確定屬組(所有者的同組用戶)擁有該文件的權(quán)限空幻,第7-9位確定其他用戶擁有該文件的權(quán)限烁峭。
其中,第1秕铛、4约郁、7位表示讀權(quán)限,如果用"r"字符表示但两,則有讀權(quán)限鬓梅,如果用"-"字符表示,則沒(méi)有讀權(quán)限谨湘;
第2绽快、5、8位表示寫(xiě)權(quán)限悲关,如果用"w"字符表示谎僻,則有寫(xiě)權(quán)限,如果用"-"字符表示沒(méi)有寫(xiě)權(quán)限寓辱;第3艘绍、6、9位表示可執(zhí)行權(quán)限秫筏,如果用"x"字符表示诱鞠,則有執(zhí)行權(quán)限,如果用"-"字符表示这敬,則沒(méi)有執(zhí)行權(quán)限航夺。
Linux文件屬主和屬組
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="shell" cid="n28" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">[root@www /]# ls -l
total 64
dr-xr-xr-x 2 root root 4096 Dec 14 2012 bin
dr-xr-xr-x 4 root root 4096 Apr 19 2012 boot
……</pre>
對(duì)于文件來(lái)說(shuō),它都有一個(gè)特定的所有者崔涂,也就是對(duì)該文件具有所有權(quán)的用戶阳掐。
同時(shí),在Linux系統(tǒng)中冷蚂,用戶是按組分類的缭保,一個(gè)用戶屬于一個(gè)或多個(gè)組。
文件所有者以外的用戶又可以分為文件所有者的同組用戶和其他用戶蝙茶。
因此艺骂,Linux系統(tǒng)按文件所有者、文件所有者同組用戶和其他用戶來(lái)規(guī)定了不同的文件訪問(wèn)權(quán)限隆夯。
在以上實(shí)例中钳恕,bin文件是一個(gè)目錄文件别伏,屬主和屬組都為root,屬主有可讀忧额、可寫(xiě)厘肮、可執(zhí)行的權(quán)限;與屬主同組的其他用戶有可讀和可執(zhí)行的權(quán)限宙址;其他用戶也有可讀和可執(zhí)行的權(quán)限轴脐。
更改文件屬性
1、chgrp:更改文件屬組
語(yǔ)法:
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="shell" cid="n37" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">chgrp [-R] 屬組名文件名</pre>
參數(shù)選項(xiàng)
- -R:遞歸更改文件屬組抡砂,就是在更改某個(gè)目錄文件的屬組時(shí)大咱,如果加上-R的參數(shù),那么該目錄下的所有文件的屬組都會(huì)更改注益。
2碴巾、chown:更改文件屬主,也可以同時(shí)更改文件屬組
語(yǔ)法:
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="shell" cid="n44" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">chown [–R] 屬主名 文件名
chown [-R] 屬主名:屬組名 文件名</pre>
進(jìn)入 /root 目錄(~)將install.log的擁有者改為bin這個(gè)賬號(hào):
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="" cid="n46" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">[root@www ~] cd ~
[root@www ~]# chown bin install.log
[root@www ~]# ls -l
-rw-r--r-- 1 bin users 68495 Jun 25 08:53 install.log</pre>
將install.log的擁有者與群組改回為root:
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="" cid="n48" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">[root@www ~]# chown root:root install.log
[root@www ~]# ls -l
-rw-r--r-- 1 root root 68495 Jun 25 08:53 install.log</pre>
3丑搔、chmod:更改文件9個(gè)屬性
Linux文件屬性有兩種設(shè)置方法厦瓢,一種是數(shù)字,一種是符號(hào)啤月。
Linux文件的基本權(quán)限就有九個(gè)煮仇,分別是owner/group/others三種身份各有自己的read/write/execute權(quán)限。
先復(fù)習(xí)一下剛剛上面提到的數(shù)據(jù):文件的權(quán)限字符為:『-rwxrwxrwx』谎仲, 這九個(gè)權(quán)限是三個(gè)三個(gè)一組的浙垫!其中,我們可以使用數(shù)字來(lái)代表各個(gè)權(quán)限郑诺,各權(quán)限的分?jǐn)?shù)對(duì)照表如下:
r:4
w:2
x:1
每種身份(owner/group/others)各自的三個(gè)權(quán)限(r/w/x)分?jǐn)?shù)是需要累加的夹姥,例如當(dāng)權(quán)限為: [-rwxrwx---] 分?jǐn)?shù)則是:
owner = rwx = 4+2+1 = 7
group = rwx = 4+2+1 = 7
others= --- = 0+0+0 = 0
所以等一下我們?cè)O(shè)定權(quán)限的變更時(shí),該文件的權(quán)限數(shù)字就是770啦辙诞!變更權(quán)限的指令chmod的語(yǔ)法是這樣的:
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="shell" cid="n69" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"> chmod [-R] xyz 文件或目錄</pre>
選項(xiàng)與參數(shù):
xyz : 就是剛剛提到的數(shù)字類型的權(quán)限屬性辙售,為 rwx 屬性數(shù)值的相加。
-R : 進(jìn)行遞歸(recursive)的持續(xù)變更飞涂,亦即連同此目錄下的所有文件都會(huì)變更
舉例來(lái)說(shuō)旦部,如果要將.bashrc這個(gè)文件所有的權(quán)限都設(shè)定啟用,那么命令如下:
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="" cid="n77" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">[root@www ~]# ls -al .bashrc
-rw-r--r-- 1 root root 395 Jul 4 11:45 .bashrc
[root@www ~]# chmod 777 .bashrc
[root@www ~]# ls -al .bashrc
-rwxrwxrwx 1 root root 395 Jul 4 11:45 .bashrc</pre>
那如果要將權(quán)限變成 -rwxr-xr-- 呢较店?那么權(quán)限的分?jǐn)?shù)就成為 4+2+1[4+0+0]=754志鹃。
符號(hào)類型改變文件權(quán)限
還有一個(gè)改變權(quán)限的方法呦!從之前的介紹中我們可以發(fā)現(xiàn)泽西,基本上就九個(gè)權(quán)限分別是(1)user (2)group (3)others三種身份啦! 那么我們就可以藉由u, g, o來(lái)代表三種身份的權(quán)限缰趋!
此外捧杉, a 則代表 all 亦即全部的身份陕见!那么讀寫(xiě)的權(quán)限就可以寫(xiě)成r, w, x!也就是可以使用底下的方式來(lái)看:
chmod | u g o a | +(加入) -(除去) =(設(shè)定) | r w x | 文件或目錄 |
---|---|---|---|---|
如果我們需要將文件權(quán)限設(shè)置為 -rwxr-xr-- 味抖,可以使用 chmod u=rwx,g=rx,o=r 文件名 來(lái)設(shè)定:
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="shell" cid="n97" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">[root@www ~]# ls -al .bashrc
-rwxr-xr-x 1 root root 395 Jul 4 11:45 .bashrc
[root@www ~]# chmod a+w .bashrc
[root@www ~]# ls -al .bashrc
-rwxrwxrwx 1 root root 395 Jul 4 11:45 .bashrc</pre>
而如果是要將權(quán)限去掉而不改變其他已存在的權(quán)限呢评甜?例如要拿掉全部人的可執(zhí)行權(quán)限,則:
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="shell" cid="n99" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">[root@www ~]# chmod a-x .bashrc
[root@www ~]# ls -al .bashrc
-rw-rw-rw- 1 root root 395 Jul 4 11:45 .bashrc</pre>