Configuring Hostname and DNS
1. Viewing Hostname
| Command | Description | Example Output |
|---|---|---|
| hostname | Displays the system's hostname | myserver |
| hostname -f | Displays the fully qualified domain name (FQDN) | myserver.example.com |
| hostname -I | Displays all IP addresses associated with the host | 192.168.1.100 10.0.0.5 |
| cat /etc/hostname | Displays the hostname stored in the system configuration file | myserver |
| uname -n | Displays the network node hostname using the uname system call | myserver |
2. Setting Hostname
| Method | Description | Command/Config | Notes |
|---|---|---|---|
| /etc/hostname | Static hostname file (systemd-based systems) | localhost.localdomain |
Reboot required for changes to take effect |
| hostnamectl | Systemd utility to manage hostname | hostnamectl set-hostname "new-hostname" |
Applies immediately; persists across reboots |
| hostname | Legacy command-line utility | hostname new-hostname |
Does not persist across reboots unless using /etc/hostname |
Note: The
hostnamectl command is preferred on modern systemd-based systems as it handles both runtime and persistent changes.
Warning: Changing hostname via
/etc/hostname or hostnamectl does not update the /etc/hosts file automatically. Manual updates may be necessary for local resolution.
3. Configuring Static Hostname
| Method | Description | Command/Configuration | Notes |
|---|---|---|---|
| /etc/hostname | Static hostname file (traditional method) | localhost.localdomain |
Requires reboot or hostname command to apply changes |
| hostnamectl | Systemd-based hostname management | hostnamectl set-hostname "myhost.local" |
Applies immediately, persists across reboots |
| nmcli (NetworkManager) | Hostname via NetworkManager | nmcli general hostname myhost.local |
Requires NetworkManager service running |
Example: Setting Static Hostname Using hostnamectl
hostnamectl set-hostname "webserver.prod"
hostnamectl status
Note: The static hostname must be a valid DNS label and cannot contain spaces or special characters.
Warning: Changes made via
/etc/hostname may not take effect until the system is rebooted or the hostname command is executed manually.Summary of Hostname Configuration Methods
- /etc/hostname: Simple file-based method, requires manual reload
- hostnamectl: Modern systemd approach, persistent and immediate
- nmcli: NetworkManager integration for dynamic environments
4. Understanding hosts File
| Entry Format | Description | Example |
|---|---|---|
| IP Address | IPv4 or IPv6 address of the host | 127.0.0.1 |
| Hostname | Primary name for the IP address | localhost |
| Aliases | Alternative names for the same IP address | localhost.localdomain |
Example: hosts file entry
127.0.0.1 localhost
127.0.0.1 localhost.localdomain
::1 localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
Note: The
/etc/hosts file is read before DNS lookups and can override DNS resolution for specific hostnames.
Key Points
- The
/etc/hostsfile is a static lookup table for hostnames and IP addresses. - It is processed before DNS queries, allowing local overrides of domain names.
- Entries can include IPv4 and IPv6 addresses with aliases.
- Changes to
/etc/hoststake effect immediately without requiring a service restart.
5. Adding Local DNS Entries
| Entry Type | File Location | Example | Description |
|---|---|---|---|
| Local Hosts | /etc/hosts |
|
Maps hostnames to IP addresses for local resolution without DNS lookup |
| Custom Hosts | /etc/hosts |
|
Allows defining custom local domain names and their corresponding IPs |
Example: Adding a local DNS entry for a development server
# Edit the /etc/hosts file to add a local entry
sudo nano /etc/hosts
# Add the following line to map an IP to a local domain name
127.0.0.1 dev-server.localdomain
Note: Changes to
/etc/hosts take effect immediately without requiring a reboot or service restart.
Warning: Modifying
/etc/hosts can interfere with network services or cause unexpected behavior if incorrect entries are added.
6. Configuring DNS Resolvers
| Configuration File | Content | Description |
|---|---|---|
| /etc/resolv.conf | |
Primary DNS resolver configuration file |
Note: Changes to
/etc/resolv.conf may be overwritten by network managers like systemd-resolved.
Warning: Ensure DNS resolvers are accessible and do not block internal traffic.
| Resolver Type | Address | Notes |
|---|---|---|
| Google Public DNS | 8.8.8.8 |
Reliable and fast DNS service |
| Cloudflare DNS | 1.1.1.1 |
Fast and privacy-focused DNS |
| OpenDNS | 208.67.222.222 |
Includes security features |
7. Understanding NSSwitch
| Service | Source Order | Description |
|---|---|---|
| passwd | files dns |
Looks up user information in /etc/passwd first, then DNS (if configured) |
| group | files dns |
Looks up group information in /etc/group first, then DNS |
| hosts | files dns |
Resolves hostnames using /etc/hosts first, then DNS |
| services | files dns |
Maps service names to port numbers using /etc/services first, then DNS |
Example: NSSwitch Configuration
/etc/nsswitch.conf
passwd: files dns
group: files dns
hosts: files dns
services: files dns
Note: The order of sources in
/etc/nsswitch.conf determines the lookup priority. If a service is not found in the first source, it will attempt to find it in the next one.
8. Using systemd-resolved
| Configuration File | Path | Description |
|---|---|---|
| /etc/systemd/resolved.conf | /etc/systemd/resolved.conf |
Main configuration file for systemd-resolved |
Example: Basic systemd-resolved configuration
[Resolve]
DNS=8.8.8.8 8.8.4.4
FallbackDNS=1.1.1.1
Domains=~.
Note: The
Domains=~. directive ensures that all domains are resolved via the configured DNS servers.
| Option | Values | Description |
|---|---|---|
| DNS | 8.8.8.8 8.8.4.4 |
Primary DNS servers to use for resolution |
| FallbackDNS | 1.1.1.1 |
Fallback DNS servers if primary ones fail |
| Domains | ~. |
Domain suffixes to resolve using this resolver |
Workflow: Setting up systemd-resolved
- Ensure
systemd-resolvedservice is running:sudo systemctl status systemd-resolved - Edit
/etc/systemd/resolved.confto configure DNS servers and domains - Restart the service:
sudo systemctl restart systemd-resolved - Verify configuration:
systemd-resolve --status
9. Configuring Search Domains
| Configuration File | Content | Description |
|---|---|---|
| /etc/resolv.conf | |
Defines search domains and DNS servers for hostname resolution |
Note: Changes to
/etc/resolv.conf may be overwritten by network managers like NetworkManager or systemd-resolved.
Warning: Incorrectly configured search domains can lead to unexpected DNS resolution behavior.
10. Testing DNS Resolution
| Command | Description | Example |
|---|---|---|
| nslookup | Query DNS name servers for IP address | nslookup example.com |
| dig | Query DNS name servers with detailed output | dig example.com |
| host | Resolve domain names to IP addresses | host example.com |
| ping | Test network connectivity and DNS resolution | ping example.com |