linux最基礎最常用的命令之一,也有不少小細節:chown例項

命令簡介

chown是linux最基礎的命令之一,也是最常用的命令之一。可以透過chown修改檔案、資料夾、符號連結的所有者資訊。

示例

chown需要root許可權執行。root許可權需要root使用者登入,或者sudo許可權。

1 使用chown修改檔案的所有者

更改檔案的所有者,是chown命令的簡單應用。引數需要一個新的使用者名稱,以及一個檔名。使用者名稱必須為系統中可用賬戶。

命令格式:

$ sudo chown new_owner file_name

示例:

yunzhong@DESKTOP-9VB7LN7:/tmp/chowntest$ ls -ltotal 4-rw-r——r—— 1 yunzhong yunzhong 77 Nov 11 13:47 friutes。txtyunzhong@DESKTOP-9VB7LN7:/tmp/chowntest$ sudo chown test1 friutes。txtyunzhong@DESKTOP-9VB7LN7:/tmp/chowntest$ ls -l friutes。txt-rw-r——r—— 1 test1 yunzhong 77 Nov 11 13:47 friutes。txt

2 使用chown更改檔案所在組

Chown命令用於修改檔案所屬組。在新組名前必須使用冒號。否則,它將被視為新owner。

命令格式:

$ sudo chown :new_group file_name

命令示例:

yunzhong@DESKTOP-9VB7LN7:/tmp/chowntest$ sudo chown :yunzhong friutes。txtyunzhong@DESKTOP-9VB7LN7:/tmp/chowntest$ lltotal 4-rw-r——r—— 1 test1 yunzhong 77 Nov 11 13:47 friutes。txt

3 chown命令使用使用者ID修改檔案的所有者

chown也可以透過使用者ID來更改檔案的所有者。但這裡有個問題:系統怎麼知道傳入的引數是使用者ID,還是使用者名稱?其實系統也無法區分,如果有一個使用者的名字和使用者ID相同,系統則認為傳入的為使用者名稱。可以使用id命令檢視使用者ID。

id -u user_name

命令格式,和透過使用者名稱更改所有者是一樣的。

示例:

yunzhong@DESKTOP-9VB7LN7:/tmp/chowntest$ id -u yunzhong1000yunzhong@DESKTOP-9VB7LN7:/tmp/chowntest$ sudo chown 1000 friutes。txtyunzhong@DESKTOP-9VB7LN7:/tmp/chowntest$ lltotal 4-rw-r——r—— 1 yunzhong yunzhong 77 Nov 11 13:47 friutes。txt

如果有一個使用者名稱為:1000,那麼所有者就改成了1000。

yunzhong@DESKTOP-9VB7LN7:/tmp/chowntest$ sudo useradd 1000yunzhong@DESKTOP-9VB7LN7:/tmp/chowntest$ sudo chown 1000 friutes。txtyunzhong@DESKTOP-9VB7LN7:/tmp/chowntest$ lltotal 4-rw-r——r—— 1 1000 yunzhong 77 Nov 11 13:47 friutes。txt

4 使用group ID 更改檔案所在組

和修改所有者類似,group也可以透過id來修改。檢視group ID的命令:

id -g group_name

命令格式:

$ sudo chown :group_id file_name

示例:

yunzhong@DESKTOP-9VB7LN7:/tmp/chowntest$ lltotal 4-rw-r——r—— 1 test1 test1 77 Nov 11 13:47 friutes。txtyunzhong@DESKTOP-9VB7LN7:/tmp/chowntest$ id -g yunzhong1000yunzhong@DESKTOP-9VB7LN7:/tmp/chowntest$ sudo chown :1000 friutes。txtyunzhong@DESKTOP-9VB7LN7:/tmp/chowntest$ lltotal 4-rw-r——r—— 1 test1 1000 77 Nov 11 13:47 friutes。txtyunzhong@DESKTOP-9VB7LN7:/tmp/chowntest$ id -g 10001006yunzhong@DESKTOP-9VB7LN7:/tmp/chowntest$ id -g test11002yunzhong@DESKTOP-9VB7LN7:/tmp/chowntest$ sudo chown :1002 friutes。txtyunzhong@DESKTOP-9VB7LN7:/tmp/chowntest$ lltotal 4-rw-r——r—— 1 test1 test1 77 Nov 11 13:47 friutes。txt

和上一節同理,如果group ID和另外一個group名字重複,則認為引數是group名字。

5 chown修改多個檔案的所有者

當需要修改多個檔案為一個所有者時,chown只需要執行一次就可以做到。chown支援一次傳入多個檔名,透過空格分割。

$ sudo chown new_owner file_name1 file_name2 file_name3

示例:

