Configuring hostnames and DNS


By the end of this article you should be able to answer the following questions:


You can find all my latest posts on medium.

What is the command to display just your machine's hostname?

$ hostname -s

What is the command to display your machine's fqdn?

$ hostname -f

What is the command to just display your machine's ip address?

$ hostname -I

What is the command to display your machine's hostname along with other related info?

$ hostnamectl status

What is the file you need to edit to change your machine's hostname?


Assuming you don't want to edit this file directly, then what is the command to (persistantly) change your machine's hostname to puppet01.local?

$ hostnamectl set-hostname puppet01.local

What file is used to lookup dns locally?



All machines in a network have two ways to identify itself to the other machines on the network, they are the machine’s ip address and it’s name (aka hostname). The hostname is your machine’s name. You can view this name, using the hostname command:

$ hostname -s

This is machine’s short name. It’s full name is:

$ hostname -f

This full name is more commonly known as the machine’s Fully Qualified Domain Name, (aka FQDN).

To view a machine’s ip address we can do:

$  hostname -I

Another way to view your machine’s hostname is by using the hostnamectl command:

$ hostnamectl status
   Static hostname: puppetmaster.local
         Icon name: computer-vm
           Chassis: vm
        Machine ID: bb2262658ee64941afea091071b78f45
           Boot ID: c59311743d9940b1981d065179eeccf3
    Virtualization: oracle
  Operating System: CentOS Linux 7 (Core)
       CPE OS Name: cpe:/o:centos:centos:7
            Kernel: Linux 3.10.0-229.14.1.el7.x86_64
      Architecture: x86_64

You can change your machine’s hostname by editing the following file:

$ cat /etc/hostname

Or you can use the hostnamectl command:

$ hostnamectl set-hostname puppetmaster.local.test
$ cat /etc/hostname

As you can see the hostnamectl command just edits the same file behind the scene.

Domain Name Servers (DNS)

Hostnames are human friendly names for a machine. However machines can’t identify themselves on a network using hostnames. Instead they have to identify themselves using an IP number. Machines use IP numbers pretty much the same way that phones connect to each other through phone numbers.

If we try to connect to a machine, e.g. via ssh, then we normally use it’s fqdn:

ssh root@puppetagent01.local

That’s because ip numbers are too difficult to remember. However before a connection to puppetagent01.local can be established, the fqdn will first need to be resolved to an ip number. This resolution can be done locally on your machine by adding an entry to the /etc/hosts file:

$ cat /etc/hosts localhost puppetmaster.local puppetmaster puppetmaster puppetmaster.local puppetagent01 puppetagent01.local puppetagent02 puppetagent02.local

If your machine can’t find a matching entry here, then your machine will contact the dns server to resolve it instead.

DNS Server

A dns server is a server that receives requests for an ip address for a given a fqdn. The dns server then sends back the ip number.

The dns server’s details is specified in the /etc/resolv.conf file:

$ cat /etc/resolv.conf
# Generated by NetworkManager
search local

This file is actually not used anymore and is only used internally be the NetworkManager. So please don’t make any changes to this file, otherwise it will get overwritten by NetworkManager service.

This file is actually combined list of all the dns entries specified in the individual ifcfg-* config files. We will cover these files later.

Finally you can query a dns server using nslookup command.

$ nslookup

Non-authoritative answer:

Also, here is an interesting setting:

$ cat /etc/nsswitch.conf | grep "^hosts:"
hosts:      files dns myhostname

This shows the order in which dns lookup occurs, first “files” which refers to /etc/hosts, then it is the dns server.