Skip to main content
Back to Category

Server Hostname and DNS Configuration: Step-by-Step Guide

Set hostname with hostnamectl, configure /etc/hosts and /etc/resolv.conf, set up FQDN, understand PTR record importance, and test with dig/nslookup.

Read time: 10 min Server Management
hostnamednsfqdnhostnamectlresolv.confptr recordlinux

Table of Contents

Server Hostname and DNS Configuration: Step-by-Step Guide

Your server's hostname and DNS configuration form the foundation of network communication. A misconfigured hostname can cause email delivery issues, confusion in log records, and some applications to malfunction. This guide covers hostname setup, DNS configuration, and verification steps.

What is a Hostname?

A hostname is the name that identifies your server on the network. There are two types:

  • Short hostname: Just the machine name (e.g., webserver01)
  • FQDN (Fully Qualified Domain Name): Full domain name (e.g., webserver01.rexe.tr)

The FQDN combines the hostname with the domain name and uniquely identifies your server on the internet.

Always use FQDN on production servers. This is critical for mail servers, SSL certificates, and log management.

Viewing the Current Hostname

hljs bash
# Short hostname
hostname

# FQDN
hostname -f

# All hostname information
hostnamectl

# Example output:
# Static hostname: webserver01
# Icon name: computer-server
# Chassis: server
# Operating System: Ubuntu 22.04.3 LTS
# Kernel: Linux 5.15.0-91-generic
# Architecture: x86-64

Setting Hostname with hostnamectl

hostnamectl is the modern tool for hostname management on systemd-based systems.

hljs bash
# Set hostname
sudo hostnamectl set-hostname webserver01

# Set hostname with FQDN
sudo hostnamectl set-hostname webserver01.rexe.tr

# Verify the change
hostnamectl

# Apply change by opening a new shell
exec bash

Hostname Types

hljs bash
# Static hostname (persistent, written to /etc/hostname)
sudo hostnamectl set-hostname webserver01 --static

# Transient hostname (reset on reboot)
sudo hostnamectl set-hostname webserver01 --transient

# Pretty hostname (for display purposes)
sudo hostnamectl set-hostname "Web Server 01" --pretty

The /etc/hostname File

The hostname is stored persistently in /etc/hostname:

hljs bash
# View the file
cat /etc/hostname

# Edit manually
sudo nano /etc/hostname
webserver01

Write only the short hostname in /etc/hostname, not the FQDN. FQDN configuration is done in /etc/hosts.

Configuring /etc/hosts

The /etc/hosts file is a local name resolution table checked before DNS queries.

hljs bash
# View the file
cat /etc/hosts

# Edit
sudo nano /etc/hosts

Correct /etc/hosts Configuration

127.0.0.1       localhost
127.0.1.1       webserver01.rexe.tr webserver01

# Server's real IP address
203.0.113.100   webserver01.rexe.tr webserver01

# IPv6
::1             localhost ip6-localhost ip6-loopback
ff02::1         ip6-allnodes
ff02::2         ip6-allrouters

The 127.0.1.1 line is used for FQDN on Ubuntu/Debian systems. On CentOS/RHEL systems, use the real IP address.

/etc/hosts Use Cases

hljs bash
# Local development environment
192.168.1.10    db.local database
192.168.1.20    cache.local redis

# Block a specific domain
0.0.0.0         ads.example.com

# Alias for quick access
10.0.0.5        prod-db production-database

/etc/resolv.conf DNS Configuration

/etc/resolv.conf defines the system's DNS servers.

hljs bash
# View current DNS configuration
cat /etc/resolv.conf

Example resolv.conf

# DNS servers
nameserver 8.8.8.8
nameserver 8.8.4.4
nameserver 1.1.1.1

# Search domain (for short names)
search rexe.tr

# Domain
domain rexe.tr

# Maximum DNS retry count
options attempts:3
# DNS timeout (seconds)
options timeout:2

DNS Management with systemd-resolved

On Ubuntu 18.04+ and modern Debian systems, DNS is managed by systemd-resolved:

