getent command in Linux with examples
Last Updated :
03 Sep, 2024
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.
Similar Reads
How to Find a File in Linux | Find Command
Linux, renowned for its robust command-line interface, provides a suite of powerful tools for efficient file and directory management. Among these, the "find" command stands out as an indispensable asset, offering unparalleled versatility in searching for files based on diverse criteria. This articl
9 min read
Finger command in Linux with Examples
The 'finger' command is a powerful utility in Linux used to display information about users logged into the system. This command is commonly used by system administrators to retrieve detailed user information, including login name, full name, idle time, login time, and sometimes the user's email add
4 min read
fmt command in Linux with examples
fmt command in LINUX actually works as a formatter for simplifying and optimizing text files. Formatting of text files can also be done manually, but it can be really time-consuming when it comes to large text files, this is where fmt comes to rescue. fmt re-formats each paragraph in the file specif
4 min read
fold command in Linux with examples
The fold command in Linux wraps each line in an input file to fit a specified width and prints it to the standard output. By default, it wraps lines at a maximum width of 80 columns, which is configurable. To fold input using the fold command pass a file or standard input to the command. Syntax of `
3 min read
for command in Linux with Examples
IntroductionThe for command in linux used in shell scripting to iterate over a set of values or perform set of tasks repeatedly. The for loop allows users to automate operations efficiently which is easier to maintain. Syntax: for NAME [in WORDS ... ] ; do COMMANDS; doneHere NAME takes the value of
2 min read
free Command in Linux with examples
While using LINUX there might come a situation when you are willing to install a new application (big in size) and you wish to know the amount of free memory available on your system. In LINUX, there exists a command line utility for this and that is the 'free' command which displays the total amoun
6 min read
Fun Commands in Linux
Linux isn't just for coding and administrationâit can also be a lot of fun. With a variety of terminal commands, you can add some entertainment to your Linux experience. Below is a list of some cool and fun commands you can use in Linux to enhance your terminal experience. Weâll also cover how to in
4 min read
function command in Linux with examples
The function is a command in Linux that is used to create functions or methods. It is used to perform a specific task or a set of instructions. It allows users to create shortcuts for lengthy tasks making the command-line experience more efficient and convenient. The function can be created in the u
2 min read
Compiling with g++
g++ command is a GNU c++ compiler invocation command, which is used for preprocessing, compilation, assembly and linking of source code to generate an executable file. The different "options" of g++ command allow us to stop this process at the intermediate stage.  Check g++ compiler version informa
3 min read
gawk command in Linux with Examples
The gawk command in Linux is a pattern scanning and processing language. No compilation is required, and variables can be used along with numeric functions, string functions, and logical operators. Gawk is a utility that enables programmers to write highly compact but still effective programs as sta
3 min read