yunzhong@DESKTOP-9VB7LN7:/tmp/chowntest$ lltotal 12-rw-r——r—— 1 test1 test1 77 Nov 11 13:47 friutes。txt-rw-r——r—— 1 test1 test1 77 Nov 14 08:32 friutes。txt。1-rw-r——r—— 1 test1 test1 77 Nov 14 08:32 friutes。txt。2yunzhong@DESKTOP-9VB7LN7:/tmp/chowntest$ sudo chown yunzhong:yunzhong friutes。txt friutes。txt。1 friutes。txt。2yunzhong@DESKTOP-9VB7LN7:/tmp/chowntest$ lltotal 12-rw-r——r—— 1 yunzhong yunzhong 77 Nov 11 13:47 friutes。txt-rw-r——r—— 1 yunzhong yunzhong 77 Nov 14 08:32 friutes。txt。1-rw-r——r—— 1 yunzhong yunzhong 77 Nov 14 08:32 friutes。txt。2

如上面的示例,檔名有相同的字首,我們可以用更簡單的方式批次修改所有者:*萬用字元。

yunzhong@DESKTOP-9VB7LN7:/tmp/chowntest$ lltotal 12-rw-r——r—— 1 yunzhong yunzhong 77 Nov 11 13:47 friutes。txt-rw-r——r—— 1 yunzhong yunzhong 77 Nov 14 08:32 friutes。txt。1-rw-r——r—— 1 yunzhong yunzhong 77 Nov 14 08:32 friutes。txt。2yunzhong@DESKTOP-9VB7LN7:/tmp/chowntest$ sudo chown test1 friutes。txt*yunzhong@DESKTOP-9VB7LN7:/tmp/chowntest$ lltotal 12-rw-r——r—— 1 test1 yunzhong 77 Nov 11 13:47 friutes。txt-rw-r——r—— 1 test1 yunzhong 77 Nov 14 08:32 friutes。txt。1-rw-r——r—— 1 test1 yunzhong 77 Nov 14 08:32 friutes。txt。2

6 chown命令支援同時修改所有者和所在組

chown支援同時輸入所有者和所在組,一次修改檔案屬性。

命令格式:

$ sudo chown new_owner:new_group file_name

示例:

yunzhong@DESKTOP-9VB7LN7:/tmp/chowntest$ lltotal 12-rw-r——r—— 1 test1 yunzhong 77 Nov 11 13:47 friutes。txt-rw-r——r—— 1 test1 yunzhong 77 Nov 14 08:32 friutes。txt。1-rw-r——r—— 1 test1 yunzhong 77 Nov 14 08:32 friutes。txt。2yunzhong@DESKTOP-9VB7LN7:/tmp/chowntest$ sudo chown yunzhong:test1 friutes。txtyunzhong@DESKTOP-9VB7LN7:/tmp/chowntest$ lltotal 12-rw-r——r—— 1 yunzhong test1 77 Nov 11 13:47 friutes。txt

7 使用chown命令將一個檔案的所有者、所在組同步到另外一個檔案

命令格式:

$ sudo chown ——reference=souce_file destination_file

示例:

yunzhong@DESKTOP-9VB7LN7:/tmp/chowntest$ lltotal 12-rw-r——r—— 1 yunzhong test1 77 Nov 11 13:47 friutes。txt-rw-r——r—— 1 test1 yunzhong 77 Nov 14 08:32 friutes。txt。1-rw-r——r—— 1 test1 yunzhong 77 Nov 14 08:32 friutes。txt。2yunzhong@DESKTOP-9VB7LN7:/tmp/chowntest$ sudo chown ——reference = friutes。txt friutes。txt。1chown: failed to get attributes of ‘=’: No such file or directoryyunzhong@DESKTOP-9VB7LN7:/tmp/chowntest$ sudo chown ——reference=friutes。txt friutes。txt。1yunzhong@DESKTOP-9VB7LN7:/tmp/chowntest$ lltotal 12-rw-r——r—— 1 yunzhong test1 77 Nov 11 13:47 friutes。txt-rw-r——r—— 1 yunzhong test1 77 Nov 14 08:32 friutes。txt。1

注意一點,引數——reference = 之後不能有空格,如果有空格,則認為引數錯誤,如上示例。

8 列印chown 命令的變更記錄

chown可以列印檔案被自己操作的記錄。如果引數不傳入檔案,則什麼都不列印。

命令格式:

$ sudo chown -v

示例:

yunzhong@DESKTOP-9VB7LN7:/tmp/chowntest$ lltotal 12-rw-r——r—— 1 yunzhong test1 77 Nov 11 13:47 friutes。txt-rw-r——r—— 1 yunzhong test1 77 Nov 14 08:32 friutes。txt。1-rw-r——r—— 1 test1 yunzhong 77 Nov 14 08:32 friutes。txt。2yunzhong@DESKTOP-9VB7LN7:/tmp/chowntest$ sudo chown -v test1 friutes。txtchanged ownership of ‘friutes。txt’ from yunzhong to test1yunzhong@DESKTOP-9VB7LN7:/tmp/chowntest$ sudo chown -v test1 friutes。txtownership of ‘friutes。txt’ retained as test1

