> ## Documentation Index
> Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Demo Configuring Hostnames and Name Resolution

> How to view and set system hostnames and manage local name resolution using /etc/hostname, /etc/hosts, getent, and nsswitch.conf on Linux

In this lesson you will learn how to view and change the system hostname, manage local host-to-IP mappings, and verify name resolution on a Linux system. Correct hostname configuration and reliable name resolution are important because many services and networked applications depend on resolving hostnames correctly.

<Frame>
  <img src="https://mintcdn.com/kodekloud-c4ac6d9a/9E2T0ocsNwD7Od8i/images/Prep-Course-Red-Hat-Certified-System-Administrator-RHCSA/Managing-Network-Configuration-and-Security/Demo-Configuring-Hostnames-and-Name-Resolution/demo-slide-configuring-hostnames-name-resolution.jpg?fit=max&auto=format&n=9E2T0ocsNwD7Od8i&q=85&s=c40e00bcda73ad7b2b4a1539bc513ad3" alt="A dark presentation slide reading &#x22;Demo&#x22; with the subtitle &#x22;Configuring Hostnames and Name Resolution&#x22; on the left. The right side shows a large empty dark panel with a small &#x22;Copyright KodeKloud&#x22; in the top-right." width="1920" height="1080" data-path="images/Prep-Course-Red-Hat-Certified-System-Administrator-RHCSA/Managing-Network-Configuration-and-Security/Demo-Configuring-Hostnames-and-Name-Resolution/demo-slide-configuring-hostnames-name-resolution.jpg" />
</Frame>

## Displaying the current hostname

* Use the `hostname` command to print the current (runtime) hostname.
* The persistent hostname is stored in `/etc/hostname`.
* For detailed metadata and the canonical static/present hostname, use `hostnamectl` (systemd).

Example session:

```bash theme={null}
root@control:~# hostname
control

root@control:~# cat /etc/hostname
control

root@control:~# hostnamectl
   Static hostname: control
   Icon name: computer-vm
   Chassis: vm
   Machine ID: 500d9331afaf4b96957586d4650b73e2
   Boot ID: 42b37518fa5c422991641e371a935270
   Product UUID: 8daa7ceb-1348-044b-929d-623364a7716a
   Virtualization: oracle
   Operating System: Red Hat Enterprise Linux 10.1 (Coughlan)
   CPE OS Name: cpe:/o:redhat:enterprise_linux:10.1
   Kernel: Linux 6.12.0-124.8.1.el10_1.x86_64
   Architecture: x86-64
   Hardware Vendor: innotek GmbH
   Hardware Model: VirtualBox
   Hardware Serial: VirtualBox-eb7caa8d-4813-4b04-929d-623364a7716a
   Firmware Version: VirtualBox
   Firmware Date: Fri 2006-12-01
```

## Setting a new hostname

To change the persistent hostname (updates `/etc/hostname` automatically), use:

```bash theme={null}
hostnamectl set-hostname control.example.com
```

Verify the change:

```bash theme={null}
root@control:~# hostname
control.example.com

root@control:~# cat /etc/hostname
control.example.com
```

Note: `hostnamectl` updates the static hostname and is the recommended method on systemd-based distributions.

## Local name resolution — /etc/hosts

* The file `/etc/hosts` contains local host-to-IP mappings. This file is consulted before DNS when `files` appears before `dns` in `/etc/nsswitch.conf`.
* Each line should contain an IP address, then one or more hostnames separated by whitespace.

Example (with a commented correction and a real mapping):

```text theme={null}
# Loopback entries; do not change.
# For historical reasons, localhost precedes localhost.localdomain:
127.0.0.1  localhost localhost.localdomain localhost4 localhost4.localdomain4
::1        localhost localhost.localdomain localhost6 localhost6.localdomain6
# See hosts(5) for proper format and other examples: https://man7.org/linux/man-pages/man5/hosts.5.html
# 192.168.1.10 foo.example.org foo
# 192.168.1.13 bar.example.org bar
10.0.2.6   node1
```

If you update a mapping (for example change `node1` to `node5` at a new IP), save `/etc/hosts`. The system will then resolve that hostname using the updated entry.

## Testing resolution with ping

After editing `/etc/hosts`, use `ping` to confirm the hostname resolves to the expected IP:

