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
Description | Getent (Get Entries From The Administrative Database) |
Difficulty Level | Low |
Root or Sudo Privileges | No |
OS Compatibility | Ubuntu, Manjaro, Fedora, etc. |
Prerequisites | getent |
Internet Required | No |
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:
To get information about a specific user account, use the user’s username or UID as the database key.
$ getent passwd linuxtldr
Output:
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:
To get the information for a specific group, assign its name or GID as a database key.
$ getent group sudo
Output:
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:
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:
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:
To get the information for a specific service, specify either its name or port number as the database key.
$ getent services 80
Output:
Displaying the Network’s Information
The following command will print your current network and IP address from the “/etc/networks
” file.
$ getent networks
Output:
Displaying the Protocols
The following command will print the protocol information for your Linux system from the “/etc/protocols
” file.
$ getent protocols
Output:
Specify the specific protocol name as a database key to get the result.
$ getent protocols ip
Output:
Getent Help
You can get basic information regarding the flags and supported database using the “--help
” flag.
$ getent --help
Output:
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.