简体   繁体   English

如何在 Linux 中将 UID/GID 映射到用户名/组名

[英]How to map UID/GID to username/groupname in linux

In a C program on Linux, I get UID and GID of a file, but I want to print the username and group name.在 Linux 上的 C 程序中,我获得了文件的 UID 和 GID,但我想打印用户名和组名。 So how to map UID/GID to username/group name?那么如何将 UID/GID 映射到用户名/组名呢?

Use the getpwuid function to get the username associated with a uid:使用getpwuid函数获取与 uid 关联的用户名:

struct passwd *getpwuid(uid_t uid);

The name is returned in passwd.pw_name名称在passwd.pw_name返回

For groups, use getgrgid :对于组,使用getgrgid

struct group *getgrgid(gid_t gid);

The name is returned in group.gr_name名称在group.gr_name返回

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM