---
title: "更改linux文件的拥有者及用户组(chown和chgrp)"
author: "biandroid"
date: "2014-11-11T14:40:40Z"
tags: []
categories: ["操作系统"]
slug: "linux-chown-chgrp"
draft: false
lastmod: "2014-11-11T14:40:40Z"
id: 2154547
url: "https://biandroid.iteye.com/blog/2154547"
---

**一、基本知识**

  在Linux中，创建一个文件时，该文件的拥有者都是创建该文件的用户。该文件用户可以修改该文件的拥有者及用户组，当然root用户可以修改任何文件的拥有者及用户组。在Linux中，对于文件的权限（rwx），分为三部分，一部分是该文件的拥有者所拥有的权限，一部分是该文件所在用户组的用户所拥有的权限，另一部分是其他用户所拥有的权限。对于文件的权限请参考《[Linux的chmod命令](http://hubingforever.blog.163.com/blog/static/171040579201041432333648/ "阅读全文")》

   文件（含文件夹，下同）的权限，在shell中可以通过chmod命令来完成，关于此请参考《[Linux的chmod命令](http://hubingforever.blog.163.com/blog/static/171040579201041432333648/ "阅读全文")》。在 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 ...

**参数：**

-R : 进行递归( recursive )的持续更改，即连同子目录下的所有文件、目录

都更新成为这个用户组。常常用在更改某一目录的情况。

**示例3**

\[root@localhost home]# ls testfile -l 

-rw--w--w- 1 yangzongde root 0 Jun 7 19:35 testfile //查看文件拥有者为 yangzongde，但组为 root 

\[root@localhost home]# chgrp yangzongde testfile //修改拥有者组为 yangzongde 

\[root@localhost home]# ls testfile -l 

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

\[root@localhost home]# chown root:root testfile // 使用 chown 一次性修改拥有者及组 

\[root@localhost home]# ls testfile -l 

-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' &lt;== 出现错误信息～找不到这个用户组名～

**四、chown 函数的使用**

在Linux 的C 应用编程中，可以使用 chown 函数来修改文件的拥有者及拥有者组。此函数声明如下： 

/usr/include/unistd.h文件中

 

/* Change the owner and group of FILE. \*/ 

externint chown (\_\_const char\*\_\_file,\_\_uid\_t \_\_owner,\_\_gid\_t \_\_group)\_\_THROW \_\_nonnull ((1)) \_\_wur; 

此函数的第一个参数为欲修改用户的文件，第二个参数为修改后的文件拥有者，第三个参数为修改后该文件拥有者所在的组。

 

对于已打开的文件，使用 fchown 函数来修改。其第一个参数为已打开文件的文件描述符，其他同 chown 函数。该函数声明如下： 

 

/* Change the owner and group of the file that FD is open on. \*/ 

externint fchown (int \_\_fd,\_\_uid\_t \_\_owner,\_\_gid\_t \_\_group) \_\_THROW \_\_wur; 

对于连接文件，则可以使用 lchown 函数。其参数同于 chown 函数。 

 

/* Change owner and group of FILE, if it is a symbolic link the ownership of the symbolic 

link is changed. \*/ 

externint lchown (\_\_const char\*\_\_file,\_\_uid\_t \_\_owner,\_\_gid\_t \_\_group) \_\_THROW \_\_nonnull ((1)) \_\_wur; 

以上这 3 个函数如果执行成功，将返回 0，否则返回-1。