The /etc/hostname
file in Ubuntu (and most Linux distributions) is designed to store only one hostname per system. This is a fundamental design choice in Unix-like systems where the kernel maintains a single nodename (retrievable via uname -n
). Attempting to put multiple names in this file will cause system issues.
For servers needing different identities in multiple domains (like ISP's domain and company domain), the correct approach involves DNS and host file configuration rather than modifying /etc/hostname
.
1. Set Primary Hostname
# Set the primary hostname in /etc/hostname
echo "primary-hostname" | sudo tee /etc/hostname
# Update the running system
sudo hostname -F /etc/hostname
2. Configure /etc/hosts File
# Example /etc/hosts configuration
127.0.0.1 localhost
192.168.1.10 primary-hostname.isp-domain.com primary-hostname
192.168.1.10 company-alias.company-domain.com company-alias
3. DNS Configuration
Create these DNS records in both domains:
; ISP Domain Zone
primary-hostname.isp-domain.com. IN A 192.168.1.10
; Company Domain Zone
company-alias.company-domain.com. IN CNAME primary-hostname.isp-domain.com.
For web servers, you can implement this at the application level:
# Apache VirtualHost example
<VirtualHost *:80>
ServerName primary-hostname.isp-domain.com
ServerAlias company-alias.company-domain.com
# Other configuration...
</VirtualHost>
After configuration, verify with:
hostname # Should show primary-hostname
hostname -f # Should show FQDN
ping company-alias.company-domain.com # Should resolve
- SSL certificates must cover all names if using HTTPS
- Mail servers require special MX record handling
- Kerberos/AD environments need additional SPN configuration
The /etc/hostname
file in Ubuntu (and most Linux distributions) is designed to store a single hostname for identification purposes. This is a fundamental system configuration where multiple entries simply won't work as expected. The systemd-hostnamed service reads this file at boot and expects exactly one hostname.
For servers needing multiple DNS identities (like datacenter.domain and company.domain), you should:
# 1. Set primary hostname in /etc/hostname
echo "primary-hostname" | sudo tee /etc/hostname
# 2. Configure aliases in /etc/hosts
127.0.1.1 primary-hostname.company.domain primary-hostname
127.0.1.1 dc-hostname.datacenter.domain
For external resolution, create proper DNS records:
- A/AAAA record for primary hostname
- CNAME alias for secondary hostname
- PTR records for reverse DNS
After configuration, verify with:
hostname # Shows configured hostname
hostname -f # Shows FQDN
hostname -A # Shows all FQDNs
For a web server serving both company.com and datacenter.net:
# /etc/hostname
web01
# /etc/hosts
127.0.1.1 web01.company.com web01
192.168.1.10 web01.datacenter.net
If hostname changes don't persist:
sudo hostnamectl set-hostname newname
- Reboot or restart systemd-hostnamed
- Check
systemctl status systemd-hostnamed