天天看點

【Linux開發】如何更改linux檔案的擁有者及使用者組(chown和chgrp)

一、基本知識

  在Linux中,建立一個檔案時,該檔案的擁有者都是建立該檔案的使用者。該檔案使用者可以修改該檔案的擁有者及使用者組,當然root使用者可以修改任何檔案的擁有者及使用者組。在Linux中,對于檔案的權限(rwx),分為三部分,一部分是該檔案的擁有者所擁有的權限,一部分是該檔案所在使用者組的使用者所擁有的權限,另一部分是其他使用者所擁有的權限。對于檔案的權限請參考《​​Linux的chmod指令​​》

   檔案(含檔案夾,下同)的權限,在shell中可以通過chmod指令來完成,關于此請參考《​​Linux的chmod指令​​》。在 shell 中,可以使用chown指令來改變檔案所有者及使用者組,chgrp指令來改變檔案所在使用者組。在

Linux的C程式中,可以使用chown函數來改變檔案所有者,及所在使用者組。

  另外,在shell中,要修改檔案目前的使用者必須具有管理者root的權限。可以通過su指令切換到root使用者,也可以通過sudo獲得root的權限。

二、使用chown指令更改檔案擁有者

在 shell 中,可以使用chown指令來改變檔案所有者。chown指令是change owner(改變擁有者)的縮寫。需要要注意的是,使用者必須是已經存在系統中的,也就是隻能改變為在

/etc/passwd這個檔案中有記錄的使用者名稱才可以。

chown指令的用途很多,還可以順便直接修改使用者組的名稱。此外,如果要連目錄下的所有子目錄或檔案同時更改檔案擁有者的話,直接加上 -R的參數即可。

基本文法:

chown [-R] 賬号名稱 檔案或目錄

chown [-R] 賬号名稱:使用者組名稱 檔案或目錄

參數:

-R : 進行遞歸( recursive )的持續更改,即連同子目錄下的所有檔案、目錄

都更新成為這個使用者組。常常用在更改某一目錄的情況。

示例1:

[root@localhost home]# touch testfile //由 root 使用者建立檔案 

[root@localhost home]# ls testfile –l 

-rw--w--w- 1 root root 0 Jun 7 19:35 testfile //檔案的擁有者及擁有者級均為 root 

[root@localhost home]# chown yangzongde testfile //修改檔案擁有者為 yangzongde 

[root@localhost home]# ls testfile -l 

-rw--w--w- 1 yangzongde root 0 Jun 7 19:35 testfile //檢視檔案擁有者為 yangzongde,但組仍為 root 

示例2:

chown bin install.log

ls -l

-rw-r--r--  1 bin  users 68495 Jun 25 08:53 install.log

chown root:root install.log

ls -l

-rw-r--r--  1 root root 68495 Jun 25 08:53 install.log

三、使用chgrp指令更改檔案所屬使用者組

在shell中,可以使用chgrp指令來改變檔案所屬使用者組,該指令就是change group(改變使用者組)的縮寫。需要注意的是要改變成為的使用者組名稱,必須在 /etc/group裡存在,否則就會顯示錯誤。

chgrp [-R] 使用者組名稱 dirname/filename ...

示例3

-rw--w--w- 1 yangzongde root 0 Jun 7 19:35 testfile //檢視檔案擁有者為 yangzongde,但組為 root 

[root@localhost home]# chgrp yangzongde testfile //修改擁有者組為

yangzongde 

-rw--w--w- 1 yangzongde yangzongde 0 Jun 7 19:35 testfile 

[root@localhost home]# chown root:root testfile //

使用 chown 一次性修改擁有者及組 

-rw--w--w- 1 root root 0 Jun 7 19:35 testfile 

示例4

[root@linux ~]# chgrp users install.log

[root@linux ~]# ls -l

-rw-r--r--  1 root users 68495 Jun 25 08:53 install.log

示例5

更改為一個 /etc/group裡不存在的使用者組

[root@linux ~]# chgrp testing install.log

