1. 程式人生 > >Linux chown命令詳解使用格式和方法

Linux chown命令詳解使用格式和方法

指令名稱 : 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