It’s not always enough to have only one virtual machine for testing purposes. More – it’s not enough in most of the cases. One of such cases is – for example – when you want to try a multi-node configuration for software like Hadoop or Cassandra, or make a failover test of your system. For me most comfortable and useful is to have a configuration that allows guest system to use the Internet (usually it’s a default, NAT mode) but also makes it possible to easily connect to guest system from host system (not by port forwarding) and makes guest systems able to “talk” with each other – it’s good enough to mimic most of production configurations I use. To do this we need to setup a two-network-cards configuration for alle the guest systems we have. This article is about how to make it work.
Note: This tutorial assumes that you setup one virtual machine first and then clone it, because it’s usually the easiest solution to have a working, identical system there. However, it makes a little mess with your network configuration (changed interfaces names because of regenerated MAC addresses) and this tutorial explains how to get rid of this problem. This tutorial was prepared basing on CentOS 6. All commands should be run with root privileges.
First, you need to setup a host-only network in VirtualBox. To do this go to:
File -> Preferences -> Network and click “Add host-only network”. You can leave the default settings – just check and remember the settings to know which IP adresses are available.
Then you have to enable second network card in your “base” virtual machine – you can do it in its VirtualBox settings menu. First network card should be attached by default to NAT which makes the gust system able to connect to the Internet. Second one should be turned on and attached to “Host-only Adapter”. Then, pick your host-only network you’ve just created using the field below (“Name”). Make sure that all MAC addresses are unique (might be not true if you cloned the machine and forgot to force VirtualBox to pick new MAC’s for it) and that “Cable Connected” checkbox is checked. Now you can turn on the virtual machine.
It’s very possible that your second network interface is not up by default – you’ll probably have
eth0 up and running, but no
eth1 at all. You can check it with
ifconfig. To bring it up, copy the existing
eth0 configuration – calling it
eth1 – and configure it according to your needs – i.e. by setting static IP configuration. This is what I did:
cp /etc/sysconfig/network-scripts/ifcfg-eth0 /etc/sysconfig/network-scripts/ifcfg-eth1
[root@centos01 ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth1 DEVICE="eth1" BOOTPROTO="static" HWADDR="08:00:27:D0:CD:D3" IPADDR=192.168.56.101 NETMASK=255.255.255.0 NM_CONTROLLED="yes" ONBOOT="yes" TYPE="Ethernet" UUID="a090309a-efd5-4ad1-8332-8c24435c6877"
Remember to set proper MAC address of second network card (HWADDR) and set BOOTPROTO to “static”. ONBOOT set to “yes” sounds like a good idea too. After doing this – start (or restart if it was already up) interface using
ifup eth1 and after this
ifconfig should tell you that everything works as intended.
At this moment you have one virtual machine and can use your host system to connect to it without any port forwarding – just ssh to the IP you set (
192.168.56.101 in this example).
Now it’s time to clone the machine. Remember to check the option “Reinitialize the MAC address of all network card”. If you forget doing this, you can do it for each network card separately in the virtual machine’s settings.
When you boot the cloned machine, you’ll notice that eth0 and eth1 interfaces haven’t been brought up on system startup (if you missed this information – run
/etc/init.d/network restart) – this is because there are some system settings refering to “old” MACs. System has noticed that there are two new devices (MACs), but it wasn’t able to find out what actually happened, so it just created two new interfaces (
eth3) for these MACs, but there’s no configuration for them. We have to fix this.
First – change server’s hostname. As you do it, open
/etc/sysconfig/network-scripts/ifcfg-eth1 and update them with newly generated MACs. Additionally, you have to set proper IP for
eth1 interface – for example in my case it would be
After that open
/etc/udev/rules.d/70-persistent-net.rules and configure the interfaces so system could properly assign MAC addresses to interfaces. If you follow this tutorial, there are 4 lines there, but first two of them refer to unexistent devices (“old” MACs), so you have to remove them. In next two of them change
eth1 and change MAC addresses so they correspond to virtual machine’s settings –
eth0 MAC should be the same as MAC of first network card of the virtual machine and
eth1 should match the second network card settings. Save the file, “retrigger” udev (so the changes you made in udev configuration will be applied) and restart network daemon:
udevadm trigger /etc/init.d/network
This should bring both interfaces up and proper IP configuration should be now obtained. To check it, ssh to guest system from your host, for example:
michal@laptop:~$ ssh email@example.com firstname.lastname@example.org's password: Last login: Sun Jul 15 17:47:18 2012 [root@hdps01 ~]# ping www.google.com PING www.l.google.com (126.96.36.199) 56(84) bytes of data. 64 bytes from 188.8.131.52: icmp_seq=1 ttl=63 time=40.1 ms 64 bytes from 184.108.40.206: icmp_seq=2 ttl=63 time=39.0 ms ^C --- www.l.google.com ping statistics --- 2 packets transmitted, 2 received, 0% packet loss, time 1569ms rtt min/avg/max/mdev = 39.052/39.607/40.162/0.555 ms
Of course it works for