chgrp: invalid group name `testing' <== 出現錯誤資訊~找不到這個使用者組名~

四、chown 函數的使用

在Linux 的C 應用程式設計中,可以使用 chown 函數來修改檔案的擁有者及擁有者組。此函數聲明如下: 

/usr/include/unistd.h檔案中

<p style="margin-top: 0px; margin-bottom: 10px; padding-top: 0px; padding-bottom: 0px;"></p><div><span class="com" style="color: rgb(136, 0, 0);">/* Change the owner and group of FILE. */</span><span class="pln"> </span></div><div><span class="kwd" style="color: rgb(0, 0, 136);">extern</span><span class="pln"> </span><span class="kwd" style="color: rgb(0, 0, 136);">int</span><span class="pln"> chown </span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="pln">__const </span><span class="kwd" style="color: rgb(0, 0, 136);">char</span><span class="pln"> </span><span class="pun" style="color: rgb(102, 102, 0);">*</span><span class="pln">__file</span><span class="pun" style="color: rgb(102, 102, 0);">,</span><span class="pln"> </span><span class="typ" style="color: rgb(102, 0, 102);">__uid_t</span><span class="pln"> __owner</span><span class="pun" style="color: rgb(102, 102, 0);">,</span><span class="pln"> </span><span class="typ" style="color: rgb(102, 0, 102);">__gid_t</span><span class="pln"> __group</span><span class="pun" style="color: rgb(102, 102, 0);">)</span><span class="pln">__THROW __nonnull </span><span class="pun" style="color: rgb(102, 102, 0);">((</span><span class="lit" style="color: rgb(0, 102, 102);">1</span><span class="pun" style="color: rgb(102, 102, 0);">))</span><span class="pln"> __wur</span><span class="pun" style="color: rgb(102, 102, 0);">;</span><span class="pln"> </span></div><p style="margin-top: 0px; margin-bottom: 10px; padding-top: 0px; padding-bottom: 0px;"></p>      

此函數的第一個參數為欲修改使用者的檔案,第二個參數為修改後的檔案擁有者,第三個參數為修改後該檔案擁有者所在的組。

對于已打開的檔案,使用 fchown 函數來修改。其第一個參數為已打開檔案的檔案描述符,其他同 chown

函數。該函數聲明如下: 

<p style="margin-top: 0px; margin-bottom: 10px; padding-top: 0px; padding-bottom: 0px;"></p><div><span class="com" style="color: rgb(136, 0, 0);">/* Change the owner and group of the file that FD is open on. */</span><span class="pln"> </span></div><div><span class="kwd" style="color: rgb(0, 0, 136);">extern</span><span class="pln"> </span><span class="kwd" style="color: rgb(0, 0, 136);">int</span><span class="pln"> fchown </span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="kwd" style="color: rgb(0, 0, 136);">int</span><span class="pln"> __fd</span><span class="pun" style="color: rgb(102, 102, 0);">,</span><span class="pln"> </span><span class="typ" style="color: rgb(102, 0, 102);">__uid_t</span><span class="pln"> __owner</span><span class="pun" style="color: rgb(102, 102, 0);">,</span><span class="pln"> </span><span class="typ" style="color: rgb(102, 0, 102);">__gid_t</span><span class="pln"> __group</span><span class="pun" style="color: rgb(102, 102, 0);">)</span><span class="pln"> __THROW __wur</span><span class="pun" style="color: rgb(102, 102, 0);">;</span><span class="pln"> </span></div><p style="margin-top: 0px; margin-bottom: 10px; padding-top: 0px; padding-bottom: 0px;"></p>      

對于連接配接檔案,則可以使用 lchown 函數。其參數同于 chown 函數。 

<p style="margin-top: 0px; margin-bottom: 10px; padding-top: 0px; padding-bottom: 0px;"></p><div><span class="com" style="color: rgb(136, 0, 0);">/* Change owner and group of FILE, if it is a symbolic link the ownership of the symbolic </span></div><div><span class="com" style="color: rgb(136, 0, 0);">link is changed. */</span><span class="pln"> </span></div><div><span class="kwd" style="color: rgb(0, 0, 136);">extern</span><span class="pln"> </span><span class="kwd" style="color: rgb(0, 0, 136);">int</span><span class="pln"> lchown </span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="pln">__const </span><span class="kwd" style="color: rgb(0, 0, 136);">char</span><span class="pln"> </span><span class="pun" style="color: rgb(102, 102, 0);">*</span><span class="pln">__file</span><span class="pun" style="color: rgb(102, 102, 0);">,</span><span class="pln"> </span><span class="typ" style="color: rgb(102, 0, 102);">__uid_t</span><span class="pln"> __owner</span><span class="pun" style="color: rgb(102, 102, 0);">,</span><span class="pln"> </span><span class="typ" style="color: rgb(102, 0, 102);">__gid_t</span><span class="pln"> __group</span><span class="pun" style="color: rgb(102, 102, 0);">)</span><span class="pln"> __THROW __nonnull </span><span class="pun" style="color: rgb(102, 102, 0);">((</span><span class="lit" style="color: rgb(0, 102, 102);">1</span><span class="pun" style="color: rgb(102, 102, 0);">))</span><span class="pln"> __wur</span><span class="pun" style="color: rgb(102, 102, 0);">;</span><span class="pln"> </span></div><p style="margin-top: 0px; margin-bottom: 10px; padding-top: 0px; padding-bottom: 0px;"></p>      

以上這 3 個函數如果執行成功,将傳回 0,否則傳回-1。

繼續閱讀