linux修改文件的拥有者及组 _VMware, Unix及操作系统讨论区_Weblogic技术|Tuxedo技术|中间件技术|Oracle论坛|JAVA论坛|Linux/Unix技术|hadoop论坛_联动北方技术论坛  
网站首页 | 关于我们 | 服务中心 | 经验交流 | 公司荣誉 | 成功案例 | 合作伙伴 | 联系我们 |
联动北方-国内领先的云技术服务提供商
»  游客             当前位置:  论坛首页 »  自由讨论区 »  VMware, Unix及操作系统讨论区 »
总帖数
1
每页帖数
101/1页1
返回列表
0
发起投票  发起投票 发新帖子
查看: 3197 | 回复: 0   主题: linux修改文件的拥有者及组         上一篇   下一篇 
zlasdf
注册用户
等级:新兵
经验:72
发帖:70
精华:0
注册:2011-10-17
状态:离线
发送短消息息给zlasdf 加好友    发送短消息息给zlasdf 发消息
发表于: IP:您无权察看 2015-8-3 18:01:37 | [全部帖] [楼主帖] 楼主

1.函数说明 
在创建时,任何文件的拥有者都是创建该文件的用户。当然用户可以修改该文件的拥有
者及拥有者组。在 shell 中,使用 chmod 和 chgrp函数来修改。示例如下: 

[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 

[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


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

//come from /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 函数。该函数声明如下: 

#if defined __USE_BSD || defined __USE_XOPEN_EXTENDED
/* 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。

--转自 北京联动北方科技有限公司




赞(0)    操作        顶端 
总帖数
1
每页帖数
101/1页1
返回列表
发新帖子
请输入验证码: 点击刷新验证码
您需要登录后才可以回帖 登录 | 注册
技术讨论