chown [OPTION]... [OWNER][:[GROUP]] FILE...
chown [OPTION]... --reference=RFILE FILE...
$ ls -l Checkers.class
-rw-rw-r-- 1 fireway fireway 738 5月 4 07:16 Checkers.class
$ chown mail:mail Checkers.class
chown: 正在更改"Checkers.class" 的所有者: 不允许的操作
# chown mail:mail Checkers.class
# ls -l Checkers.class
-rw-rw-r-- 1 mail mail 738 5月 4 07:16 Checkers.class
# ls -l FrequencyAnalysis.java
-rwxrwxrwx 1 fireway fireway 5044 9月 21 08:26 FrequencyAnalysis.java
# chown root: FrequencyAnalysis.java
# ls -l FrequencyAnalysis.java
-rwxrwxrwx 1 root root 5044 9月 21 08:26 FrequencyAnalysis.java
# ls -l Test.class
-rw-rw-r-- 1 fireway fireway 995 9月 20 18:56 Test.class
# chown :mail Test.class
# ls -l Test.class
-rw-rw-r-- 1 fireway mail 995 9月 20 18:56 Test.class
$ id -a
uid=1000(fireway) gid=1000(fireway) 组=1000(fireway),4(adm),24(cdrom),27(sudo),30(dip),46(plugdev),108(lpadmin),124(sambashare)
$ id -G
1000 4 24 27 30 46 108 124
$ id -g
1000
$ id fireway
uid=1000(fireway) gid=1000(fireway) 组=1000(fireway),4(adm),24(cdrom),27(sudo),30(dip),46(plugdev),108(lpadmin),124(sambashare)
/**
* 文件名称: mychown.c
* 作者: firewaywei
* 时间: 2016年 10月 23日 星期日 10:59:24 CST
*/
#include <unistd.h>
int main()
{
if (chown("1", 1, -1) == -1)
{
perror("chown error:");
return -1;
}
return 0;
}
$ touch 1
$ ll 1
-rw-rw-r-- 1 fireway fireway 0 10月 23 11:01 1
$ su
密码:
# ll 1
-rw-rw-r-- 1 fireway fireway 0 10月 23 11:01 1
# ./a.out
# ll 1
-rw-rw-r-- 1 daemon fireway 0 10月 23 11:01 1
# id daemon
uid=1(daemon) gid=1(daemon) 组=1(daemon)
#include <unistd.h>
int chown(const char *pathname, uid_t owner, gid_t group); int fchown(int fd, uid_t owner, gid_t group); int fchownat(int fd, const char *pathname, uid_t owner, gid_t group, int flag); int lchown(const char *pathname, uid_t owner, gid_t group); All four return: 0 if OK, ?1 on error |
Name of option | Indicates ... | name argument |
_POSIX_CHOWN_RESTRICTED | whether use of chown is restricted | _PC_CHOWN_RESTRICTED |
/**
* 文件名: chown_restricted.c
* 文件内容:打印Linux平台_POSIX_CHOWN_RESTRICTED的值
* 作者:firewaywei
* 时间: 2016年 10月 23日 星期日 15:38:01 CST
*/
#include <unistd.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
static void pr_pathconf(char *mesg, char *path, int name);
int main(int argc, char *argv[])
{
if (argc != 2)
{
printf("usage: a.out <dirname>");
}
#ifdef _POSIX_CHOWN_RESTRICTED
printf("_POSIX_CHOWN_RESTRICTED is defined (val is %ld)\n", (long)(_POSIX_CHOWN_RESTRICTED + 0));
#else
printf("_POSIX_CHOWN_RESTRICTED is undefined\n");
#endif
#ifdef _PC_CHOWN_RESTRICTED
pr_pathconf("pathconf says _PC_CHOWN_RESTRICTED =", argv[1], _PC_CHOWN_RESTRICTED);
#else
printf("no symbol for _PC_CHOWN_RESTRICTED\n");
#endif
// 打印用户ID和组ID
printf("uid = %d, gid = %d\n", getuid(), getgid());
if (-1 == chown(argv[1], -1, 24))
{
perror("chown error:");
}
return 0;
}
static void pr_pathconf(char *mesg, char *path, int name)
{
fputs(mesg, stdout);
errno = 0;
long val = 0;
if ((val = pathconf(path, name)) < 0)
{
if (errno != 0)
{
if (EINVAL == errno)
{
fputs(" (not supported)\n", stdout);
}
else
{
char *err = strerror(errno);
printf("pathconf error, path = %s : %s\n", path, err);
}
}
else
{
fputs(" (no limit)\n", stdout);
}
}
else
{
printf(" %ld\n", val);
}
}
$ ./a.out 1
_POSIX_CHOWN_RESTRICTED is defined (val is 0)
pathconf says _PC_CHOWN_RESTRICTED = 1
uid = 1000, gid = 1000
$ ll 1
-rw-rw-r-- 1 fireway cdrom 0 10月 23 15:27 1
$ id -G
1000 4 24 27 30 46 108 124
原文:http://www.cnblogs.com/fireway/p/6014282.html