Beginners Guide for Getent Command on Linux

Linux TLDR
Last Updated:
Reading time: 2 minutes

The getent command is used to fetch entries from the administrative text files, also known as databases.

The supported databases are ahosts, ahostsv4, ahostsv6, aliases, ethers, group, gshadow, hosts, initgroups, netgroup, networks, passwd, protocols, rpc, services, and shadow.

In this article, you will learn how to read the above databases using the getent command (with practical examples).

Tutorial Details

DescriptionGetent (Get Entries From The Administrative Database)
Difficulty LevelLow
Root or Sudo PrivilegesNo
OS CompatibilityUbuntu, Manjaro, Fedora, etc.
Prerequisitesgetent
Internet RequiredNo

Usually, you might only be interested in the following databases:

  • passwd is used to get user information like user ID, group ID, home directory, etc.
  • group will list all the existing groups on your Linux system.
  • hosts will give you the information of your current host from the /etc/hosts file.
  • services will give you information about all the services configured on your system.
  • networks will show the configured networks on your system.
  • protocols will enumerate the protocol database.

Syntax of the Getent Command

The getent command requires three arguments: one is the option (optional); the other is the database name; and the third is the database key.

$ getent [OPTION] [DATABASE_NAME] [DATABASE_KEY]

Listing All or Specific User Information from the Passwd Database

The following command will pull all the user’s account information like their login name, UID, GID, home directory, default login shell, etc. from the “/etc/passwd” file.

$ getent passwd

Output:

Listing all the user's information from the passwd file

To get information about a specific user account, use the user’s username or UID as the database key.

$ getent passwd linuxtldr

Output:

Fetching specific user account information

Listing All or Specific Group Information from the Group Database

The following command will list all the groups as well as their GID in your system by pulling the information from the “/etc/group” file.

$ getent group

Output:

Listing all the groups

To get the information for a specific group, assign its name or GID as a database key.

$ getent group sudo

Output:

Fetching specific group information

Displaying the Hosts Information

The following command will give you the existing hosts information in your system from the “/etc/hosts” file.

$ getent hosts

Output:

Listing all the hosts

The hostname for my system is “linux“.

If the name is specified as a database key, then it will print only the information related to the specified hosts.

$ getent hosts linux

Output:

Fetching specific host information

Listing All or Specific Services Information from the Services Database

The following command will list all the services in your system with their port number and protocol from the “/etc/services” file

$ getent services

Output:

Listing all the system services

To get the information for a specific service, specify either its name or port number as the database key.

$ getent services 80

Output:

Fetching specific system service information

Displaying the Network’s Information

The following command will print your current network and IP address from the “/etc/networks” file.

$ getent networks

Output:

Display the current network information

Displaying the Protocols

The following command will print the protocol information for your Linux system from the “/etc/protocols” file.

$ getent protocols

Output:

Listing all the protocol information

Specify the specific protocol name as a database key to get the result.

$ getent protocols ip

Output:

Fetching specific protocol information

Getent Help

You can get basic information regarding the flags and supported database using the “--help” flag.

$ getent --help

Output:

Getent help section

But I strongly recommend that you read the manual page using the “man getent” command.

It’s time to go meet you in the next article. Bye bye.

Join The Conversation

Users are always welcome to leave comments about the articles, whether they are questions, comments, constructive criticism, old information, or notices of typos. Please keep in mind that all comments are moderated according to our comment policy.