chown将指定檔案的擁有者改為指定的使用者或組,使用者可以是使用者名或者使用者ID;組可以是組名或者組ID;檔案是以空格分開的要改變權限的檔案清單,支援通配符。系統管理者經常使用chown指令,在将檔案拷貝到另一個使用者的名錄下之後,讓使用者擁有使用該檔案的權限。
1.指令格式:
chown [選項]... [所有者][:[組]] 檔案...
2.指令功能:
通過chown改變檔案的擁有者和群組。在更改檔案的所有者或所屬群組時,可以使用使用者名稱和使用者識别碼設定。普通使用者不能将自己的檔案改變成其他的擁有者。其操作權限一般為管理者。
3.指令參數:
必要參數:
-c 顯示更改的部分的資訊
-f 忽略錯誤資訊
-h 修複符号連結
-R 處理指定目錄以及其子目錄下的所有檔案
-v 顯示詳細的處理資訊
-deference 作用于符号連結的指向,而不是連結檔案本身
選擇參數:
--reference=<目錄或檔案> 把指定的目錄/檔案作為參考,把操作的檔案/目錄設定成參考檔案/目錄相同擁有者和群組
--from=<目前使用者:目前群組> 隻有目前使用者和群組跟指定的使用者和群組相同時才進行改變
--help 顯示幫助資訊
--version 顯示版本資訊
4.使用執行個體:
執行個體1:改變擁有者和群組
指令:
chown mail:mail log2012.log
輸出:

[root@localhost test6]# ll
---xr--r-- 1 root users 302108 11-30 08:39 linklog.log
---xr--r-- 1 root users 302108 11-30 08:39 log2012.log
-rw-r--r-- 1 root users 61 11-30 08:39 log2013.log
-rw-r--r-- 1 root users 0 11-30 08:39 log2014.log
-rw-r--r-- 1 root users 0 11-30 08:39 log2015.log
-rw-r--r-- 1 root users 0 11-30 08:39 log2016.log
-rw-r--r-- 1 root users 0 11-30 08:39 log2017.log
[root@localhost test6]# chown mail:mail log2012.log
[root@localhost test6]# ll
---xr--r-- 1 root users 302108 11-30 08:39 linklog.log
---xr--r-- 1 mail mail 302108 11-30 08:39 log2012.log
-rw-r--r-- 1 root users 61 11-30 08:39 log2013.log
-rw-r--r-- 1 root users 0 11-30 08:39 log2014.log
-rw-r--r-- 1 root users 0 11-30 08:39 log2015.log
-rw-r--r-- 1 root users 0 11-30 08:39 log2016.log
-rw-r--r-- 1 root users 0 11-30 08:39 log2017.log
[root@localhost test6]#

說明:
執行個體2:改變檔案擁有者和群組
chown root: log2012.log
輸出:

[root@localhost test6]# ll
總計 604
---xr--r-- 1 root users 302108 11-30 08:39 linklog.log
---xr--r-- 1 mail mail 302108 11-30 08:39 log2012.log
-rw-r--r-- 1 root users 61 11-30 08:39 log2013.log
-rw-r--r-- 1 root users 0 11-30 08:39 log2014.log
-rw-r--r-- 1 root users 0 11-30 08:39 log2015.log
-rw-r--r-- 1 root users 0 11-30 08:39 log2016.log
-rw-r--r-- 1 root users 0 11-30 08:39 log2017.log
[root@localhost test6]# chown root: log2012.log
[root@localhost test6]# ll
總計 604
---xr--r-- 1 root users 302108 11-30 08:39 linklog.log
---xr--r-- 1 root root 302108 11-30 08:39 log2012.log
-rw-r--r-- 1 root users 61 11-30 08:39 log2013.log
-rw-r--r-- 1 root users 0 11-30 08:39 log2014.log
-rw-r--r-- 1 root users 0 11-30 08:39 log2015.log
-rw-r--r-- 1 root users 0 11-30 08:39 log2016.log
-rw-r--r-- 1 root users 0 11-30 08:39 log2017.log
[root@localhost test6]#

執行個體3:改變檔案群組
chown :mail log2012.log

[root@localhost test6]# ll
總計 604
---xr--r-- 1 root users 302108 11-30 08:39 linklog.log
---xr--r-- 1 root root 302108 11-30 08:39 log2012.log
-rw-r--r-- 1 root users 61 11-30 08:39 log2013.log
-rw-r--r-- 1 root users 0 11-30 08:39 log2014.log
-rw-r--r-- 1 root users 0 11-30 08:39 log2015.log
-rw-r--r-- 1 root users 0 11-30 08:39 log2016.log
-rw-r--r-- 1 root users 0 11-30 08:39 log2017.log
[root@localhost test6]# chown :mail log2012.log
[root@localhost test6]# ll
總計 604
---xr--r-- 1 root users 302108 11-30 08:39 linklog.log
---xr--r-- 1 root mail 302108 11-30 08:39 log2012.log
-rw-r--r-- 1 root users 61 11-30 08:39 log2013.log
-rw-r--r-- 1 root users 0 11-30 08:39 log2014.log
-rw-r--r-- 1 root users 0 11-30 08:39 log2015.log
-rw-r--r-- 1 root users 0 11-30 08:39 log2016.log
-rw-r--r-- 1 root users 0 11-30 08:39 log2017.log

執行個體4:改變指定目錄以及其子目錄下的所有檔案的擁有者和群組
chown -R -v root:mail test6

[root@localhost test]# ll
drwxr-xr-x 2 root users 4096 11-30 08:39 test6
[root@localhost test]# chown -R -v root:mail test6
“test6/log2014.log” 的所有者已更改為 root:mail
“test6/linklog.log” 的所有者已更改為 root:mail
“test6/log2015.log” 的所有者已更改為 root:mail
“test6/log2013.log” 的所有者已更改為 root:mail
“test6/log2012.log” 的所有者已保留為 root:mail
“test6/log2017.log” 的所有者已更改為 root:mail
“test6/log2016.log” 的所有者已更改為 root:mail
“test6” 的所有者已更改為 root:mail
[root@localhost test]# ll
drwxr-xr-x 2 root mail 4096 11-30 08:39 test6
[root@localhost test]# cd test6
[root@localhost test6]# ll
總計 604
---xr--r-- 1 root mail 302108 11-30 08:39 linklog.log
---xr--r-- 1 root mail 302108 11-30 08:39 log2012.log
-rw-r--r-- 1 root mail 61 11-30 08:39 log2013.log
-rw-r--r-- 1 root mail 0 11-30 08:39 log2014.log
-rw-r--r-- 1 root mail 0 11-30 08:39 log2015.log
-rw-r--r-- 1 root mail 0 11-30 08:39 log2016.log
-rw-r--r-- 1 root mail 0 11-30 08:39 log2017.log

部落格中所涉及到的圖檔都有版權,請謹慎使用