bash 当我有 groupid 时如何获取 groupname

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16163910/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 05:15:07  来源:igfitidea点击:

How to get groupname when I have the groupid

linuxbashldap

提问by moony

I have the parameters (especially the userid and the groupid) of a file read with the statcommand and I work in a network where the users and groups are specified on an ldap server.

我有使用stat命令读取的文件的参数(尤其是 userid 和 groupid),并且我在一个网络中工作,其中用户和组在 ldap 服务器上指定。

I've got the username out of the result of the command getent passwd userid.

我从命令的结果中得到了用户名getent passwd userid

Now my idea was to get the groupname with getent group groupid, but this doesn't work.

现在我的想法是用 获取组名getent group groupid,但这不起作用。

Can anyone tell me where I have my mistake or how I get the groupname?

谁能告诉我我的错误在哪里或我如何获得组名?

Thanks!

谢谢!

采纳答案by spbnick

Leaving aside the possibility that you're supplying a wrong group ID, this might be a bug in LDAP setup, which manifests in reverse group resolution not working. This is reinforced by the fact that this works on a plain "files" setup.

撇开您提供错误组 ID 的可能性不谈,这可能是 LDAP 设置中的一个错误,它表现为反向组解析不起作用。这在一个简单的“文件”设置上工作这一事实得到了加强。

The getent(1) states:

getent(1) 指出:

group     When no key is provided, use setgrent(3), getgrent(3), and
          endgrent(3) to enumerate the group database.  When one  or
          more  key arguments are provided, pass each numeric key to
          getgrgid(3) and each nonnumeric  key  to  getgrnam(3)  and
          display the result.

This could mean that getgrgid(3) fails on your setup.

这可能意味着 getgrgid(3) 在您的设置中失败。

To test this compile this program (getgrgid_test.c) with "make getgrgid_test":

要测试这个编译这个程序 (getgrgid_test.c) 与“make getgrgid_test”:

#include <stdio.h>
#include <sys/types.h>
#include <grp.h>

int
main(int argc, char **argv)
{
    int gid;
    struct group *g;

    if (argc != 2) {
        fprintf(stderr, "Invalid number of positional arguments\n");
        fprintf(stderr, "Usage getgrid_test GID\n");
        return 1;
    }
    gid = atoi(argv[1]);
    g = getgrgid(gid);
    if (g == NULL) {
        fprintf(stderr, "gid %d not found\n", gid);
        return 1;
    }
    printf("%s\n", g->gr_name);
    return 0;
}

Then run it with your gid like this:

然后像这样用你的 gid 运行它:

getgrgid_test GID

If it doesn't produce a group name report to your system administrators.

如果它没有向您的系统管理员报告组名。

Otherwise, if it does work, but "getent group GID" doesn't, it's a bug in "getent".

否则,如果它确实有效,但“getent group GID”无效,则是“getent”中的错误。

回答by Petesh

You probably have a configuration issue where either you've not got a line like:

您可能有一个配置问题,其中要么没有像这样的行:

group:    files ldap

in your /etc/nsswitch.conf.

在您的/etc/nsswitch.conf.

or your group information on the ldap server is in a form that doesn't have group id numbers e.g. of type groupOfNames, groupOfUniqueNamesinstead of being of type posixGroup.

或者您在 ldap 服务器上的组信息采用的形式没有组 ID 号,例如 type groupOfNamesgroupOfUniqueNames而不是 type posixGroup

Only posixGroup has the appropriate attributes that permit it's use as a valid group in linux/unix (i.e. the group id number which is needed to match). In that situation the ldap server doesn't return valid groups.

只有 posixGroup 具有适当的属性,允许将其用作 linux/unix 中的有效组(即需要匹配的组 ID 号)。在这种情况下,LDAP 服务器不会返回有效的组。

You canhave a perfectly functioning ldap configuration without any or all the remote groups being present in the output from getent group.

可以拥有完美运行的 ldap 配置,而无需任何或所有远程组出现在getent group.