Ask Different is a question and answer site for power users of Apple hardware and software. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have several scripts which require the utility getent. In addition, I've read several handy tips which use getent.

getent is a Networking utility which is widely available on Unix, *BSD and Linux-based systems. This tool is commonly used by Sysadmins and power users to lookup network information (IP addresses, etc). As far as hostnames go, getent is one of the few tools which use both /etc/hosts and DNS.

As Wikipedia says:

getent is a unix command that helps a user get entries in a number of important text files called databases.

The important thing here is that getent will search the same name services in the same order as would most applications on a unix-like system. This makes it invaluable when debugging system or application trouble. Tools like dig and nslookup specifically look at DNS only, while dscl expects you to spell out the name services you want to search.

I would like to use getent for two testcases:

  1. Parse /etc/hosts before querying DNS. For example, if I add to /etc/hosts, then getent hosts should use the entry from /etc/hosts before querying DNS.

  2. Query LDAP using getent

MacPorts does not seem to provide a package providing getent.

Does getent exist on Snow Leopard?

share|improve this question
What is getent? What does it do? What systems is it typically available on? – CajunLuke Mar 20 '12 at 0:20
There's no 1:1 mapping for getent on OS X because OS X uses DirectoryService to do resolution on things like hostnames. Can you give some specific getent use examples? It'd make showing you the corresponding way to get that information on OS X easier. – Ian C. Mar 20 '12 at 0:41
@CajunLuke : getent is typically available on Unix, *BSD and Linux-like systems. I updated my question. – Stefan Lasiewski Mar 20 '12 at 0:47
So what is it you're trying to do with getent? Resolve host names from /etc/hosts before using DNS? – Ian C. Mar 20 '12 at 3:23
My most common use case is to resolve hostnames from /etc/hosts before DNS, which is useful for Webserver and other network tests. But I (and other sysadmins) use getent for other things as well, such as troubleshooting LDAP. – Stefan Lasiewski Mar 20 '12 at 18:34
up vote 1 down vote accepted

dscacheutil is probably the best way to do this. It doesn't emulate the usual search process, it uses it, so you'll actually get the same results as a standard lookup (including caching, /etc/hosts, computer records in directory services, DNS, mDNS for .local names, etc).

$ dscacheutil -q host -a name

$ dscacheutil -q host -a name gordons-mbp.local
name: gordons-mbp.local
ipv6_address: fe80:5::e6ce:8fff:fe50:136

name: gordons-mbp.local

$ dscacheutil -q host -a ip_address
name: localhost

$ dscacheutil -q user -a name gordon
name: gordon
password: ********
uid: 501
gid: 20
dir: /Users/gordon
shell: /bin/bash
gecos: Gordon Davisson
share|improve this answer

Have a look at How to emulate Unix’ getent with MacOSX’s dscl. Since you have scripts that make use of getent, you may wish to pursue a script as a wrapper around dscl.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.