Linux的檔案許可權和目錄配置(二)

來源:互聯網
上載者:User

標籤:linux

一 linux 如何改變檔案屬性與許可權

我們知道檔案許可權對於一個系統的安全重要性,也知道檔案的許可權對於使用者與群組的相關性, 那如何修改一個檔案的屬性與許可權呢?

我們這裡介紹幾個常用於群組、擁有者、各種身份的許可權的指令。如下所示:

chgrp  :  改變檔案所屬群組

chown :  改變檔案擁有者

chmod :  改變檔案的許可權, SUID, SGID, SBIT等等的特性


1. 改變所屬群組, chgrp

[[email protected] ~]# chgrp [-R] dirname/filename ... 選項與參數: -R :進行遞迴(recursive)的持續變更,亦即連同次目錄下的所有檔案、目錄都更新成為這個群組之意。常常用在變更某一目錄內所有的檔案之情況。
範例:[[email protected] ~]# chgrp users install.log [[email protected] ~]# ls -l -rw-r--r-- 1 root users 68495 Jun 25 08:53 install.log [[email protected] ~]# chgrp testing install.log chgrp: invalid group name `testing‘ <== 發生錯誤資訊息囉~找不到這個群組名~

2. 改變檔案擁有者, chown

[[email protected] ~]# chown [-R] 帳號名稱 檔案或目錄 [[email protected] ~]# chown [-R] 帳號名稱:組名 檔案或目錄選項與參數:    -R : 進行遞迴(recursive)的持續變更,亦即連同次目錄下的所有檔案都變更範例:將install.log的擁有者改為bin這個帳號:[[email protected] ~]# chown bin install.log [[email protected] ~]# ls -l -rw-r--r-- 1 bin users 68495 Jun 25 08:53 install.log 範例:將install.log的擁有者與群組改回為root: [[email protected] ~]# chown root:root install.log [[email protected] ~]# ls -l -rw-r--r-- 1 root root 68495 Jun 25 08:53 install.log

3. 改變許可權, chmod

許可權的設定方法有兩種, 分別可以使用數字或者是符號來進行許可權的變更。

3.1 數字類型改變檔案許可權

Linux檔案的基本許可權就有九個,分別是owner/group/others三種身份各有自己的read/write/execute許可權

舉例:檔案的許可權字元為 -rwxrwxrwx  這九個許可權是三個三個一組的!其中,我們可以使用數字來代表各個許可權,各許可權的分數對照表如下:

r:4   w:2   x:1

每種身份(owner/group/others)各自的三個許可權(r/w/x)分數是需要累加的,例如當許可權為: [-rwxrwx---] 分數則是:

owner = rwx = 4+2+1 = 7

group = rwx = 4+2+1 = 7

others= --- = 0+0+0 = 0

所以我們設定許可權的變更時,該檔案的許可權數字就是770啦!變更許可權的指令chmod的文法是這樣的:

[[email protected] ~]# chmod [-R] xyz 檔案或目錄 選項與參數: xyz : 就是剛剛提到的數字類型的許可權屬性,為 rwx 屬性數值的相加。 -R : 進行遞迴(recursive)的持續變更,亦即連同次目錄下的所有檔案都會變更

舉例來說,如果要將.bashrc這個檔案所有的許可權都設定啟用,那麼就下達:

[[email protected] ~]# ls -al .bashrc -rw-r--r-- 1 root root 395 Jul 4 11:45 .bashrc [[email protected] ~]# chmod 777 .bashrc [[email protected] ~]# ls -al .bashrc -rwxrwxrwx 1 root root 395 Jul 4 11:45 .bashrc

那如果要將許可權變成 -rwxr-xr-- 呢?那麼許可權的分數就成為 [4+2+1][4+0+1][4+0+0]=754 囉!所以你需要下達:

[[email protected] ~]# chmod 754 filename

3.2 符號類型改變檔案許可權

還有一個改變許可權的方法呦!從之前的介紹中我們可以發現,基本上就九個許可權分別是(1)user (2)group (3)others三種身份啦!那麼我們就可以藉由u, g, o來代表三種身份的許可權!此外, a 則代表 all 亦即全部的身份!那麼讀寫的許可權就可以寫成r, w, x囉!也就是可以使用底下的方式來看:

來實驗一下吧!假如我們要設定一個檔案的許可權成為『-rwxr-xr-x』時,基本上就是:
o user (u):具有可讀、可寫、可執行檔許可權;
o group 與 others (g/o):具有可讀不執行的許可權。

所以就是:

[[email protected] ~]# chmod u=rwx,go=rx .bashrc # 注意喔!那個 u=rwx,go=rx 是連在一起的,中間並沒有任何空格符! [[email protected] ~]# ls -al .bashrc -rwxr-xr-x 1 root root 395 Jul 4 11:45 .bashrc

那麼假如是『 -rwxr-xr-- 』這樣的許可權呢?可以使用『 chmod u=rwx,g=rx,o=r filename 』來設定。此外,如果我們知道原先的檔案屬性,而我只想要增加.bashrc這個檔案的每個人均可寫入的許可權, 那麼我就可以使用:

[[email protected] ~]# ls -al .bashrc -rwxr-xr-x 1 root root 395 Jul 4 11:45 .bashrc [[email protected] ~]# chmod a+w .bashrc [[email protected] ~]# ls -al .bashrc -rwxrwxrwx 1 root root 395 Jul 4 11:45 .bashrc

而如果是要將許可權去掉而不改變其他已存在的許可權呢?例如要拿掉全部人的可執行許可權,則:

[[email protected] ~]# chmod a-x .bashrc [[email protected] ~]# ls -al .bashrc -rw-rw-rw- 1 root root 395 Jul 4 11:45 .bashrc


來源:http://www.cnblogs.com/yangjinjin/p/3165076.html

Linux的檔案許可權和目錄配置(二)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.