
Changing the domain name or set hostname on a Linux virtual machine (VM) involves updating the system’s hostname and domain configuration files. This guide explains the steps to modify the domain name on your Linux VM.
Step 1: Check the Current Hostname
Before changing the domain name, verify the current hostname and domain settings:
hostnamectl
The output will display the current hostname and related details.
Step 2: Update the Hostname
- Change the hostname using the
hostnamectl
command:sudo hostnamectl set-hostname new-hostname
Replacenew-hostname
with the desired hostname. - Update the
/etc/hostname
file:sudo nano /etc/hostname
Replace the old hostname with the new one and save the file. - Update the
/etc/hosts
file to map the new hostname to the localhost IP:sudo nano /etc/hosts
Update the line that begins with127.0.1.1
to include your new hostname, for example:127.0.1.1 new-hostname.domainname new-hostname
Step 3: Set the Domain Name
To set or change the domain name:
- Edit the
/etc/hosts
file:sudo nano /etc/hosts
Add or update the domain name entry for the localhost IP, for example:127.0.0.1 localhost 127.0.1.1 new-hostname.new-domain.com new-hostname
- Update the
/etc/resolv.conf
file to set the DNS search domain:sudo nano /etc/resolv.conf
Add the following line:search new-domain.com
Optionally, specify DNS nameservers:nameserver 8.8.8.8 nameserver 8.8.4.4
Step 4: Restart the System
Restart your Linux VM to apply the changes:
sudo reboot
After rebooting, verify the changes:
hostnamectl
Step 5: Verify Domain Resolution
To ensure the new domain name is properly set up:
- Use the
ping
command:ping new-domain.com
- Check DNS resolution:
nslookup new-domain.com
Troubleshooting
- Hostname Not Updating: Ensure all changes are saved in
/etc/hostname
and/etc/hosts
. - DNS Issues: Verify the contents of
/etc/resolv.conf
and confirm that your DNS servers are reachable. - Persistent “ Issues: If the file resets on reboot, use a network manager or override settings in
/etc/systemd/resolved.conf
.
Conclusion
By following these steps, you can successfully change the domain name on your Linux VM. Ensure the new hostname and domain are reflected across all configuration files for seamless integration.