Complete Guide: Configuring Domain Names in CentOS via SSH – Hosts File & Network Configuration


2 views

When configuring domain names on CentOS, these two files are crucial:

/etc/hosts       # Local hostname resolution
/etc/sysconfig/network   # System hostname configuration

1. Setting the System Hostname:

# Edit network configuration
vi /etc/sysconfig/network

# Add or modify these lines:
NETWORKING=yes
HOSTNAME=yourdomain.com

2. Configuring Local DNS Resolution:

vi /etc/hosts

# Example configuration:
127.0.0.1   localhost localhost.localdomain
192.168.1.100 server1.yourdomain.com server1

For permanent changes that persist after reboot:

# Using hostnamectl (CentOS 7+)
hostnamectl set-hostname server1.yourdomain.com

# Verify the hostname
hostnamectl status
# Restart network services
systemctl restart network

# Verify configuration
hostname
hostname -f
ping server1.yourdomain.com

If changes don't take effect:

# Check SELinux context
ls -Z /etc/hosts

# Verify name resolution
getent hosts server1.yourdomain.com

# Check system logs
journalctl -xe
# For systems using NetworkManager
nmcli general hostname
nmcli general hostname server1.yourdomain.com

When working with CentOS servers, especially via SSH, configuring domain names correctly is crucial for network communication. The two primary files involved are:

  • /etc/sysconfig/network - Contains basic network configuration
  • /etc/hosts - Maps hostnames to IP addresses

First, let's configure the network settings:

# Open the network configuration file
sudo vi /etc/sysconfig/network

# Add or modify these lines
NETWORKING=yes
HOSTNAME=yourdomain.com
GATEWAY=192.168.1.1

The hosts file needs proper entries for name resolution:

# Edit the hosts file
sudo vi /etc/hosts

# Add entries like this (replace with your actual IPs)
127.0.0.1   localhost localhost.localdomain
192.168.1.100 server1.yourdomain.com server1
192.168.1.101 server2.yourdomain.com server2

After making changes, verify with these commands:

# Check hostname
hostname
hostname -f

# Test DNS resolution
ping server1.yourdomain.com
nslookup yourdomain.com

For production environments, consider these additional steps:

# Install and configure BIND for DNS
sudo yum install bind bind-utils -y

# Configure named.conf
sudo vi /etc/named.conf

# Set up zone files
sudo vi /var/named/yourdomain.com.zone

If you encounter problems:

  • Ensure the hostname matches in both files
  • Check for typos in IP addresses
  • Restart network services after changes: sudo systemctl restart network
  • Verify firewall isn't blocking DNS traffic