LFCS 101 🐧 Hostname Configuration
A hostname is the label that identifies a machine on a network. In systemd-based Linux distributions, the concept is split into three distinct values: the static hostname (the persistent name stored in /etc/hostname), the transient hostname (a runtime value that can be set by DHCP or mDNS), and the pretty hostname (a free-form display name stored in /etc/machine-info) . The LFCS exam expects you to configure the static hostname correctly, verify it with the appropriate tools, and ensure that local name resolution in /etc/hosts reflects the change.
Key point: hostnamectl set-hostname is the modern command. It updates /etc/hostname automatically and sets both the static and transient hostnames in one operation. The legacy hostname command only changes the transient value and does not survive a reboot .
Why hostname configuration matters
The identity problem. Many services—SSH, mail servers, logging daemons, and monitoring agents—use the hostname as part of their identity. A hostname that does not match the system’s actual identity causes confusing logs, failed service checks, and certificate mismatches. The static hostname is the authoritative name that persists across reboots.
The three-hostname problem. systemd-hostnamed distinguishes between static, transient, and pretty hostnames. The static hostname is what you configure and what is written to /etc/hostname. The transient hostname is a fallback value that can be set by network configuration (DHCP, mDNS) and is overridden by the static hostname if one exists. The pretty hostname is a human-readable label like “Lennart’s Laptop” that has no restrictions on characters . For exam purposes, the static hostname is what matters.
The resolution problem. Changing the hostname does not automatically update /etc/hosts. If the system relies on /etc/hosts for local name resolution (because files precedes dns in /etc/nsswitch.conf), a stale entry will cause ping, getent hosts, and other tools to resolve the old name or fail to resolve the new one . The exam tests the complete workflow: set the hostname, update /etc/hosts, and verify the change.
The validation problem. Static and transient hostnames are limited to 64 characters and must follow DNS label rules: lowercase ASCII letters, digits, and hyphens. They cannot start or end with a hyphen. The pretty hostname has no such restrictions, but when it is set alongside a static hostname, systemd simplifies it by removing special characters and spaces .
The trade-off. hostnamectl requires systemd. On non-systemd systems, editing /etc/hostname directly is the only option. The exam assumes systemd-based distributions, where hostnamectl is the correct tool.
a. Viewing and setting the hostname
The hostnamectl command without arguments displays all hostname-related information: the static hostname, the transient hostname, the pretty hostname, the machine ID, and the operating system. The status subcommand produces the same output explicitly .
# Display all hostname information
hostnamectl
# Display only the static hostname
hostnamectl --static
# Display only the transient hostname
hostnamectl --transient
To change the hostname, use set-hostname. Without flags, it updates the static, transient, and pretty hostnames simultaneously.
# Set the static hostname (recommended)
sudo hostnamectl set-hostname server01.example.com
# Verify the change
hostnamectl --static
# Output: server01.example.com
# Check the file that was updated
cat /etc/hostname
# Output: server01.example.com
The set-hostname command writes the new value to /etc/hostname and instructs systemd-hostnamed to update the kernel’s runtime hostname. The change is immediate and survives reboot .
For temporary changes that should not persist, the legacy hostname command sets only the transient hostname.
# Temporary change (lost on reboot)
sudo hostname temp-name
hostname
# Output: temp-name
After a reboot, the transient hostname is re-derived from the static hostname in /etc/hostname .
b. Updating /etc/hosts for local resolution
Changing the hostname is only half the configuration. The local resolution file /etc/hosts must be updated to map the new hostname to the loopback address (or the machine’s actual IP address). Without this update, commands like sudo may take longer to start, and services that resolve the local hostname may fail.
# Edit the hosts file
sudo nano /etc/hosts
A typical /etc/hosts file after a hostname change:
127.0.0.1 localhost localhost.localdomain
::1 localhost localhost.localdomain
127.0.1.1 server01.example.com server01
The 127.0.1.1 entry is the conventional mapping for the system’s own hostname on Debian-based systems. On Red Hat-based systems, the mapping may use the machine’s actual IP address instead. The key is that the hostname configured with hostnamectl appears in /etc/hosts with a corresponding IP address .
c. Verifying the configuration
Verification is a two-step process. First, confirm that the hostname itself has changed. Second, confirm that the new hostname resolves correctly.
# Step 1: Confirm hostname
hostnamectl --static
hostname
# Step 2: Confirm resolution via /etc/hosts
getent hosts server01.example.com
# Output: 127.0.1.1 server01.example.com
# Step 3: Test with ping (if applicable)
ping -c 1 server01
The getent hosts command queries the system’s name service switch (/etc/nsswitch.conf) and respects the order defined there. If files precedes dns, the /etc/hosts entry is consulted first. This is the correct way to verify local resolution without involving external DNS .
If the hostname does not resolve, check /etc/nsswitch.conf to confirm that files is present in the hosts: line. A typical line is:
hosts: files dns myhostname
The myhostname module (provided by systemd) allows the system to resolve its own hostname even if /etc/hosts is incomplete. However, relying on it is not a substitute for a correct /etc/hosts entry .
Complete Example Session
# ============================================
# PART 1: CHECK THE CURRENT HOSTNAME
# ============================================
hostnamectl
# Static hostname: localhost.localdomain
# Transient hostname: localhost.localdomain
# ============================================
# PART 2: SET A NEW STATIC HOSTNAME
# ============================================
sudo hostnamectl set-hostname web01.example.com
# No output on success.
# ============================================
# PART 3: VERIFY THE STATIC HOSTNAME
# ============================================
hostnamectl --static
# Output: web01.example.com
cat /etc/hostname
# Output: web01.example.com
# ============================================
# PART 4: CHECK THE TRANSIENT HOSTNAME
# ============================================
hostnamectl --transient
# Output: web01.example.com
# The transient value was updated alongside the static one.
# ============================================
# PART 5: UPDATE /ETC/HOSTS
# ============================================
sudo nano /etc/hosts
# Add or modify the line:
# 127.0.1.1 web01.example.com web01
# ============================================
# PART 6: VERIFY LOCAL RESOLUTION
# ============================================
getent hosts web01.example.com
# Output: 127.0.1.1 web01.example.com
# ============================================
# PART 7: TEST WITH PING
# ============================================
ping -c 1 web01
# PING web01 (127.0.1.1) 56(84) bytes of data.
# 64 bytes from web01 (127.0.1.1): icmp_seq=1 ttl=64 time=0.045 ms
# ============================================
# PART 8: TEMPORARY HOSTNAME CHANGE
# ============================================
sudo hostname temp01
hostname
# Output: temp01
# The static hostname is unchanged:
hostnamectl --static
# Output: web01.example.com
# ============================================
# PART 9: REBOOT AND CONFIRM PERSISTENCE
# ============================================
sudo reboot
# After reboot:
hostname
# Output: web01.example.com
The nine parts trace the full workflow: checking the current state, setting a static hostname, verifying the file and runtime values, updating /etc/hosts, testing resolution, demonstrating the temporary change, and confirming persistence after reboot.
Quick Reference
hostnamectl Commands
| Command | Purpose |
|---|---|
hostnamectl | Show all hostname information |
hostnamectl --static | Show only static hostname |
hostnamectl --transient | Show only transient hostname |
hostnamectl set-hostname <name> | Set static, transient, and pretty |
hostnamectl set-hostname --static <name> | Set only static hostname |
Legacy hostname Command
| Command | Effect |
|---|---|
hostname | Print current (transient) hostname |
hostname <name> | Set transient hostname (not persistent) |
Configuration Files
| File | Purpose |
|---|---|
/etc/hostname | Static hostname (persistent) |
/etc/hosts | Local host-to-IP mappings |
/etc/machine-info | Pretty hostname, icon, chassis |
/etc/nsswitch.conf | Resolution order (files, dns) |
Validation Rules
| Rule | Limit |
|---|---|
| Character set | a-z, 0-9, hyphen |
| Maximum length | 64 characters |
| Cannot start/end with | Hyphen |
Best Practices
✅ Do This:
sudo hostnamectl set-hostname server01.example.com
<!-- Use the modern systemd command --> # ✅
getent hosts server01.example.com
<!-- Verify local resolution after change --> # ✅
127.0.1.1 server01.example.com server01
<!-- Map hostname in /etc/hosts --> # ✅
❌ Don’t Do This:
sudo hostname temp-name
<!-- Only changes transient value; lost on reboot --> # ❌
# Change hostname without updating /etc/hosts
<!-- Resolution may fail or use stale entry --> # ❌
# Use uppercase or special characters
<!-- Invalid for static hostname --> # ❌
Common Pitfalls
| Pitfall | Why It Happens | Fix |
|---|---|---|
| Hostname lost after reboot | Used hostname instead of hostnamectl | Use hostnamectl set-hostname |
| Local resolution fails | /etc/hosts not updated | Add mapping for new hostname |
sudo slow to start | Hostname does not resolve | Update /etc/hosts |
| Invalid hostname rejected | Special characters or uppercase | Use lowercase, digits, hyphens only |
| Hostname change not visible | Shell prompt not updated | Log out and back in, or run exec bash |
Real-World Examples
1. Set Static Hostname
sudo hostnamectl set-hostname db01.example.com
2. View All Hostnames
hostnamectl
3. View Static Only
hostnamectl --static
4. Temporary Change
sudo hostname temp01
5. Update /etc/hosts
127.0.1.1 db01.example.com db01
6. Verify Resolution
getent hosts db01.example.com
7. Test Connectivity
ping -c 1 db01
8. Check Persistent File
cat /etc/hostname
9. Check Resolution Order
grep hosts /etc/nsswitch.conf
10. Reboot and Verify
sudo reboot
hostname
Visual
Hostname Types
┌──────────────────────────────────────────────┐
│ PRETTY HOSTNAME │
│ "Lennart's Laptop" │
│ Free-form, stored in /etc/machine-info │
└──────────────────┬───────────────────────────┘
│
▼
┌──────────────────────────────────────────────┐
│ STATIC HOSTNAME │
│ "lennarts-laptop" │
│ Persistent, stored in /etc/hostname │
│ Set with: hostnamectl set-hostname │
└──────────────────┬───────────────────────────┘
│
▼
┌──────────────────────────────────────────────┐
│ TRANSIENT HOSTNAME │
│ "node12345678" │
│ Runtime fallback, set by DHCP/mDNS │
│ Ignored if static hostname exists │
└──────────────────────────────────────────────┘
The static hostname is the one that matters for persistence and identity.
Resolution Flow
┌──────────────────────────────────────────────┐
│ Application calls getaddrinfo() │
└──────────────────┬───────────────────────────┘
│
▼
┌──────────────────────────────────────────────┐
│ /etc/nsswitch.conf: hosts: files dns │
└──────────────────┬───────────────────────────┘
│
▼
┌──────────────────────────────────────────────┐
│ Check /etc/hosts │
│ ├─ Match found → return IP │
│ └─ No match → query DNS │
└──────────────────────────────────────────────┘
The /etc/hosts entry is consulted before DNS when files precedes dns.
Summary
| Item | Value |
|---|---|
| Persistent hostname file | /etc/hostname |
| Modern set command | hostnamectl set-hostname <name> |
| Legacy temporary command | hostname <name> |
| Pretty hostname file | /etc/machine-info |
| Local resolution file | /etc/hosts |
| Resolution order | /etc/nsswitch.conf |
| Max hostname length | 64 characters |
| Valid characters | a-z, 0-9, hyphen |
Key takeaways:
hostnamectl set-hostnameis the correct command. It updates/etc/hostnameand sets both static and transient hostnames. The change persists across reboots .- The legacy
hostnamecommand is temporary. It changes only the transient hostname, which reverts to the static value on reboot . /etc/hostsmust be updated separately. Changing the hostname does not update the local resolution file. The new hostname must be mapped to an IP address forgetent hostsandpingto succeed .- The static hostname has validation rules. Lowercase ASCII, digits, and hyphens only, maximum 64 characters. The pretty hostname has no such restrictions but is not used for network identity .
getent hostsis the verification tool. It respects/etc/nsswitch.confand shows how the system resolves the name without sending network traffic .
Remember: Hostname configuration is a two-file operation. hostnamectl sets the identity; /etc/hosts makes it resolvable. The exam tests both. Setting the hostname without updating /etc/hosts leaves a system that knows its name but cannot resolve it—a configuration that appears correct until a service tries to connect.
Stop using slow, ad-bloated tool sites! 🤮
🔎 Search “KandZ Tools” on Google to use many professional utilities for free.
KandZ.me is the ultimate minimalist hub for:
✅ Finance (Mortgage, Interest, Inflation)
✅ Tech (Base64, JSON, Dev Suite, IP)
✅ Health (BMI, BMR, TDEE)
✅ Productivity (Timer, Workspace, QR)
⚡️ Fast & Private
🔒 No data leaves your device
💎 100% Free
🔗 Use it now: https://tools.kandz.me
🔖 Bookmark it—you’ll need it later!