Chown, Fchown, Lchown functions __ functions

Source: Internet
Author: User

NOTE: Reprint please indicate the source

Using the ls–l command gives the file owner and the group where the file owner is. You can modify the group that the file owner and the file owner belong to by using the functions Chown, Fchown, and Lchown functions, as follows:

Chown, Fchown, and Lchown functions

Header file



function form

int chown (const char *path,uid_t owner,gid_t Group)

int fchown (int fd, uid_t owner, gid_t Group);

int Lchown (const char *path,uid_t owner, gid_t Group);

return value



Whether to set errno




Description: These 3 functions are used to change the group where the owner of the file and the owner of the file are located. Of course, a process that modifies all permissions on a file must have the appropriate permissions (the process needs to have Cap_chown capabilities for the Linux system).

Chown function Error message:

Eacces: The path given to the file does not have access rights.

Efault: The path points to a file address error.

Eloop: The number of symbolic links in the path to the given file is more than that.

Enametoolong: path is too long.

Enoent: file does not exist.

ENOMEM: Insufficient kernel memory space.

Enotdir: The given file contains a path that is not part of the directory.

Eperm: The valid user ID is different from the owner of the file, and the process does not have access to the modify file owners.

Erofs: The file is located in a read-only file system.

Fchown function Error message:

EBADF: Illegal file descriptor.

Eio: An I/O error occurred.

Enoent: file does not exist.

Eperm: A valid user ID is different from a file owner, and the process does not have access to the modified file.

Erofs: The file is located in a read-only file system.


#include <sys/types.h>

#include <sys/stat.h>

#include <unistd.h>

#include <stdio.h>

int main (int argc, char *argv[])


if (argc!= 2)


printf ("Usage:%s filename\n", argv[0]);

return (1);


if (Chown (Argv[1], 0, 0) < 0)


Perror ("Cannot change the owner!");

return (1);


return (0);


Run Result:

[Root@localhost test]# ll ChDir

-r--r--r-x 1 Bin bin7778 Apr 01:43 chdir

[Root@localhost test]#./chown chdir

[Root@localhost test]# ll ChDir

-r--r--r-x 1 root 7778 Apr 01:43 chdir

[Root@localhost test]#

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.