歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux管理 >> Linux維護 >> 如何使用chown命令改變Linux文件的所有者

如何使用chown命令改變Linux文件的所有者

日期:2017/3/2 10:33:40   编辑:Linux維護

Linux命令中,chown命令可改變文件的所有者,一般作為系統的管理員才會使用這個命令,一般的用戶是沒有權限使用的,下面小編就給大家詳細介紹下chown命令的用法。

系統管理員經常使用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

上面就是Linux chown命令的用法介紹了,作為系統的管理員,需要熟悉使用該命令,通過chown命令還可改變文件群組。

Copyright © Linux教程網 All Rights Reserved