chown命令示例

chown將指定檔的擁有者改為指定的用戶或組,用戶可以是用戶名或者用戶ID;組可以是組名或者組ID;檔是以空格分開的要改變許可權的檔列表,支持通配符。系統管理員經常使用chown命令,在將檔拷貝到另一個用戶的名錄下之後,讓用戶擁有使用該檔的許可權。

1.命令格式

chown [選項]... [所有者][:[組]] 檔...

2.命令功能

通過chown改變檔的擁有者和群組。在更改檔的所有者或所屬群組時,可以使用用戶名稱和用戶識別碼設置。普通用戶不能將自己的檔改變成其他的擁有者。其操作許可權一般為管理員。

3.命令參數:

必要參數:

  • -c 顯示更改的部分的資訊
  • -f 忽略錯誤資訊
  • -h 修復符號鏈接
  • -R 處理指定目錄以及其子目錄下的所有檔
  • -v 顯示詳細的處理資訊
  • -deference 作用於符號鏈接的指向,而不是鏈接檔本身

選擇參數:

  • --reference=<目錄或檔> 把指定的目錄/檔作為參考,把操作的檔/目錄設置成參考檔/目錄相同擁有者和群組
  • --from=<當前用戶:當前群組> 只有當前用戶和群組跟指定的用戶和群組相同時才進行改變
  • --help 顯示幫助資訊
  • --version 顯示版本資訊

4.使用實例

實例1:改變擁有者和群組

命令:

chown zaixian:zaixian newfile.txt

輸出:

[root@localhost scripts]# ll
total 4
-rw-r--r--. 1 root   root   11 Feb 17 02:38 newfile.txt
-rw-rw-r--. 1 zaixian nobody  0 Feb 17 02:11 run.pl
-rw-rw-r--. 1 zaixian nobody  0 Feb 17 02:10 run.py
-rw-rw-r--. 1 zaixian nobody  0 Feb 17 02:10 run.sh
[root@localhost scripts]#  chown zaixian:zaixian newfile.txt
[root@localhost scripts]# ll
total 4
-rw-r--r--. 1 zaixian zaixian 11 Feb 17 02:38 newfile.txt
-rw-rw-r--. 1 zaixian nobody  0 Feb 17 02:11 run.pl
-rw-rw-r--. 1 zaixian nobody  0 Feb 17 02:10 run.py
-rw-rw-r--. 1 zaixian nobody  0 Feb 17 02:10 run.sh
[root@localhost scripts]

實例2:改變檔擁有者和群組

命令:

chown root: newfile.txt

輸出:

[root@localhost scripts]# ll
total 4
-rw-r--r--. 1 zaixian zaixian 11 Feb 17 02:38 newfile.txt
-rw-rw-r--. 1 zaixian nobody  0 Feb 17 02:11 run.pl
-rw-rw-r--. 1 zaixian nobody  0 Feb 17 02:10 run.py
-rw-rw-r--. 1 zaixian nobody  0 Feb 17 02:10 run.sh
[root@localhost scripts]# chown root: newfile.txt
[root@localhost scripts]# ll
total 4
-rw-r--r--. 1 root   root   11 Feb 17 02:38 newfile.txt
-rw-rw-r--. 1 zaixian nobody  0 Feb 17 02:11 run.pl
-rw-rw-r--. 1 zaixian nobody  0 Feb 17 02:10 run.py
-rw-rw-r--. 1 zaixian nobody  0 Feb 17 02:10 run.sh
[root@localhost scripts]#

實例3:改變檔群組

命令:

chown :zaixian newfile.txt

輸出:

[root@localhost scripts]# ll
total 4
-rw-r--r--. 1 zaixian nobody 11 Feb 17 02:38 newfile.txt
-rw-rw-r--. 1 zaixian nobody  0 Feb 17 02:11 run.pl
-rw-rw-r--. 1 zaixian nobody  0 Feb 17 02:10 run.py
-rw-rw-r--. 1 zaixian nobody  0 Feb 17 02:10 run.sh
[root@localhost scripts]# chown :zaixian newfile.txt
[root@localhost scripts]# ll
total 4
-rw-r--r--. 1 zaixian zaixian 11 Feb 17 02:38 newfile.txt
-rw-rw-r--. 1 zaixian nobody  0 Feb 17 02:11 run.pl
-rw-rw-r--. 1 zaixian nobody  0 Feb 17 02:10 run.py
-rw-rw-r--. 1 zaixian nobody  0 Feb 17 02:10 run.sh
[root@localhost scripts]#

實例4:改變指定目錄以及其子目錄下的所有檔的擁有者和群組

命令:

chown -R -v root:zaixian scripts/

輸出:

[root@localhost zaixian]# ll scripts/
total 4
-rw-r--r--. 1 zaixian zaixian 11 Feb 17 02:38 newfile.txt
-rw-rw-r--. 1 zaixian nobody  0 Feb 17 02:11 run.pl
-rw-rw-r--. 1 zaixian nobody  0 Feb 17 02:10 run.py
-rw-rw-r--. 1 zaixian nobody  0 Feb 17 02:10 run.sh
[root@localhost zaixian]# chown -R -v root:zaixian scripts/
changed ownership of ‘scripts/run.py’ from zaixian:nobody to root:zaixian
changed ownership of ‘scripts/run.sh’ from zaixian:nobody to root:zaixian
changed ownership of ‘scripts/run.pl’ from zaixian:nobody to root:zaixian
changed ownership of ‘scripts/newfile.txt’ from zaixian:zaixian to root:zaixian
changed ownership of ‘scripts/’ from zaixian:nobody to root:zaixian
[root@localhost zaixian]# ll scripts/
total 4
-rw-r--r--. 1 root zaixian 11 Feb 17 02:38 newfile.txt
-rw-rw-r--. 1 root zaixian  0 Feb 17 02:11 run.pl
-rw-rw-r--. 1 root zaixian  0 Feb 17 02:10 run.py
-rw-rw-r--. 1 root zaixian  0 Feb 17 02:10 run.sh
[root@localhost zaixian]#

上一篇: chgrp命令示例 下一篇: df命令示例