Commands / Names

Names

getent hosts

Also /etc/resolv.conf, /etc/hosts, resolver configuration, nsswitch

The getent command resolves a name the way programs do, /etc/hosts first; /etc/resolv.conf says where names come from.

Programs such as curl and ssh do not use dig: they ask the system resolver, which reads /etc/hosts first and then the DNS servers in /etc/resolv.conf (nameserver lines, tried in order). getent hosts NAME shows exactly what a program would get. When curl and dig disagree, one of these two files is why.

getent hosts command usage

getent hosts NAME
cat /etc/resolv.conf
cat /etc/hosts

getent hosts examples

getent hosts webServer.lab
Resolves webServer.lab the way programs do, hosts file included.
cat /etc/resolv.conf
Shows which DNS servers this device asks, and its search domains.
cat /etc/hosts
Shows names pinned to addresses on this device, which win over DNS.
echo 'nameserver 10.0.1.2' > /etc/resolv.conf
Makes 10.0.1.2 this device's only DNS server.

Reading the output

$ cat /etc/resolv.conf; getent hosts webServer.lab
nameserver 10.0.1.2
10.0.2.10        webServer.lab  webServer.lab
  • nameserver 10.0.1.2: every lookup goes to this address first. If nothing answers DNS there, every name fails.
  • getent printed an address: programs can resolve it. Nothing printed (exit 2): they cannot.

Pitfalls

  • A stale line in /etc/hosts silently wins over a correct DNS answer.

Run it on real machines

Learn it on real machines.

Every lab is a small network of real Linux machines in your browser. Start with a free account: Google or email, no card.

Start this lab, free