If you are accustomed to VMware vCenter Server and the virtual center management tool then you are well aware of the option to clone a virtual machine. This seems to be the most simple way to create a new virtual machine, especially in a time crunched situation. While Microsoft is pretty widespread and has sufficient GUI options, Linux is not quite the same. There are a ton of Linux gurus out there so forgive me if this is quite elementary as it is aimed at the less experienced Linux user. For the purpose of this article I will use Red Hat Enterprise Linux 5.5 (RHEL) as my example, simply because I administer it on a daily basis in my virtual infrastructure. It’s important to note that VMware ESX/ESXi is based on an RHEL kernel so it’s good to at least know the basics of the Linux kernel in order to create a solid foundation on which to build your VMware skill set.

The other day I was given the task of installing and configuring ESX 3.5 (yes, we are a tad behind) on two new Dell R610 servers and then subsequently build and deploy 60 RHEL 5.5 VMs on each server. At first glance this seems like a daunting task, but with the use of Virtual Center and its endless options, it is much more manageable than you would think. After installing and configuring the servers, I added the hosts to my vCenter inventory and then created an RHEL 5.5 virtual machine. Once I created it, I opened up a new terminal window and typed hostname RHEL01 which on the surface seems to have applied the hostname. However, if you are to reboot your machine and type the hostname command in a terminal window, you would not see RHEL01, it doesn’t stick. The proper way to give your VM a hostname that will stick is to configure both your /ect/hosts file as well as your /etc/sysconfig/network file.

The /etc/hosts File
What the heck is an /etc/hosts file anyways? Your /etc/hosts file is a simple text database that will work in conjunction with DNS in giving you name to IP address mapping, in short, it puts the name with and address. If you don’t have a DNS server stood up you can place the host name of all your local computers connected to your LAN. To configure your /etc/hosts file, simply open a terminal window, type vim /etc/hosts then hit enter, and your /etc/hosts file will appear in your vim editor. At this point in time you will see in the /etc/hosts file the file name of the VM that you cloned from. Having multiple VMs with the same hostname will cause you havoc on your network, I think this is pretty obvious though. This is where you need to enter into edit mode (by typing a lower case i) and change the old host name to your new hostname, hit esc when you are done and then :wq! to save what you have written in that file. Your /etc/hosts file should look something like this (depending on the distro of Linux): localhost.localdomain localhost server.name.de server

The /etc/sysconfig/network File
Now that you are a vim genius, and you have conquered the /etc/hosts file, time to graduate over to the /etc/sysconfig/network file. This is important because if you leave it configured with the original VM’s network settings you will obviously have an IP address conflict and you’ll never reach the Internet (duhn, duhn, duhn!). So let’s make it right so that we don’t have users griping at us. Your /etc/sysconfig/network file will look something like this (depending on the distro of Linux):

    HOSTNAME=deep. openna.com

You will need to make sure that NETWORKING is set to yes, FORWARD_IPV4 is set to yes, make sure you erase the old host name and enter your new host name, then set your GATEWAY to whatever it is on your given subnet, GATEWAYDEV should be where gwdev is the device name eth# you use to access the remote gateway. That’s it, it’s quite simple and there is nothing more you need to do with this file.

Clone With Caution
You should be set to clone away on your ESXi host. Just remember that if you attempt to do to many clones at the same time, you will take a major hit on performance speed. Take your time, never attempt to have more than 5 clones processing at the same time. Another important thing to remember is to make sure you have enough space on your host for the amount of clones that you or your customer wants. I ran into this problem with a customer and he was incensed that he couldn’t have as many clones as he wanted. He wanted VMs with huge hard drives but didn’t have enough SAS drives to cover it. Just make sure you cover your bases before diving into a cloning frenzy, make sure it will be easy, that’s how it’s supposed to be.

Greg W Stuart
Greg is the owner and editor of vDestination.com. He's been a VMware vExpert every year since 2011. Greg enjoys spending time with his wife and 3 kids. He works as a Sr. Consultant at VMware and resides in Northern Virginia, 15 minutes west of Washington DC.

2 thoughts on “Host Name Issues When Cloning a Linux VM

  1. Good article. Another place you should check is /etc/sysctl.conf as the kernel.hostname can be set there. If the system is running Oracle, there’s also a ORACLE_HOSTNAME environmental variable set in the .bash_profile.

    In both cases, the actual machine’s hostname would be changed, but it would still look the old hostname if you’re on the (virtual) console of the VM.

  2. Good old times… It used to be almost simple, but there’s a lot more than “just” that. The “predictable file names” thing has made cloning a bit simpler on the network side (especially if you rely on DHCP without reservations), but juggling with not-so-unique UUIDs for your disks is gonna make things a bit of a nightmare if you have to attach your VMDKs from one clone to another… Guess what happens when /dev/hdb is a clone of /dev/hda and the boot disk is referenced by UUID?

    Make sure the cloned machine doesn’t reference anything by UUID and make sure to change UUIDs as part of the “sysprep” (what the Windows thing is called).

    Cloning a Linux VM is not “that” easy, and FOSS is not “that” transparent any longer.

Leave a Reply

Your email address will not be published. Required fields are marked *