hljs bash
# View DNS status
resolvectl status

# Query DNS for a specific domain
resolvectl query rexe.tr

# Flush DNS cache
resolvectl flush-caches

# View DNS statistics
resolvectl statistics
hljs bash
# Persistent DNS setting with systemd-resolved
sudo nano /etc/systemd/resolved.conf
hljs ini
[Resolve]
DNS=8.8.8.8 8.8.4.4
FallbackDNS=1.1.1.1
Domains=rexe.tr
DNSSEC=no
DNSOverTLS=no
hljs bash
# Restart the service
sudo systemctl restart systemd-resolved

Setting Up FQDN

Follow these steps for correct FQDN setup:

hljs bash
# 1. Set hostname
sudo hostnamectl set-hostname webserver01

# 2. Edit /etc/hosts
sudo nano /etc/hosts
127.0.0.1       localhost
127.0.1.1       webserver01.rexe.tr webserver01
203.0.113.100   webserver01.rexe.tr webserver01
hljs bash
# 3. Verify FQDN
hostname -f
# Output: webserver01.rexe.tr

# 4. Verify short hostname
hostname -s
# Output: webserver01

# 5. Verify IP address
hostname -I

PTR Record (Reverse DNS) Importance

A PTR record is a reverse DNS record that resolves an IP address to a hostname.

Why PTR Records Matter

  • Email delivery: Spam filters reject emails from IPs without PTR records
  • Security: Network security tools verify PTR records
  • Log analysis: Seeing hostnames instead of IPs makes logs easier to read
  • Some services: FTP, IRC and similar services may require PTR records

Setting Up PTR Records

PTR records are set by the ISP or hosting provider that owns your IP address. You can manage your PTR record from the REXE customer panel.

hljs bash
# Check current PTR record
dig -x 203.0.113.100

# or
nslookup 203.0.113.100

# Expected output:
# 100.113.0.203.in-addr.arpa. IN PTR webserver01.rexe.tr.

Testing DNS with dig

dig (Domain Information Groper) is the most powerful tool for testing DNS queries.

hljs bash
# Query A record
dig rexe.tr A

# Query MX record
dig rexe.tr MX

# Query NS record
dig rexe.tr NS

# Query TXT record
dig rexe.tr TXT

# Use a specific DNS server
dig @8.8.8.8 rexe.tr A

# Short output
dig +short rexe.tr A

# Query all records
dig rexe.tr ANY

# Reverse DNS query
dig -x 203.0.113.100

# Check DNS propagation
dig +trace rexe.tr

Testing DNS with nslookup

hljs bash
# Basic query
nslookup rexe.tr

# With specific DNS server
nslookup rexe.tr 8.8.8.8

# Reverse DNS
nslookup 203.0.113.100

# Interactive mode
nslookup
> server 8.8.8.8
> set type=MX
> rexe.tr
> exit

Quick Testing with host Command

hljs bash
# Basic query
host rexe.tr

# Reverse DNS
host 203.0.113.100

# MX record
host -t MX rexe.tr

# Specific DNS server
host rexe.tr 1.1.1.1

Troubleshooting

Hostname Change Not Applied

hljs bash
# Start a new shell
exec bash

# or restart the system
sudo reboot

# Check /etc/hostname
cat /etc/hostname

DNS Resolution Not Working

hljs bash
# Test DNS connectivity
dig @8.8.8.8 google.com

# Check resolv.conf
cat /etc/resolv.conf

# Check systemd-resolved status
systemctl status systemd-resolved

# Check nsswitch.conf
cat /etc/nsswitch.conf | grep hosts
# Output: hosts: files dns

For DNS issues, first check /etc/hosts, then /etc/resolv.conf. The hosts line in nsswitch.conf determines the resolution order.

Conclusion

Correct hostname and DNS configuration is one of the cornerstones of server management. Properly setting the FQDN on your REXE servers is critical for email delivery, SSL certificates, and application configurations. You can use the REXE customer panel to ensure your PTR record is correctly configured.