天天看點

linux指令(6/11)--修改檔案的使用者組chgrp和檔案所有者chown

在lunix系統裡,檔案或目錄的權限的掌控以擁有者及所訴群組來管理。可以使用chgrp指令取變更檔案與目錄所屬群組,這種方式采用群組名稱或群組識别碼都可以。Chgrp指令就是change group的縮寫!要被改變的組名必須要在/etc/group檔案記憶體在才行。

1.指令格式:

chgrp [選項] [組] [檔案]

2.指令功能:

chgrp指令可采用群組名稱或群組識别碼的方式改變檔案或目錄的所屬群組。使用權限是超級使用者。 

3.指令參數:

必要參數:

-c 當發生改變時輸出調試資訊

-f 不顯示錯誤資訊

-R 處理指定目錄以及其子目錄下的所有檔案

-v 運作時顯示詳細的處理資訊

--dereference 作用于符号連結的指向,而不是符号連結本身

--no-dereference 作用于符号連結本身

選擇參數:

--reference=<檔案或者目錄>

--help 顯示幫助資訊

--version 顯示版本資訊

4.使用執行個體:

執行個體1:改變檔案的群組屬性 

指令:

chgrp -v bin log2012.log

輸出:

[root@localhost test]# ll

---xrw-r-- 1 root root 302108 11-13 06:03 log2012.log

[root@localhost test]# chgrp -v bin log2012.log

“log2012.log” 的所屬組已更改為 bin

---xrw-r-- 1 root bin  302108 11-13 06:03 log2012.log

說明:

将log2012.log檔案由root群組改為bin群組

執行個體2:根據指定檔案改變檔案的群組屬性 

chgrp --reference=log2012.log log2013.log

-rw-r--r-- 1 root root     61 11-13 06:03 log2013.log

[root@localhost test]#  chgrp --reference=log2012.log log2013.log 

-rw-r--r-- 1 root bin      61 11-13 06:03 log2013.log

改變檔案log2013.log 的群組屬性,使得檔案log2013.log的群組屬性和參考檔案log2012.log的群組屬性相同

執行個體3:改變指定目錄以及其子目錄下的所有檔案的群組屬性 

drwxr-xr-x 2 root root   4096 11-30 08:39 test6

[root@localhost test]# cd test6

[root@localhost test6]# ll

---xr--r-- 1 root root 302108 11-30 08:39 linklog.log

-rw-r--r-- 1 root root      0 11-30 08:39 log2017.log

[root@localhost test6]# cd ..

[root@localhost test]# chgrp -R bin test6

-rw-r--r-- 1 root bin     61 11-30 08:39 log2013.log

-rw-r--r-- 1 root bin      0 11-30 08:39 log2014.log

-rw-r--r-- 1 root bin      0 11-30 08:39 log2015.log

-rw-r--r-- 1 root bin      0 11-30 08:39 log2016.log

-rw-r--r-- 1 root bin      0 11-30 08:39 log2017.log

drwxr-xr-x 2 root bin    4096 11-30 08:39 test6

[root@localhost test]#

改變指定目錄以及其子目錄下的所有檔案的群組屬性

執行個體4:通過群組識别碼改變檔案群組屬性

chgrp -R 100 test6

[root@localhost test]# chgrp -R 100 test6

drwxr-xr-x 2 root users   4096 11-30 08:39 test6

指令名稱 : ​

​chown​

​ 使用權限 : ​

​root​

​ 

使用方式 : ​

​chown [-cfhvR] [--help] [--version] user[:group] file…​

利用 chown 可以将檔案的擁有者加以改變。這個指令隻有是由系統管理者(root)所使用,一般使用者沒有權限可以改變别人的檔案擁有者,也沒有權限可以自己的檔案擁有者改設為别人。隻有系統管理者(root)才有這樣的權限。 

參數:

-c或-change:作用與-v相似,但隻傳回修改的部分 

-f或–quiet或–silent:不顯示錯誤資訊 

-h或–no-dereference:隻對符号連結的檔案做修改,而不更改其他任何相關檔案 

-R或-recursive:遞歸處理,将指定目錄下的所有檔案及子目錄一并處理 

-v或–verbose:顯示指令執行過程 

–dereference:作用和-h剛好相反 

–help:顯示線上說明 

–reference=<參考檔案或目錄>:把指定檔案或目錄的所有者與所屬組,統統設定成和參考檔案或目錄的所有者與所屬組相同 

