歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux技術 >> 每天一個Linux命令(26)chown命令

每天一個Linux命令(26)chown命令

日期:2017/3/3 12:28:02   编辑:Linux技術

[b] chown命令改變某個文件或目錄的所有者和所屬的組,該命令可以向某個用戶授權,使該用戶變成指定文件的所有者或者改變文件所屬的組。[/b][b] (1)用法:[/b]

[b] 用法: chown [選項]... [所有者][:[組]] 文件... [/b] 或 chown [選項]... --reference=參考文件 文件...

[b] (2)功能:[/b] [b]功能: 更改每個文件的所有者和/或所屬組。[/b]

當使用 --referebce 參數時,將文件的所有者和所屬組更改為與指定參考文件相同。 用戶可以是用戶或者是用戶D,用戶組可以是組名或組id。文件名可以使由空格分開的文件列表,在文件名中可以包含通配符。

[b] (3)選項參數:[/b] 1) -R --recursive            遞歸處理,將指定目錄下的所有文件及子目錄一並處理

2) -f                   忽略錯誤信息 3) -v                   顯示詳細的處理信息

4) -c                   顯示更改的部分的信息 5) --reference=<參考文件或目錄>    把指定文件或目錄的擁有者與所屬群組全部設成和參考文件或目錄的擁有者與所屬群組相同

[b] (4)實例:[/b] 1)[root@localhost Documents]# chown -v sunjimeng findDir/Dir/{head_text,less1,less2} 改變文件的擁有者(指名多個文件時還可以用空格隔開)

[root@localhost Documents]# ls -l findDir/Dir
總用量 12
-r-xr-xr-x. 1 root root 664 5月   9 07:59 head_text
-r-xr-xr-x. 1 root root  45 5月   9 08:15 less1
-r-xr-xr-x. 1 root root  57 5月   9 08:16 less2
[root@localhost Documents]# chown -v sunjimeng findDir/Dir/{head_text,less1,less2}    在指明路徑時,findDir在當前目錄Documents下,所以不能帶/。
changed ownership of "findDir/Dir/head_text" from root to sunjimeng
changed ownership of "findDir/Dir/less1" from root to sunjimeng
changed ownership of "findDir/Dir/less2" from root to sunjimeng
[root@localhost Documents]# ls -l findDir/Dir
總用量 12
-r-xr-xr-x. 1 sunjimeng root 664 5月   9 07:59 head_text
-r-xr-xr-x. 1 sunjimeng root  45 5月   9 08:15 less1
-r-xr-xr-x. 1 sunjimeng root  57 5月   9 08:16 less2
2)[root@localhost Documents]# chown -vR sunjimeng findDir/Dir          遞歸地將文件夾下的所有文件的所屬組改變

[root@localhost Documents]# ls -l findDir/Dir
總用量 12
-r-xr-xr-x. 1 sunjimeng root 664 5月   9 07:59 head_text
-r-xr-xr-x. 1 sunjimeng root  45 5月   9 08:15 less1
-r-xr-xr-x. 1 sunjimeng root  57 5月   9 08:16 less2
[root@localhost Documents]# ls -l findDir
總用量 0
dr-xr-xr-x. 2 root root 46 5月  21 06:58 Dir            //在實例1中並沒有改變文件夾的所有者
[root@localhost Documents]# chown -v root findDir/Dir/{head_text,less1,less2}     //先把所有者復原為root
changed ownership of "findDir/Dir/head_text" from sunjimeng to root
changed ownership of "findDir/Dir/less1" from sunjimeng to root
changed ownership of "findDir/Dir/less2" from sunjimeng to root
[root@localhost Documents]# chown -vR sunjimeng findDir/Dir                   //再遞歸地把Dir文件夾和其下的文件所有者更改
changed ownership of "findDir/Dir/head_text" from root to sunjimeng
changed ownership of "findDir/Dir/less2" from root to sunjimeng
changed ownership of "findDir/Dir/less1" from root to sunjimeng
changed ownership of "findDir/Dir" from root to sunjimeng
[root@localhost Documents]# ls -l findDir              //在這裡查看,文件夾Dir及其下文件的所有者都已經改變為sunjimeng
總用量 0
dr-xr-xr-x. 2 sunjimeng root 46 5月  21 06:58 Dir
[root@localhost Documents]# ls -l findDir/Dir
總用量 12
-r-xr-xr-x. 1 sunjimeng root 664 5月   9 07:59 head_text
-r-xr-xr-x. 1 sunjimeng root  45 5月   9 08:15 less1
-r-xr-xr-x. 1 sunjimeng root  57 5月   9 08:16 less2
3[root@localhost Documents]# chown -vR root:sunjimeng findDir              改變所有者的同時改變所屬組

