命令簡介
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.txt2 使用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.txt3 chown命令使用用戶ID修改文件的所有者
chown也可以通過用戶ID來更改文件的所有者。但這里有個問題:系統怎么知道傳入的參數是用戶ID,還是用戶名?其實系統也無法區分,如果有一個用戶的名字和用戶ID相同,系統則認為傳入的為用戶名。可以使用id命令查看用戶ID。
id -u ur_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 uradd 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.txt4 使用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.26 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.txt7 使用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 test19 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 chowntest11 只有所有者和指定參數匹配,才修改所有者
通過設定參數--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.txt12 只有所在組和指定參數匹配,才修改所在組
其實,用戶可以通過參數單獨校驗所有者,所在組,也可以同時校驗兩者。命令格式:
$ 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.txt13 更改一個文件夾下所有內容的所有者或所在組
使用-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.214 修改符號鏈接的所有者、所在組
默認情況下,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
本文發布于:2023-02-28 20:58:00,感謝您對本站的認可!
本文鏈接:http://www.newhan.cn/zhishi/a/167771216595141.html
版權聲明:本站內容均來自互聯網,僅供演示用,請勿用于商業和其他非法用途。如果侵犯了您的權益請與我們聯系,我們將在24小時內刪除。
本文word下載地址:chown(chown.doc
本文 PDF 下載地址:chown(chown.pdf
| 留言與評論(共有 0 條評論) |