–version:顯示版本資訊 

chown指令使用舉例:(像我用的vps,就要先ssh登入)

​# chown [-R] [使用者名稱] [檔案或目錄]​

​# chown [-R] [使用者名稱:組名稱] [檔案或目錄]​

範例1:将test3.txt檔案的屬主改為test使用者。 

​# ls -l test3.txt​

-rw-r–r– 1 test root 0 2009-10-23 9:59 test3.txt 

​# chown test:root test3.txt​

​# ls -l test3.txt​

-rw-r–r– 1 test root 0 2009-10-23 9:59

範例2:chown所接的新的屬主和新的屬組之間可以使用:連接配接,屬主和屬組之一可以為空。如果屬主為空,應該是“:屬組”;如果屬組為空,“:”可以不用帶上。

​# ls -l test3.txt​

-rw-r–r– 1 test root 0 2009-10-23 9:59 test3.txt

​# chown :test test3.txt <==把檔案test3.txt的屬組改為test​

​# ls -l test3.txt​

-rw-r–r– 1 test test 0 2009-10-23 9:59 test3.txt

範例3:chown也提供了-R參數,這個參數對目錄改變屬主和屬組極為有用,可以通過加 -R參數來改變某個目錄下的所有檔案到新的屬主或屬組。 

​# ls -l testdir <== 檢視testdir目錄屬性​

drwxr-xr-x 2 usr root 0 2009-10-56 10:38 testdir/ <==檔案屬主是usr使用者,屬組是 root使用者 

​# ls -lr testdir <==檢視testdir目錄下所有檔案及其屬性​

total 0 

-rw-r–r– 1 usr root 0 2009-10-23 10:38 test1.txt 

-rw-r–r– 1 usr root 0 2009-10-23 10:38 test2.txt 

-rw-r–r– 1 usr root 0 2009-10-23 10:38 test3.txt 

​# chown -R test:test testdir/ <==修改testdir及它的下級目錄和所有檔案到新的使用者和使用者組​

​# ls -l testdir​

drwxr-xr-x 2 test test 0 2009-10-23 10:38 testdir/ 

​# ls -lr testdir​

-rw-r–r– 1 test test 0 2009-10-23 10:38 test1.txt 

-rw-r–r– 1 test test 0 2009-10-23 10:38 test2.txt 

-rw-r–r– 1 test test 0 2009-10-23 10:38 test3.txt

1 chgrp 改變檔案或目錄的所屬群組

①使用 man chgrp指令可以檢視chgrp在Linux中的說明文檔。

②指令格式

    chgrp [-參數選項] group dir/file

    如果要更改目錄下所有檔案或子目錄,加上參數-R。

③例子

   chgrp users install.log

  将檔案install.log的所屬群組改為users

2 chown 改變檔案或目錄的所屬使用者,也可順便修改所屬群組

①使用 man chown指令可以檢視chown在Linux中的說明文檔。

    chown [-參數選項] 使用者名 目錄或檔案名

   chown [-參數選項] 使用者名:群組名 目錄或檔案名

    如需更改子目錄或包含的檔案,加參數-R。

   chown bin install.log

将install.log檔案的所屬使用者改為bin

chown bin:root install.log

将install.log檔案的所屬使用者改為bin,所屬群組改為root。

3 chmod 改變目錄或檔案的權限

①使用 man chmod指令可以檢視chmod在Linux中的說明文檔。

② 權限說明

  Linux檔案的基本全線有九個,為 -rwxrwxrwx,三個為一組,分别代表owner/group/others三中身份的讀/寫/執行權限,如果對應位置為-,代表沒有相應權限。

改變權限指令有兩種方式:數字方式和符号方式

③數字方式改變檔案權限類型

r:4,w:2,x:1

chmod xyz 目錄或檔案名

x為owner身份的三種權限對應數字相加,y、z依次類推。

④符号方式改變檔案權限類型

u代表user身份,g代表group身份,o代表others,a代表所有身份;

+代表增權重限,-代表去掉權限,=代表設定權限

示例:

chmod u=rwx,go=x install.log

将install.log檔案給使用者身份設定可讀寫和執行的權限,group和others設定可執行的權限

chmod a+w install.log

設定所有使用者對install.log檔案都有寫權限

chmod u-x install.log

去掉使用者身份對install.log檔案的執行權限

⑤ 說明

+和-不會改變文檔原有權限,= 會重新設定原有文檔所有權限。

繼續閱讀