[root@localhost Documents]# chown -vR root:sunjimeng findDir
changed ownership of "findDir/Dir/head_text" from sunjimeng:root to root:sunjimeng
changed ownership of "findDir/Dir/less2" from sunjimeng:root to root:sunjimeng
changed ownership of "findDir/Dir/less1" from sunjimeng:root to root:sunjimeng
changed ownership of "findDir/Dir" from sunjimeng:root to root:sunjimeng
changed ownership of "findDir" from root:root to root:sunjimeng
[root@localhost Documents]# ll findDir
總用量 0
dr-xr-xr-x. 2 root sunjimeng 46 5月  21 06:58 Dir
[root@localhost Documents]# ll findDir/Dir
總用量 12
-r-xr-xr-x. 1 root sunjimeng 664 5月   9 07:59 head_text
-r-xr-xr-x. 1 root sunjimeng  45 5月   9 08:15 less1
-r-xr-xr-x. 1 root sunjimeng  57 5月   9 08:16 less2
4)[root@localhost Dir]# chown -c .root head_text less1                 只改變文件的所屬組(-c參數與-v類似)

[root@localhost Dir]# ll
總用量 12
-r-xr-xr-x. 1 root sunjimeng 664 5月   9 07:59 head_text
-r-xr-xr-x. 1 root sunjimeng  45 5月   9 08:15 less1
-r-xr-xr-x. 1 root sunjimeng  57 5月   9 08:16 less2
[root@localhost Dir]# chown -c .root head_text less1
changed ownership of "head_text" from root:sunjimeng to :root
changed ownership of "less1" from root:sunjimeng to :root
[root@localhost Dir]# chgrp -v root head_text less2
"head_text" 的所屬組已保留為root                      //因為文件的所屬組已經為root了,所以這裡顯示保留為root
changed group of "less2" from sunjimeng to root
[root@localhost Dir]# ll
總用量 12
-r-xr-xr-x. 1 root root 664 5月   9 07:59 head_text
-r-xr-xr-x. 1 root root  45 5月   9 08:15 less1
-r-xr-xr-x. 1 root root  57 5月   9 08:16 less2
5)[root@localhost Dir]# chown -v :sunjimeng less1 和[root@localhost Dir]# chown -v :sunjimeng less1 和[root@localhost Dir]# chown -v sunjimeng head_text

[root@localhost Dir]# ll
總用量 12
-r-xr-xr-x. 1 root root 664 5月   9 07:59 head_text
-r-xr-xr-x. 1 root root  45 5月   9 08:15 less1
-r-xr-xr-x. 1 root root  57 5月   9 08:16 less2
[root@localhost Dir]# chown -v :sunjimeng less1
changed ownership of "less1" from root:root to :sunjimeng
[root@localhost Dir]# chown -v .sunjimeng less2
changed ownership of "less2" from root:root to :sunjimeng
[root@localhost Dir]# chown -v sunjimeng head_text
changed ownership of "head_text" from root to sunjimeng
[root@localhost Dir]# ll
總用量 12
-r-xr-xr-x. 1 sunjimeng root      664 5月   9 07:59 head_text
-r-xr-xr-x. 1 root      sunjimeng  45 5月   9 08:15 less1
-r-xr-xr-x. 1 root      sunjimeng  57 5月   9 08:16 less2
[b] (5)其他:[/b]

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

以下選項是在指定了 -R 選項時被用於設置如何穿越目錄結構體系。 如果您指定了多於一個選項,那麼只有最後一個會生效。   -H 如果命令行參數是一個通到目錄的符號鏈接,則遍歷符號鏈接 -L 遍歷每一個遇到的通到目錄的符號鏈接

  -P 不遍歷任何符號鏈接(默認)   --help 顯示此幫助信息並退出

  --version 顯示版本信息並退出 如果沒有指定所有者,則不會更改。   所屬組若沒有指定也不會更改,但當加上 ":"時 GROUP 會更改為指定所有者的主要組。所有者和所屬組可以是數字或名稱。

Copyright © Linux教程網 All Rights Reserved