9 chown只有在檔案所有者、所在組發生變化的時候才打印資訊

和上面的-v引數不同,-c命令只有在所有者、所在組發生變化的時候才會列印。

命令格式:

$ sudo chown -c

示例:

yunzhong@DESKTOP-9VB7LN7:/tmp/chowntest$ lltotal 12-rw-r——r—— 1 test1 test1 77 Nov 11 13:47 friutes。txt-rw-r——r—— 1 yunzhong test1 77 Nov 14 08:32 friutes。txt。1-rw-r——r—— 1 test1 yunzhong 77 Nov 14 08:32 friutes。txt。2yunzhong@DESKTOP-9VB7LN7:/tmp/chowntest$ sudo chown -c test1:test1 friutes。txtyunzhong@DESKTOP-9VB7LN7:/tmp/chowntest$ sudo chown -c yunzhong:yunzhong friutes。txtchanged ownership of ‘friutes。txt’ from test1:test1 to yunzhong:yunzhongyunzhong@DESKTOP-9VB7LN7:/tmp/chowntest$

10 chown命令修改一個目錄的所有者、所在組

和修改檔案的操作類似,只要傳輸的改成目錄名就可以。

命令格式:

# 修改資料夾的所有者$ sudo chown new_owner directory_name# 修改資料夾的所在組$ sudo chown :new_group directory_name

示例:

yunzhong@DESKTOP-9VB7LN7:/tmp$ lldrwxr-xr-x 2 yunzhong yunzhong 4096 Nov 14 08:32 chowntestyunzhong@DESKTOP-9VB7LN7:/tmp$ sudo chown test1:test1 chowntest/yunzhong@DESKTOP-9VB7LN7:/tmp$ lldrwxr-xr-x 2 test1 test1 4096 Nov 14 08:32 chowntest

11 只有所有者和指定引數匹配,才修改所有者

透過設定引數——from,可以校驗檔案當前的所有者是否匹配。只有在匹配的情況下才會更改。

命令格式:

$ sudo chown ——from=current_owner new_owner file_name

示例:

yunzhong@DESKTOP-9VB7LN7:/tmp/chowntest$ lltotal 12-rw-r——r—— 1 yunzhong yunzhong 77 Nov 11 13:47 friutes。txtyunzhong@DESKTOP-9VB7LN7:/tmp/chowntest$ sudo chown ——from yunzhong test1 friutes。txtyunzhong@DESKTOP-9VB7LN7:/tmp/chowntest$ lltotal 12-rw-r——r—— 1 test1 yunzhong 77 Nov 11 13:47 friutes。txtyunzhong@DESKTOP-9VB7LN7:/tmp/chowntest$ sudo chown ——from yunzhong 1000 friutes。txtyunzhong@DESKTOP-9VB7LN7:/tmp/chowntest$ lltotal 12-rw-r——r—— 1 test1 yunzhong 77 Nov 11 13:47 friutes。txt

12 只有所在組和指定引數匹配,才修改所在組

其實,使用者可以透過引數單獨校驗所有者,所在組,也可以同時校驗兩者。

命令格式:

$ sudo chown ——from=:current_group :new_group file_name或者$ sudo chown ——from current_owner:current_group new_owner:new_group file_name

示例:

yunzhong@DESKTOP-9VB7LN7:/tmp/chowntest$ lltotal 12-rw-r——r—— 1 test1 yunzhong 77 Nov 11 13:47 friutes。txtyunzhong@DESKTOP-9VB7LN7:/tmp/chowntest$ sudo chown ——from yunzhong:yunzhong yunzhong:test1 friutes。txtyunzhong@DESKTOP-9VB7LN7:/tmp/chowntest$ lltotal 12-rw-r——r—— 1 test1 yunzhong 77 Nov 11 13:47 friutes。txtyunzhong@DESKTOP-9VB7LN7:/tmp/chowntest$ sudo chown ——from test1:yunzhong yunzhong:yunzhong friutes。txtyunzhong@DESKTOP-9VB7LN7:/tmp/chowntest$ lltotal 12-rw-r——r—— 1 yunzhong yunzhong 77 Nov 11 13:47 friutes。txt

13 更改一個資料夾下所有內容的所有者或所在組

使用-R引數,可以遞迴修改資料夾下所有的子檔案、子資料夾。當修改大量檔案的時候,這個引數可以幫助我們一次呼叫實現修改。

命令格式:

sudo chown -R new_owner:new_group directory_name

示例:

