How to Change or set Host Name on a Linux Virtual Machine – Domain name

set hostname ubuntu

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

  1. Change the hostname using the hostnamectl command: sudo hostnamectl set-hostname new-hostname Replace new-hostname with the desired hostname.
  2. Update the /etc/hostname file: sudo nano /etc/hostname Replace the old hostname with the new one and save the file.
  3. Update the /etc/hosts file to map the new hostname to the localhost IP: sudo nano /etc/hosts Update the line that begins with 127.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:

  1. 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
  2. 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:

  1. Use the ping command: ping new-domain.com
  2. Check DNS resolution: nslookup new-domain.com

Troubleshooting

  1. Hostname Not Updating: Ensure all changes are saved in /etc/hostname and /etc/hosts.
  2. DNS Issues: Verify the contents of /etc/resolv.conf and confirm that your DNS servers are reachable.
  3. 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.

Scroll to Top