getent command in Linux with examples
The ‘getent’ command in Linux is a powerful tool that allows users to access entries from various important text files or databases managed by the Name Service Switch (NSS) library. This command is widely used for retrieving user and group information, among other data, stored in databases such as ‘passwd’, ‘group’, ‘hosts’, and more. ‘getent’ provides a consistent and unified way to query the local files like ‘/etc/passwd‘ or network information sources such as LDAP.
What is the ‘getent’ Command?
The ‘getent’ (short for “get entries”) command fetches entries from specified databases supported by NSS. This makes it a versatile tool for looking up a wide range of information on a Linux system, including user accounts, groups, hosts, services, and more. Since it uses the same name service as the system, ‘getent’ can retrieve data from both local files and network sources like LDAP, providing a complete view of the requested information.
Common Databases Queried by ‘getent’
The ‘getent’ command can query several databases, each serving a different purpose. Some of the most commonly accessed databases include:
- ‘passwd’: Retrieves user account information.
- ‘group‘: Fetches group account details.
- ‘hosts’: Looks up hostnames and IP addresses.
- ‘services’: Displays network services and their associated ports.
- ‘protocols’: Lists network protocols.
- ‘networks’: Retrieves network names.
- ‘shadow’: Shows user password information (requires proper permissions).
- ‘aliases’: Provides mail alias information.
Other databases ‘getent’ can query include ‘ahosts’, ‘ahostsv4′, ‘ahostsv6′ (for address resolution), ‘ethers’ (Ethernet addresses), ‘gshadow’ (secure group information), ‘netgroup’, ‘rpc’ (remote procedure call), and more.
‘getent’ Command Examples in Linux
Here are some practical examples:
Example 1: Fetching All User Accounts
Fetch the list of user accounts on a Linux system (stored in a database known as ‘passwd‘). This will show all the user accounts, regardless of the type of name service being used. For example, if both the local and the LDAP name service are used for user accounts, the results will include all the local and the LDAP users:
Syntax:
getent database [key ...]
Output:
Explanation: This command displays all user accounts, including those from both local and network sources like LDAP.
Example 2: Fetching Specific User Information
If we want to fetch details for a particular user called ‘rahul’ then,
Syntax:
getent passwd rahul
Output:
Explanation: This outputs the user details from the ‘passwd’ database, including the username, user ID, group ID, home directory, and default shell.
Example 3: Fetching Group Information
If we want to fetch a list of group accounts on a Unix system (stored in a database called ‘group’) then,
Syntax:
getent group
Output:
Explanation: This shows details of each group, including group name, group ID, and group members.
Options for ‘getent’
While ‘getent’ is simple in its basic usage, it also offers options to modify its behavior:
'
-s service, --service service
'
: This option overrides all the databases with the specified service.(Since glibc 2.2.5.)'
-s database:service, --service database:service
'
: This option override only the specified databases with the specified service. The option may be used for multiple times, but only the last service for each of the database will be used.(Since glibc 2.4.)'
-i, --no-idn
'
: This option disables IDN encoding in the lookups for ahosts/getaddrinfo(3) (Since glibc-2.13.)'
-?, --help
'
: This option prints a usage summary and exit.'
--usage
'
: This option prints a short usage summary and exit.'
-V, --version
'
: This option prints the version number, license, and the disclaimer of warranty for ‘getent’.
Exit Status Codes
One of the following exit values can be used to returned by getent:
0
: This exit status shows that the Command completed successfully.1
: This exit status shows that there’s a Missing arguments, or database unknown.2
: This exit status shows that One or more supplied key could not be found in the database.3
: This exit status shows that the Enumeration not supported on this database.
getent command in Linux with examples – FAQs
What is the getent command used for in Linux?
The
getent
(get entries) command in Linux is used to fetch entries from databases supported by the Name Service Switch (NSS) library, which configures the behavior of system calls such asgetpwnam
(get password name) andgetgrnam
(get group name). The databases it can query includepasswd
(for user accounts),groups
(for group accounts),hosts
(for hostnames),services
(for service entries), and others. It’s especially useful for accessing information in a consistent way regardless of the underlying storage mechanism, whether it’s files like/etc/passwd
or network sources like LDAP.
How to use getent to retrieve user information?
To retrieve user information using
getent
, you use thepasswd
database followed by the username. For example, to fetch information about a user named “john,” you would use:getent group admin
This command will output a line from the
passwd
database that corresponds to “john,” typically including the user’s ID, group ID, home directory, and shell.
How to use getent to check group information?
To check group information using
getent
, refer to thegroup
database. For example, to get information about a group named “admin,” you would use:getent group admin
This will output a line from the
group
database, showing the group name, password (if any), group ID, and list of members.
What are some common options for the getent command?
getent
itself does not have many options other than specifying the database and the key(s) for the lookup. The command’s usefulness lies in its simplicity and its ability to interface with different backends transparently through the NSS configuration. However, the output and utility ofgetent
can be extended by combining it with other Unix tools likegrep
,awk
, orcut
for filtering and formatting the output as needed.For example, to find all users with a home directory in
/home
, you might use:getent passwd | grep /home
Or to count the number of users in the system:
getent passwd | wc -l