TCP/IP Configuration  «Prev  Next»

Lesson 4The name service methods
ObjectiveHosts Files and DNS for Name Service

Hosts Files and DNS for Name Service

As a Linux network administrator, utilizing hosts files and DNS (Domain Name System) for name service involves two distinct yet complementary approaches to resolve hostnames to IP addresses. These methods serve as the backbone for network communication, ensuring that devices can locate and communicate with each other efficiently.

Hosts File

The hosts file in Linux, typically located at `/etc/hosts`, is a simple text file that maps IP addresses to hostnames. This file allows for the static resolution of hostnames, bypassing the need for DNS queries for entries contained within it. The format of the file is straightforward, with each line specifying an IP address followed by the associated hostname(s).
To use the hosts file for name resolution:
  1. Edit the Hosts File: Open the `/etc/hosts` file with a text editor such as `vim`, `nano`, or `gedit`. Administrative privileges are required to edit this file, so you may need to precede your editor command with `sudo`.
  2. Add Entries: For each hostname and IP address mapping you wish to define, add a new line to the file. The general syntax is:
    <IP address> <hostname> [<alias>...]
    

    For example, to map the IP address `192.168.1.10` to the hostname `server.example.com`, you would add:
    192.168.1.10 server.example.com
    
  3. Save and Exit: After adding all necessary mappings, save your changes and exit the editor. The effect is immediate, and no service restarts are required for the changes to take effect.

The hosts file is particularly useful for small networks, testing, or overriding DNS for specific hosts. However, it does not scale well for large networks due to the manual maintenance required.

DNS

DNS provides dynamic hostname to IP resolution, allowing devices to query a distributed database to obtain the IP address associated with a hostname. As a network administrator, you would typically configure your network to use one or more DNS servers for name resolution.
To configure DNS in Linux:
  1. Edit resolv.conf: The primary configuration for DNS clients is the `/etc/resolv.conf` file. This file specifies the DNS servers the system should query for name resolution.
  2. Specify DNS Servers: Within `resolv.conf`, use the `nameserver` keyword to define DNS servers. You can specify multiple DNS servers for redundancy. For example:
    nameserver 8.8.8.8
    nameserver 8.8.4.4
    

    These entries tell the system to use Google's public DNS servers.
  3. Configure Search Domains (Optional): You can also specify search domains in `resolv.conf` using the `search` keyword. This is useful for resolving short hostnames within a specific domain without needing to type the full domain name.
    search example.com
    
  4. Apply Changes: Changes to `resolv.conf` may require you to restart the networking service or the entire system, depending on your Linux distribution and networking configuration.

In summary, combining the hosts file for static mappings and DNS for dynamic resolution provides a robust framework for name service in Linux. The hosts file offers a quick and straightforward method for local overrides or small networks, while DNS provides scalable and flexible name resolution across larger networks and the internet.


The first and simplest of the name service methods is to save a list of IP addresses and host names in a text file. This method is particularly useful because it is available before any networking configuration has been carried out. On UNIX machines, the /etc/hosts file is a text file that lists IP addresses in one column and host names in another.
Here is a typical example:
127.0.0.1 localhost localhost.localdomain
131.145.185.198 raphael.acme.com raphael
131.193.178.203 splinter.acme.com splinter

IP Address in file

Each line of this file specifies an IP address, followed by one or more names.
The additional names are aliases, so that, for example, in the above file the names raphael and raphael.acme.com may both be used for the machine with address 131.145.185.198. Some systems use the hosts file for other parts of TCP/IP configuration. Therefore, your hosts file should always contain the IP address and name of your own machine.

Ad TCP/IP Illustration

Becoming DNS client and resolv.conf

The second major source of name service is the Internet-wide Domain Name Service.
We will discuss DNS in detail later. For now, suffice it to say that to obtain DNS name service (in other words, to become a DNS client), you must know the IP address of at least one DNS name server. You must also know your own DNS domain name. The basic configuration information for the DNS part of the resolver library belongs in the /etc/resolv.conf configuration file.
This very simple file need only contain the following information:
domain [your DNS domain]
nameserver IP1
nameserver IP2
nameserver IP3

where IP1, IP2, and IP3 are the IP addresses of DNS servers. You need only supply one nameserver line, but you may supply up to three. The additional servers are used as backups.

Name Service Method - Quiz

Click the Quiz link below to take a short multiple-choice quiz on name service methods.
Name Service Method - Quiz

SEMrush Software