```bash theme={null}
root@control:~# ping node5
PING node5 (10.0.2.15) 56(84) bytes of data.
64 bytes from node5 (10.0.2.15): icmp_seq=1 ttl=64 time=1.25 ms
64 bytes from node5 (10.0.2.15): icmp_seq=2 ttl=64 time=0.647 ms
64 bytes from node5 (10.0.2.15): icmp_seq=3 ttl=64 time=0.640 ms
64 bytes from node5 (10.0.2.15): icmp_seq=4 ttl=64 time=0.465 ms
^C
--- node5 ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3027ms
rtt min/avg/max/mdev = 0.465/0.749/1.246/0.295 ms
```

Tip: You can map arbitrary hostnames (e.g., `google.com`) to a chosen IP in `/etc/hosts` for testing, but avoid doing this for public hostnames in production.

## Use getent to test resolver lookups

`getent` queries the system name service switch and respects the lookup order defined in `/etc/nsswitch.conf`. Use it to verify how the system resolves names without sending ICMP traffic:

```bash theme={null}
root@control:~# getent hosts node5
10.0.2.15        node5
```

This shows the resolver result as the system sees it, following `nsswitch.conf` rules.

## Name resolution order — /etc/nsswitch.conf

The file `/etc/nsswitch.conf` defines the order the system uses to resolve different databases, including hosts. A typical hosts line is:

```text theme={null}
hosts: files dns myhostname
```

Meaning:

1. `files` — check `/etc/hosts`
2. `dns` — query DNS servers
3. `myhostname` — fallback to local machine name methods

Example `/etc/nsswitch.conf` excerpt (with authselect comments corrected):

```text theme={null}
# Generated by authselect
# Do not modify this file manually, use authselect instead. Any user changes will be overwritten.
# You can stop authselect from managing your configuration by calling `authselect opt-out`.
# In order of likelihood of use to accelerate lookup.
passwd:         files systemd
shadow:         files
group:          files [SUCCESS=merge] systemd
hosts:          files dns myhostname
services:       files
netgroup:       files
automount:      files

aliases:        files
ethers:         files
gshadow:        files
networks:       files dns
protocols:      files
publickey:      files
rpc:            files
```

## Quick reference

| Item | Purpose | Example |
| - | - | - |
| View current hostname | Show runtime hostname | `hostname` |
| Persistent hostname file | Where static hostname is stored | `/etc/hostname` |
| Set persistent hostname | Change system hostname (systemd) | `hostnamectl set-hostname <name>` |
| Local overrides | Local host-to-IP mappings | `/etc/hosts` |
| Resolver test (no ICMP) | Check system resolution order | `getent hosts <name>` |
| Resolution order config | Define lookup order (files, dns, etc.) | `/etc/nsswitch.conf` (`hosts: files dns myhostname`) |

<Callout icon="lightbulb" color="#1CB2FE">
  Entries in `/etc/hosts` take precedence over DNS when `files` appears before `dns` in `/etc/nsswitch.conf`. Use `/etc/hosts` for local overrides and testing, but avoid modifying it for public hostnames unless you understand the implications.
</Callout>

## Links and references

* `hostnamectl` (systemd): [https://www.freedesktop.org/software/systemd/man/hostnamectl.html](https://www.freedesktop.org/software/systemd/man/hostnamectl.html)
* `getent` manual: [https://man7.org/linux/man-pages/man1/getent.1.html](https://man7.org/linux/man-pages/man1/getent.1.html)
* `nsswitch.conf` manual: [https://man7.org/linux/man-pages/man5/nsswitch.conf.5.html](https://man7.org/linux/man-pages/man5/nsswitch.conf.5.html)
* `hosts` file format: [https://man7.org/linux/man-pages/man5/hosts.5.html](https://man7.org/linux/man-pages/man5/hosts.5.html)
* `authselect` (RHEL): [https://man7.org/linux/man-pages/man8/authselect.8.html](https://man7.org/linux/man-pages/man8/authselect.8.html)

This concludes the demo on configuring hostnames and managing local name resolution.

<CardGroup>
  <Card title="Watch Video" icon="video" cta="Learn more" href="https://learn.kodekloud.com/user/courses/red-hat-certified-system-administrator-rhcsa/module/6bd67b03-1052-4557-8920-4b79d1b17abf/lesson/c0c720da-9304-46c0-9a41-ba56de59d43e" />
</CardGroup>


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.