Headless VirtualBox in a Linux Environment
I have been playing around with Virtualbox a bit recently, and from what I have experienced, I’m impressed, greatly! Granted VirtualBox in a headless environment is a little daunting at first, it is not too difficult to get use to. The biggest hurtle I experienced was getting use to the command line interface, as my previous experience with VirtualBox was in windows with a GUI interface. The best thing about my experience was how well documented VirtualBox was. So here are some instructions for installing & configuring VirtualBox 4.2.8 on a headless linux RHEL derivative in case anyone else wants to try it out.
VirtualBox is installed via a yum repository for RHEL, Scientific Linux, Centos, Debian, and Oracle. Instructions can be found here. So let’s add the VirtualBox repository to your system:
sudo wget http://download.virtualbox.org/virtualbox/rpm/el/virtualbox.repo -O /etc/yum.repos.d/virtualbox.repo
Then install VirtualBox with yum:
sudo yum install VirtualBox-4.2
You would normally think that your finished, but in the case of VirtualBox your incorrect. VirtualBox requires some additional stuff to enable proper headless operation, so lets do that now:
cd /tmp wget http://download.virtualbox.org/virtualbox/4.2.8/Oracle_VM_VirtualBox_Extension_Pack-4.2.8-83876.vbox-extpack sudo VBoxManage extpack install Oracle_VM_VirtualBox_Extension_Pack-4.2.8-83876.vbox-extpack
Don’t forget to add the user to your system that will be running VirtualBox if you have not done so already, and change to that user.
We will be using VBoxManage for the majority of the setup & configuration. The first step is figuring out which OS we want on the guest system. For this example I’m going to choose Windows 7 64 bit.
Create our virtual machine:
VBoxManage createvm --name "Windows 7" --ostype Windows7_64 --register
If you look at the command above you can see that I used ‘Windows7_64′ for the ostype, you can get a list of the OS Types VirtualBox supports by typing:
VBoxManage list ostypes
Now we need to tell VirtualBox how much memory to assign to the machine:
VBoxManage modifyvm "Windows 7" --memory 8192
Using the proper OS Type pre-sets some defaults commonly used for that particular OS. We will come back to these settings later, but for now we will continue with the configuration by creating a virtual drive for our guest system:
VBoxManage createhd --filename "Win7.vdi" --size 20480
As you can see I created a 20Gb virtual drive for my guest OS. Now we need to create a storage controller for the drive to attach to:
VBoxManage storagectl "Windows 7" --name "SATA Controller" --add sata --sataportcount 4
Now lets attach our drive to the new controller:
VBoxManage storageattach "Windows 7" --storagectl "SATA Controller" --port 0 --type hdd --medium WinXP.vdi
While we are at it, lets go ahead and attach the Windows 7 Install ISO:
VBoxManage storageattach "Windows 7" --storagectl "SATA Controller" --port 1 --type dvddrive --medium en_windows_7_ultimate_with_sp1_x64_dvd_u_677332.iso
Ok, back to changing some settings for the virtual machine. You can look a the current configuration so far by using:
VBoxManage showvminfo "Windows 7"
If you have a processor that supports virtualization optimization I would highly recommend you turn on a few options:
VBoxManage modifyvm "Windows 7" --hwvirtexexcl on VBoxManage modifyvm "Windows 7" --nestedpaging on VBoxManage modifyvm "Windows 7" --vtxvpid on VBoxManage modifyvm "Windows 7" --largepages on
Configure a virtual sound device (64-bit Windows 7/Server 2008/Server 2012 driver):
VBoxManage modifyvm "Windows 7" --audio oss
Configure a virtual network interface (more on this later):
VBoxManage modifyvm "Windows 7" --nic1 nat
At this point you are probably asking; “How do I interact with a headless virtual machine?”. Well, in headless mode VirtualBox attaches the machine to a port, accessible via any MS Remote Desktop compatible program, like rdesktop. But we need to tell the virtual machine where to attach itself:
VBoxManage modifyvm "Windows 7" --vrde on VBoxManage modifyvm "Windows 7" --vrdeport 4000
When we start our virtual machine it will be accessible on port 4000 of EACH ASSIGNED IP ADDRESS. We can change what IP it listens on by running:
VBoxManage modifyvm "Windows 7" --vrdeaddress 192.168.1.123
Now it will only listen on tcp port 4000 of 192.168.1.123.
Starting our Virtual Machine
We start our machine by running
VBoxHeadless --startvm "Windows 7"
This will start the machine, so you might want to screen this command. Also note that using VBoxHeadless forcibly turns ON the vrde setting. If you point a remote desktop program to your configured port and ip address you should see your system. If for some reason the command immediately terminates you may have forgotten to install the Extension Pack.
You might notice that interacting with your system at this point is difficult, but that’s because the guest os does not have VirtualBox’s guest additions running. So, after you finish the Windows install you will need to download guest additions and install it. Use your knowledge from above to attach the ISO like you did the Windows Install ISO. You can swap ISO’s in and out like regular CDs while the virtual machine is running. Also, If you want to remove an ISO from the guest os use ‘emptydrive’ in place of the ISO filename.
Advanced Network Config
The steps above attach the virtual machine to the network using a network address translation type of communication, making port forwards to the machine necessary. While port forwards are supported, you may need multiple ports forwarded to serve multiple services from the guest os. This can be tedious, you may want the virtual machine to act as a physically connected device to the network. This can be accomplished by creating an adapter for VirtualBox to use, and assigning the guest os an ip on that network. First we need to set the virtual machine’s adapter into host only mode:
VBoxManage modifyvm "Windows 7" --nic1 hostonly
Now, lets create the adapter:
sudo VBoxTunctl -u vbox -g vbox -t VBox0 sudo ifconfig VBox0 192.168.50.1/24
This will create the network device VBox0 owned by the user/group vbox and set the ip address to 192.168.50.1 with a subnet of 255.255.255.0. All you have to do is simply configure your firewall for the new adapter and set the virtual machine to use an ip in that logical range. Now all we have to do is tell the virtual machine what the name of the adapter is we just created:
VBoxManage modifyvm "Windows 7" --hostonlyadapter1 VBox0