Linux环境编程文件拥有者和组操作之chown, fchown, lchown, fchownat

本文介绍如何使用U盘启动盘安装原版Win7系统。首先制作U盘启动盘,然后通过U盘启动进入PE系统,使用装机工具加载系统镜像,选择系统版本及安装分区,最后重启电脑完成安装。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本篇记录Linux环境编程文件拥有者和组操作之chown, fchown, lchown, fchownat函数的基本用法。

首先查看帮助文档

这里先创建一个tom用户和tom组

sudo useradd tom

查看所有用户 

cat /etc/passwd

 

 查看所有组

getent group

1.chown函数用于更改文件的所有者和组。

函数名chown
相关函数fchown, lchown, fchownat
表头文件#include <unistd.h>
函数定义int chown(const char *pathname, uid_t owner, gid_t group);
函数说明

更改文件的所有者和组,如果它是一个符号链接,则会被取消引用.

参数说明:

pathname:文件名

owner:所有者id

group:组id

返回值成功时返回0,失败则返回 -1,错误码设置在errno中。

示例:

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

using namespace std;

int main()
{
    //const char *dirname = "/home/scott/trunk/command2";

    const char *filelink = "/home/scott/trunk/command2/chmod_test1_link";
    if(chown(filelink, 1002, 1002) == -1) {
        perror("Error changing chmod_test1_link file owner");
    }
    else {
        printf("changing chmod_test1_link file owner successfully\n");
    }

    const char *filename = "/home/scott/trunk/command2/chmod_test2.txt";

    if(chown(filename, 1002, 1002) == -1) {
        perror("Error changing chmod_test1.txt file owner");
    }
    else
    {
        printf("changing chmod_test1.txt file owner successfully\n");
    }

    cout << "Hello Ubuntu1804!" << endl;
    return 0;
}

运行前:

运行后:

2.fchown函数用于更改文件的所有者和组。

函数名fchown
相关函数chown, lchown, fchownat
表头文件#include <unistd.h>
函数定义int fchown(int fd, uid_t owner, gid_t group);
函数说明

更改文件描述符对就的文件的所有者和组,如果它是一个符号链接,则会被取消引用。

参数说明:

fd:文件描述符

owner:所有者id

group:组id

返回值成功时返回0,失败则返回 -1,错误码设置在errno中。

示例:

#include <iostream>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

using namespace std;

int main()
{
    const char *filename = "/home/scott/trunk/command2/chmod_test3.txt";
    int fd = open(filename, O_RDONLY); // 打开文件以只读模式
    if (fd == -1) {
        perror("Failed to open file");
        return 1;
    }


    if(fchown(fd, 1002, 1002) == -1) {
        perror("Error changing chmod_test3.txt file owner");
    }
    else
    {
        printf("changing chmod_test3.txt file owner successfully\n");
    }

    cout << "Hello Ubuntu1804!" << endl;
    return 0;
}

运行结果:

3.lchown函数用于更改文件的所有者和组。

函数名lchown
相关函数fchown, chown, fchownat
表头文件#include <unistd.h>
函数定义int lchown(const char *pathname, uid_t owner, gid_t group);
函数说明

更改文件的所有者和组,如果它是一个符号链接,也会引用。

参数说明:

pathname:文件名

owner:所有者id

group:组id

返回值成功时返回0,失败则返回 -1,错误码设置在errno中。

示例:

#include <iostream>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

using namespace std;

int main()
{
    //const char *dirname = "/home/scott/trunk/command2";

    const char *filelink = "/home/scott/trunk/command2/chmod_test1_link";
    if(lchown(filelink, 1002, 1002) == -1) {
        perror("Error changing chmod_test1_link file owner");
    }
    else {
        printf("changing chmod_test1_link file owner successfully\n");
    }

    const char *filename = "/home/scott/trunk/command2/chmod_test4.txt";

    if(lchown(filename, 1002, 1002) == -1) {
        perror("Error changing chmod_test4.txt file owner");
    }
    else
    {
        printf("changing chmod_test4.txt file owner successfully\n");
    }

    cout << "Hello Ubuntu1804!" << endl;
    return 0;
}

运行结果:

4.fchownat函数用于更改文件的所有者和组。

函数名fchownat
相关函数fchown, lchown, chown
表头文件

#include <unistd.h>

#include <fcntl.h>

函数定义int fchownat(int dirfd, const char *pathname, uid_t owner, gid_t group, int flags);
函数说明

更改文件的所有者和组,如果它是一个符号链接,则会被取消引用。如是pathname是绝对文件名,则dirfd会被忽略。

参数说明:

  • dirfd: 文件描述符,表示目录的基础路径。如果设置为 AT_FDCWD,则表示当前工作目录。
  • pathname: 要修改所有权的目标文件名。
  • owner: 新的所有者 UID。
  • group: 新的组 GID。
  • flags: 控制选项,可以是以下值之一或多者的组合:
    • AT_SYMLINK_NOFOLLOW: 如果目标是一个符号链接,则不跟随符号链接。
    • AT_EMPTY_PATH: 当 pathname 为空字符串时,作用于由 dirfd 表示的打开文件。
返回值成功时返回0,失败则返回 -1,错误码设置在errno中。

示例:

#include <iostream>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

using namespace std;

int main()
{
    const char *dirname = "/home/scott/trunk/command2";
    const char *filename = "chmod_test5.txt";

    int dir_fd = open(dirname, O_RDONLY); // 打开文件以只读模式
    if (dir_fd == -1) {
        perror("Failed to open file");
        return 1;
    }

    if(fchownat(dir_fd, filename, 1002, 1002, 0) == -1) {
        perror("Error changing chmod_test5.txt file owner");
    }
    else
    {
        printf("changing chmod_test5.txt file owner successfully\n");
    }

    cout << "Hello Ubuntu1804!" << endl;
    return 0;
}

运行结果:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值