Home » 2014 » October » 12 » GETENT command in linux to get entries from administrative database

7:34 AM
GETENT command in linux to get entries from administrative database

Share on Google+

getent - get entries from Name Service Switch libraries or administrative databases like passwd, shadow, group etc.

FORMAT
getent [Options..] 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).

Usage: getent [OPTION...] database [key ...]
Get entries from administrative database.

  -s, --service=CONFIG       Service configuration to be used
  -?, --help                 Give this help list
      --usage                Give a short usage message
  -V, --version              Print program version

Mandatory or optional arguments to long options are also mandatory or optional
for any corresponding short options.


 

Examples:

1. Get userid or groupid details from passwd file and group file using the key as username, userid, groupid etc.

[shanky@HYDBMW ~]$ getent passwd shanky
shanky:x:506:509::/home/shanky:/bin/bash

[shanky@HYDBMW ~]$ getent group test
test:x:503:
[shanky@HYDBMW ~]$ getent group shanky
shanky:x:509:

2. Get the ip address of the hostname from hosts database

[shanky@HYDBMW ~]$ getent hosts
127.0.0.1       localhost.localdomain localhost.localdomain localhost4 localhost4.localdomain4 localhost template6 harden6 HYDBMW
127.0.0.1       localhost.localdomain localhost.localdomain localhost6 localhost6.localdomain6 localhost template6 harden6 HYDBMW

172.21.5.224    nfrsat01.ad.infosys.com nfrsat01
 

[shanky@HYDBMW ~]$ getent hosts HYDBMW.ad.infosys.com
10.66.56.41     HYDBMW.ad.infosys.com

 


Following are the supported databases for this command:
ahosts ahostsv4 ahostsv6 aliases ethers group gshadow hosts netgroup
networks passwd protocols rpc services shadow


ahosts  

  When no key is provided, use sethostent(3), gethostent(3), and endhostent(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 getaliasbyname(3) and display the result.

ethers   

When one or more key arguments are provided, pass each key in succession 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 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.

gshadow  

When no key is provided, use  setsgent(3),  getsgent(3),  and  endsgent(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 endhostent(3) to enumerate  the hosts  database.   When  one  or  more  key  arguments are provided, pass each key to gethost- byaddr(3) or gethostbyname2(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 succession 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, username, 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 endnetent(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 endpwent(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 getprotobynumber(3) and each nonnumeric key to getprotobyname(3) and display the result.

rpc 

      When no key is provided, use setrpcent(3), getrpcent(3), and endrpcent(3) to enumerate the rpc database.  When one or more key arguments 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 endservent(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 endspent(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.



Return Code    

One of the following can be returned by getent command:

          0         when 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.

 
 

Category: Open System-Linux | Views: 1762 | Added by: shanky | Tags: getent command with examples, getent command in linux, getent command examples in linux, getent command in unix, getent | Rating: 5.0/1

Related blogs


You may also like to see:


[2014-10-26][Open System-Linux]
XMLLINT command in linux : a validating XML parser
[2015-06-19][Open System-Linux]
How to create swap file in Linux : Adding extra swap space
[2014-12-03][Open System-Linux]
GREP command in Linux to search for a pattern
[2015-07-18][Open System-Linux]
Creating and Managing Logical Volume Manager in Linux
[2014-10-17][Open System-Linux]
SETFACL command in Linux to set file access control list

Total comments: 0
avatar