How to Run the getent Command in Linux?

The getent command is used to find the entries from the Name Service Switch libraries located at /etc/nsswitch.conf. These entries are fetched from the text files known as the databases. The information on the passwords, services, protocols, groups, and networks can be found using the getent command.

This post will demonstrate the working and usage of the “getent” command with the following outline: 

How Does the getent Command Work in Linux?

The general syntax of using the getent command in Linux:

$ getent [Options] [Database] [Key]

The explanation of the syntax is:

  • Use the getent to invoke the command
  • Use different options of the getent command
  • Specify the database from where you want to search the keys
  • Specify the keys which you want to find from the specified database

The options which can be used with the getent command are as follows: 

$ getent --help

How to Use the getent Command in Linux?

We utilize various examples to run and understand the getent command in Linux.

Example 1: Find the User Information

To find the user account information from the passwd database, use the getent command. For example, we will display the information of the “milton” user:

$ sudo getent passwd milton

The above output displays the username, user group, and user’s folder path. 

Example 2: Find the Service Associated With the Specified Port

To find the service associated with the specific port, we can utilize the getent command. For example, we will find the services associated with port 22:

$ getent services 22

The ssh service uses port 22. 

Example 3: Perform the DNS Service to Display the IP Address

We can also find the IP Address associated with the hostname by using the getent command. For example, we will pass the “hosts” parameter with the machine name “foss” to find out information about our host:

$ getent hosts foss

The IP address of the machine has been displayed. 

Example 4: Find the Information of the Networks

To find out the information of the networks, we can utilize the getent script. It displays the information for each network specified in the database, including the network name and the network address:

$ getent networks

The network IP address “169.254.0.0” is displayed on the “link-local”. 

Example 5: Find the Information of the Groups

To find the information of the groups using the getent command, use the command. The command displays information for each group on the system, including the group name, the group password, and a list of group members:

$ getent group

The groups have been displayed with the group name, the group password, and a list of group members. 

Conclusion

To run the getent command in Linux, follow the general syntax “getent [Options] [Database] [Key]”. The getent command finds the information from the computer’s databases. The information includes passwords, services, protocols, groups, and networks. This blog has briefly explained the “getent” command and examples in Linux.