如何更改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...
参数:
-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' <== 出现错误信息~找不到这个用户组名~
四、chown 函数的使用
在Linux 的C 应用编程中,可以使用 chown 函数 来修改文件的拥有者及拥有者组。此函数声明如下:
/usr/include/unistd.h文件中
 
   

/* Change the owner and group of FILE. */
extern int 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. */
extern int 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. */
extern int lchown ( __const char * __file , __uid_t __owner , __gid_t __group ) __THROW__nonnull (( 1 )) __wur ;

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

  • 6
    点赞
  • 36
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值