Configure Hostname Resolution

LinuxLinuxBeginner
Practice Now

Introduction

In this challenge, you will learn how to configure hostname resolution on a Linux system. Proper hostname resolution is essential for network communication and system administration tasks. You will practice setting up static hostname resolution using the /etc/hosts file, as well as dynamic hostname resolution using a DNS server.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/BasicSystemCommandsGroup(["`Basic System Commands`"]) linux/BasicSystemCommandsGroup -.-> linux/echo("`Text Display`") subgraph Lab Skills linux/echo -.-> lab-389436{{"`Configure Hostname Resolution`"}} end

Configure Static Hostname Resolution

Tasks

  • Configure static hostname resolution using the /etc/hosts file.
  • Ensure that the hostname resolution works correctly for both the local system and remote systems.

Requirements

  • The hostname of the local system should be set to myhost.example.com.
  • Add an entry in the /etc/hosts file to map the IP address 192.168.1.100 to the hostname myhost.example.com.
  • Verify that the hostname resolution works correctly by using the ping command to ping the hostname myhost.example.com.

Example

After configuring the /etc/hosts file, the output of the ping command should be similar to the following:

$ ping myhost.example.com
PING myhost.example.com (192.168.1.100) 56(84) bytes of data.
64 bytes from myhost.example.com (192.168.1.100): icmp_seq=1 ttl=64 time=0.028 ms
64 bytes from myhost.example.com (192.168.1.100): icmp_seq=2 ttl=64 time=0.030 ms

Configure Dynamic Hostname Resolution using a DNS Server

Tasks

  • Configure dynamic hostname resolution using a DNS server.
  • Ensure that the hostname resolution works correctly for both the local system and remote systems.

Requirements

  • Install and configure a DNS server (e.g., BIND) on the local system.
  • Configure the DNS server to resolve the hostname myhost.example.com to the IP address 192.168.1.100.
  • Configure the local system to use the DNS server for hostname resolution.
  • Verify that the hostname resolution works correctly by using the ping command to ping the hostname myhost.example.com.

Example

After configuring the DNS server and the local system, the output of the ping command should be similar to the following:

$ ping myhost.example.com
PING myhost.example.com (192.168.1.100) 56(84) bytes of data.
64 bytes from myhost.example.com (192.168.1.100): icmp_seq=1 ttl=64 time=0.028 ms
64 bytes from myhost.example.com (192.168.1.100): icmp_seq=2 ttl=64 time=0.030 ms

Summary

In this challenge, you learned how to configure both static and dynamic hostname resolution on a Linux system. You practiced setting up the /etc/hosts file for static hostname resolution and configuring a DNS server for dynamic hostname resolution. By the end of this challenge, you should have a better understanding of how to manage basic networking and hostname resolution on a Linux system.

If you need to initialize the challenge environment, you can use the following setup.sh script:

#!/bin/bash

## Set the hostname
hostnamectl set-hostname myhost.example.com

## Create the /etc/hosts file
cat << EOF | sudo tee /etc/hosts
127.0.0.1   localhost
192.168.1.100   myhost.example.com
EOF

## Install and configure the DNS server (e.g., BIND)
sudo dnf install -y bind bind-utils
sudo systemctl start named
sudo systemctl enable named

## Configure the DNS server
sudo tee /etc/named.conf << EOF
options {
    listen-on port 53 { 127.0.0.1; };
    directory       "/var/named";
    dump-file       "/var/named/data/cache_dump.db";
    statistics-file "/var/named/data/named_stats.txt";
    memstatistics-file "/var/named/data/named_mem_stats.txt";
    recursing-file  "/var/named/data/named.recursing";
    secroots-file   "/var/named/data/named.secroots";
    allow-query     { localhost; };
};

zone "example.com" IN {
    type master;
    file "example.com.zone";
    allow-update { none; };
};
EOF

sudo tee /var/named/example.com.zone << EOF
$TTL 86400
@   IN  SOA     myhost.example.com. root.example.com. (
                2023040601 ; serial
                3600       ; refresh (1 hour)
                1800       ; retry (30 minutes)
                604800     ; expire (1 week)
                86400      ; minimum (1 day)
                )
        IN  NS      myhost.example.com.
myhost  IN  A       192.168.1.100
EOF

sudo systemctl restart named

Other Linux Tutorials you may like