yunzhong@DESKTOP-9VB7LN7:/tmp$ ll -R chowntest/chowntest/:total 16-rw-r——r—— 1 yunzhong yunzhong 77 Nov 11 13:47 friutes。txt-rw-r——r—— 1 yunzhong yunzhong 77 Nov 14 08:32 friutes。txt。1-rw-r——r—— 1 yunzhong yunzhong 77 Nov 14 08:32 friutes。txt。2drwxr-xr-x 2 yunzhong yunzhong 4096 Nov 14 10:50 subdirchowntest/subdir:total 12-rw-r——r—— 1 yunzhong yunzhong 77 Nov 14 10:50 friutes。txt-rw-r——r—— 1 yunzhong yunzhong 77 Nov 14 10:50 friutes。txt。1-rw-r——r—— 1 yunzhong yunzhong 77 Nov 14 10:50 friutes。txt。2yunzhong@DESKTOP-9VB7LN7:/tmp$ sudo chown -R test1:test1 chowntest/yunzhong@DESKTOP-9VB7LN7:/tmp$ ll -R chowntest/chowntest/:total 16-rw-r——r—— 1 test1 test1 77 Nov 11 13:47 friutes。txt-rw-r——r—— 1 test1 test1 77 Nov 14 08:32 friutes。txt。1-rw-r——r—— 1 test1 test1 77 Nov 14 08:32 friutes。txt。2drwxr-xr-x 2 test1 test1 4096 Nov 14 10:50 subdirchowntest/subdir:total 12-rw-r——r—— 1 test1 test1 77 Nov 14 10:50 friutes。txt-rw-r——r—— 1 test1 test1 77 Nov 14 10:50 friutes。txt。1-rw-r——r—— 1 test1 test1 77 Nov 14 10:50 friutes。txt。2

14 修改符號連結的所有者、所在組

預設情況下,chown修改的是符號連結原始檔的所有者、所在組。可以使用-h修改符號連結檔案的資訊。

命令格式:

$ sudo -h new_owner:new_group sym_file

示例:

yunzhong@DESKTOP-9VB7LN7:/tmp/chowntest$ lltotal 16-rw-r——r—— 1 yunzhong yunzhong 77 Nov 11 13:47 friutes。txtdrwxr-xr-x 2 yunzhong yunzhong 4096 Nov 14 10:50 subdiryunzhong@DESKTOP-9VB7LN7:/tmp/chowntest$ ln -s friutes。txt friutes。txt。lnyunzhong@DESKTOP-9VB7LN7:/tmp/chowntest$ lltotal 16-rw-r——r—— 1 yunzhong yunzhong 77 Nov 11 13:47 friutes。txtlrwxrwxrwx 1 yunzhong yunzhong 11 Nov 14 10:59 friutes。txt。ln -> friutes。txtdrwxr-xr-x 2 yunzhong yunzhong 4096 Nov 14 10:50 subdiryunzhong@DESKTOP-9VB7LN7:/tmp/chowntest$ sudo chown test1:test1 friutes。txt。lnyunzhong@DESKTOP-9VB7LN7:/tmp/chowntest$ lltotal 16-rw-r——r—— 1 test1 test1 77 Nov 11 13:47 friutes。txtlrwxrwxrwx 1 yunzhong yunzhong 11 Nov 14 10:59 friutes。txt。ln -> friutes。txtdrwxr-xr-x 2 yunzhong yunzhong 4096 Nov 14 10:50 subdiryunzhong@DESKTOP-9VB7LN7:/tmp/chowntest$ sudo chown -h test1:test1 friutes。txt。lnyunzhong@DESKTOP-9VB7LN7:/tmp/chowntest$ lltotal 16-rw-r——r—— 1 test1 test1 77 Nov 11 13:47 friutes。txtlrwxrwxrwx 1 test1 test1 11 Nov 14 10:59 friutes。txt。ln -> friutes。txtdrwxr-xr-x 2 yunzhong yunzhong 4096 Nov 14 10:50 subdir

遮蔽錯誤資訊

預設情況下,chown執行錯誤資訊會列印到終端。可以使用-f引數,遮蔽錯誤資訊。

命令格式:

$ sudo chown -f new_owner file_name

示例:

yunzhong@DESKTOP-9VB7LN7:/tmp/chowntest$ sudo chown yunzhong not_found_filechown: cannot access ‘not_found_file’: No such file or directoryyunzhong@DESKTOP-9VB7LN7:/tmp/chowntest$ sudo chown -f yunzhong not_found_fileyunzhong@DESKTOP-9VB7LN7:/tmp/chowntest$ chown yunzhong friutes。txtchown: changing ownership of ‘friutes。txt’: Operation not permittedyunzhong@DESKTOP-9VB7LN7:/tmp/chowntest$ chown -f yunzhong friutes。txt