When setting up a CentOS server to serve www.myserver.com
, you need to configure three components:
- The
/etc/hosts
file - The
/etc/sysconfig/network
file - The active hostname using the
hostname
command
For a web server serving www.myserver.com
, here's the proper setup:
1. /etc/sysconfig/network Configuration
HOSTNAME=myserver.com
This should be the base domain without the www
prefix. The hostname should represent the machine itself, not the service it's running.
2. /etc/hosts File Configuration
XXX.XXX.XXX.XXX myserver.com www.myserver.com www
This maps both the base domain and www subdomain to your server's IP address. The format should be:
IP_ADDRESS FQDN [ALIASES...]
3. Setting the Active Hostname
sudo hostname myserver.com
This sets the immediate hostname, but you'll need to reboot or restart network services for permanent changes.
For a server with IP 192.168.1.100 serving www.myserver.com:
# /etc/sysconfig/network
HOSTNAME=myserver.com
# /etc/hosts
192.168.1.100 myserver.com www.myserver.com www localhost.localdomain localhost
# Command to set hostname immediately
sudo hostname myserver.com
sudo systemctl restart network
After making these changes:
- Verify with
hostname -f
(should show FQDN) - Check
nslookup myserver.com
resolves correctly - Ensure your DNS records point to the server's IP
If hostname changes don't persist after reboot:
# Check what files might be overriding your settings
grep -r "HOSTNAME" /etc
For services that might cache the hostname:
sudo systemctl restart systemd-hostnamed
sudo systemctl restart NetworkManager
When setting up a CentOS server to serve a website like www.myserver.com, proper hostname configuration is crucial. The system uses multiple locations to store and reference this information, and they need to be consistent.
There are three primary places where hostname information is stored in CentOS:
- /etc/hosts - Local hostname resolution
- /etc/sysconfig/network - System-wide network configuration
- hostname command - Current system hostname
In your case, you should use the base domain name in this file:
HOSTNAME=myserver.com
This is because the hostname should typically be the base domain name, not the FQDN including www. The www prefix is better handled through DNS or virtual host configurations.
Your /etc/hosts file should contain both variations for proper resolution:
XXX.XXX.XXX.XXX myserver.com www.myserver.com www
This configuration ensures that:
- The system can resolve both the base domain and www subdomain
- Local services won't fail due to resolution issues
- You maintain flexibility in your web server configuration
To immediately set the hostname without rebooting, use:
sudo hostname myserver.com
This sets the transient hostname, which will be overwritten by the value in /etc/sysconfig/network on reboot.
After making these changes, verify everything is working correctly:
hostname
hostname -f
hostname -A
Also check that name resolution works both ways:
ping -c 1 myserver.com
ping -c 1 www.myserver.com
For a web server, you might also want to:
# Update the mailname if postfix is installed
echo "myserver.com" > /etc/mailname
# Ensure the hostname appears correctly in system logs
sudo service rsyslog restart
If you encounter problems:
# Check systemd hostname setting
hostnamectl status
# Verify the hostname is properly set in network manager
nmcli general hostname
Remember that some services might need to be restarted after hostname changes, especially if they cache the hostname at startup.