内容纲要

简介

Linux getent命令用来查看系统的数据库中的相关记录。

即使这些数据库不是在本地,比如ldap或者nis中的数据库, 也可以使用getent察看。长选项的强制或可选参数对对应的短选项也是强制或可选的。

一般语法

getent database [key …]

getent会在上面数据库中产找key对应的value

[root@my-test-host ~]# getent --help  
用法: getent [选项...] 数据库 [键 ...]
Get entries from administrative database.

  -i, --no-idn               disable IDN encoding
  -s, --service=CONFIG       要使用的服务配置
  -?, --help                 给出该系统求助列表
      --usage                给出简要的用法信息
  -V, --version              打印程序版本号

长选项的强制或可选参数对对应的短选项也是强制或可选的。

支持的数据库:
ahosts ahostsv4 ahostsv6 aliases ethers group gshadow hosts initgroups
netgroup networks passwd protocols rpc services shadow

For bug reporting instructions, please see:
<http://www.gnu.org/software/libc/bugs.html>.
[root@my-test-host ~]# getent --usage
用法: getent [-i?V] [-s CONFIG] [--no-idn] [--service=CONFIG] [--help]
            [--usage] [--version] 数据库 [键 ...]

参考实例

从hosts库中得到my-test-host的IP信息:

[root@my-test-host ~]# getent hosts
127.0.0.1       localhost localhost.localdomain localhost4 localhost4.localdomain4
127.0.0.1       localhost localhost.localdomain localhost6 localhost6.localdomain6
10.200.62.14    my-test-host
[root@my-test-host ~]# getent hosts ito
[root@my-test-host ~]# getent hosts my-test-host
10.100.10.10    my-test-host

从passwd库中得到账号zabbix信息:

[root@my-test-host ~]# getent passwd zabbix
zabbix:x:1001:1001::/home/zabbix:/bin/bash

从services中获取22、10050端口信息:

[root@my-test-host ~]# getent services 22
ssh                   22/tcp
[root@my-test-host ~]# getent services 10050
zabbix-agent          10050/tcp

普通用户netstat -tlnp无法看到非自己的进程名,这时getent命令非常有用

man page

GETENT(1)                             User Commands                            GETENT(1)

NAME
       getent - get entries from Name Service Switch libraries

SYNOPSIS
       getent database [key ...]

DESCRIPTION
       The  getent command displays entries from databases supported by the Name Service
       Switch libraries, which are configured in /etc/nsswitch.conf.  If one or more key
       arguments  are  provided, then only the entries that match the supplied keys will
       be displayed.  Otherwise, if no key is provided, all entries  will  be  displayed
       (unless the database does not support enumeration).

       The database may be any of those supported by the GNU C Library, listed below:

          ahosts    When  no key is provided, use sethostent(3), gethostent(3), and end‐
                    hostent(3) to enumerate the hosts database.  This  is  identical  to
                    using hosts.  When one or more key arguments are provided, pass each
                    key  in  succession  to  getaddrinfo(3)  with  the  address   family
                    AF_UNSPEC, enumerating each socket address structure returned.

          ahostsv4  Same as ahosts, but use the address family AF_INET.

          ahostsv6  Same  as  ahosts,  but use the address family AF_INET6.  The call to
                    getaddrinfo(3) in this case includes the AI_V4MAPPED flag.

          aliases   When no key is provided,  use  setaliasent(3),  getaliasent(3),  and
                    endaliasent(3)  to enumerate the aliases database.  When one or more
                    key arguments are provided, pass each key in succession to getalias‐
                    byname(3) and display the result.

          ethers    When  one  or more key arguments are provided, pass each key in suc‐
                    cession to ether_aton(3) and  ether_hostton(3)  until  a  result  is
                    obtained,  and  display the result.  Enumeration is not supported on
                    ethers, so a key must be provided.

          group     When no key is provided,  use  setgrent(3),  getgrent(3),  and  end‐
                    grent(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.

          gshadow   When  no  key  is  provided, use setsgent(3), getsgent(3), and ends‐
                    gent(3) to enumerate the gshadow database.  When  one  or  more  key
                    arguments  are  provided, pass each key in succession to getsgnam(3)
                    and display the result.

          hosts     When no key is provided, use sethostent(3), gethostent(3), and  end‐
                    hostent(3)  to  enumerate  the hosts database.  When one or more key
                    arguments are provided, pass each key to gethostbyaddr(3)  or  geth‐
                    ostbyname2(3), depending on whether a call to inet_pton(3) indicates
                    that the key is an IPv6 or IPv4 address  or  not,  and  display  the
                    result.

          initgroups
                    When  one  or more key arguments are provided, pass each key in suc‐
                    cession to getgrouplist(3) and display the result.   Enumeration  is
                    not supported on initgroups, so a key must be provided.

          netgroup  When  one key is provided, pass the key to setnetgrent(3) and, using
                    getnetgrent(3) display the resulting string triple (hostname,  user‐
                    name, domainname).  Alternatively, three keys may be provided, which
                    are interpreted as the hostname, username and domainname to match to
                    a  netgroup  name  via  innetgr(3).  Enumeration is not supported on
                    netgroup, so either one or three keys must be provided.

          networks  When no key is provided, use setnetent(3), getnetent(3), and  endne‐
                    tent(3)  to  enumerate  the networks database.  When one or more key
                    arguments are provided, pass each numeric key to getnetbyaddr(3) and
                    each nonnumeric key to getnetbyname(3) and display the result.

          passwd    When  no  key  is  provided, use setpwent(3), getpwent(3), and endp‐
                    went(3) to enumerate the passwd database.   When  one  or  more  key
                    arguments  are  provided,  pass  each numeric key to getpwuid(3) and
                    each nonnumeric key to getpwnam(3) and display the result.

          protocols When no key is provided,  use  setprotoent(3),  getprotoent(3),  and
                    endprotoent(3)  to  enumerate  the  protocols database.  When one or
                    more key arguments are provided, pass each numeric key to  getproto‐
                    bynumber(3) and each nonnumeric key to getprotobyname(3) and display
                    the result.

          rpc       When no key is provided, use setrpcent(3), getrpcent(3), and  endrp‐
                    cent(3)  to  enumerate the rpc database.  When one or more key argu‐
                    ments are provided, pass each numeric key to  getrpcbynumber(3)  and
                    each nonnumeric key to getrpcbyname(3) and display the result.

          services  When  no key is provided, use setservent(3), getservent(3), and end‐
                    servent(3) to enumerate the services database.  When one or more key
                    arguments  are provided, pass each numeric key to getservbynumber(3)
                    and each nonnumeric key to getservbyname(3) and display the result.

          shadow    When no key is provided,  use  setspent(3),  getspent(3),  and  end‐
                    spent(3)  to  enumerate  the  shadow database.  When one or more key
                    arguments are provided, pass each key in succession  to  getspnam(3)
                    and display the result.

EXIT STATUS
       One of the following exit values can be returned by getent:

          0         Command completed successfully.

          1         Missing arguments, or database unknown.

          2         One or more supplied key could not be found in the database.

          3         Enumeration not supported on this database.

SEE ALSO
       nsswitch.conf(5)

COLOPHON
       This  page is part of release 3.53 of the Linux man-pages project.  A description
       of  the  project,  and  information  about  reporting  bugs,  can  be  found   at
       http://www.kernel.org/doc/man-pages/.

Linux                                  2013-03-15                              GETENT(1)