1. 程式人生 > >linux命令(6/11)--修改文件的用戶組chgrp和文件所有者chown

linux命令(6/11)--修改文件的用戶組chgrp和文件所有者chown

test 使用權 屬性 ive lunix 文件的權限 功能 CA 連接

在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

[root@localhost test]# ll

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

說明:

將log2012.log文件由root群組改為bin群組

實例2:根據指定文件改變文件的群組屬性

命令:

chgrp --reference=log2012.log log2013.log

輸出:

[root@localhost test]# ll

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

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

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

[root@localhost test]# ll

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

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

說明:

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

實例3:改變指定目錄以及其子目錄下的所有文件的群組屬性

命令:

輸出:

[root@localhost test]# ll

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

[root@localhost test]# cd test6

[root@localhost test6]# ll

-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

[root@localhost test6]# cd ..

[root@localhost test]# ll

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

[root@localhost test]# ll

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
total 0
-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文件的執行權限

⑤ 說明

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

linux命令(6/11)--修改文件的用戶組chgrp和文件所有者chown