Run Windows Guest in CentOS 6 Linux Host using Virtualbox 4 via the command line
I wanted to be able to run Windows applications on the Linux server without having to pay for the extra money for a separate Windows server. There is a lot of unused capacity on the Linux server, so this won't interfere with its performance much. I already use Virtualbox for development because it is free and very fast.
Now, I finally learned how to control Virtualbox with command lines. Virtualbox is able to run the same virtual machine file on Linux, Windows or Mac and even run inside another virtual machine on any of those. So I installed Windows in virtualbox on my local Windows machine, and then deployed it to the Linux server.
The initial Windows 7 installation resulted in a 15gb VDI file, but I was able to bring it down to 8gb with shrinking it, and then it compressed to just 3gb with 7-zip, which took only 5 hours to upload to the production server.
Remote desktop to virtualbox virtual machines make remote management easy!
Virtualbox has the ability to run a virtual remote desktop connection to the guests without needing the OS to be running. This VRDE technology lets you see the guest OS before it has even finished booting, which makes it easier to debug start-up issues compared to using RDP in Windows itself. It also prevents you from needing a GUI / desktop installed in Linux, which most server installation don't come with a desktop installed and it's not recommended to have a desktop on a server.
Check out my documentation on how to do it
I have the whole process documented in my server installation notes including steps for removing drives, adding cdroms and shrinking the virtual machine. I have published my documentation notes here, so you can setup a Windows guest in Linux Virtualbox too!
Installing Virtualbox on Centos 6.x Linux with command line
Download virtualbox repo and put in /etc/yum.repos.d/virtualbox.repo
# Now you can install it in the Linux host
yum install VirtualBox-4.2
This extension is needed for remote desktop to work - download the file into Linux host:
# install the extension (you can delete the file afterwards)
VBoxManage extpack install /path/to/Oracle_VM_VirtualBox_Extension_Pack-4.2.6-82870.vbox-extpack
# I had to enable forwarding on ipv4 in Linux host in order for the Windows guest to share the public Internet:
# Add the following line to /etc/sysctl.conf
# restart the network
service network restart
#If virtualbox complains about kernel drivers, then run these:
yum install kernel-headers kernel-devel gcc
Installing Windows 7 in Virtualbox with VBOXManage command lines
I recommend you install Windows in virtualbox on your Windows or Mac system using the virtualbox GUI to make it easier and faster since your OS install disc is probably running on your local machine and it would be slow to upload it to the production server which is probably hosted remotely. The steps below are useful when migrating that VDI and virtual machine configuration to the Linux server that has no desktop/GUI installed.
Make sure to update /path/to/ below to be a path on your system.
# Find the OS name you are trying to install
VBoxManage list ostypes
# Create the virtual machine
VBoxManage createvm --name "Windows7" --ostype Windows7 --register
# size is in MB
VBoxManage createhd --filename "/path/to/windows7.vdi" --size 30000
VBoxManage storagectl "Windows7" --name "Sata1" --add sata --controller IntelAHCI --sataportcount 3 --bootable on
VBoxManage storageattach "Windows7" --storagectl "Sata1" --type hdd --medium "/path/to/windows7.vdi" --port 0
VBoxManage storageattach "Windows7" --storagectl "Sata1" --type dvddrive --medium host:/dev/cdrom --port 1
# Use this command when installing guest additions (Makes guest os faster and more integrated with host)
VBoxManage storageattach "Windows7" --storagectl "Sata1" --type dvddrive --medium additions --port 1
# enable nat network (host and guest can't see each other) - we use shared folders instead.
VBoxManage modifyvm "Windows7" --nic1 nat --nicpromisc1 deny
# add a shared folder
VBoxManage sharedfolder add "Windows7" --name "shared-folder" --hostpath "/path/to/shared-folder/"
# remove a shared folder
VBoxManage sharedfolder remove "Windows7" --name "shared-folder"
# give the virtual machine more cpus / memory and hardware assisted virtualization if possible
VBoxManage modifyvm "Windows7" --memory 1024 --cpus 4 --ioapic on --hwvirtex on --vtxvpid on --nestedpaging on
# set that back if virtual machine fails to start
VBoxManage modifyvm "Windows7" --memory 1024 --cpus 1 --ioapic off --hwvirtex off --vtxvpid off --nestedpaging on
# open access to the virtual machine via RDP protocol. Make sure the IP below matches an IP you can access. I.e., make it a public IP if necessary - Note: you may was to read more about security in the virtualbox documentation here: http://www.virtualbox.org/manual/ch07.html#vboxheadless
VBoxManage modifyvm "Windows7" --vrdeaddress 127.0.0.1 --vrde on --vrdeport 5889 --vrdeauthtype null --vrdemulticon on
# starting the virtual machine without a GUI / Desktop installed
VBoxManage startvm "Windows7" --type headless
# shutting down the virtual machine
VBoxManage controlvm "Windows7" poweroff
Miscellaneous commands that were helpful
# output the virtual machine's configuration
VBoxManage showvminfo "Windows7" | more
#remove hard from vm (If you make mistakes / need to update it later)
VBoxManage storageattach "Windows7" --storagectl "Sata1" --type hdd --medium none --port 0
#remove from virtualbox AND delete disk file
VBoxManage closemedium disk "/path/to/windows7.vdi" --delete
# insert OS install disc in host and allow it to run in virtual machine
VBoxManage openmedium dvd /dev/cdrom
VBoxManage modifyvm "Windows7" --dvd /dev/cdrom
# after install, detach the cdrom
VBoxManage modifyvm "Windows7" --dvd none
Shrinking the vdi file for a Windows Guest:
# defragment and move all the data to the beginning of the partition. I used http://www.mydefrag.com/ in the Windows guest. It was a free app that uses the Windows defrag API so they say it is relatively "safe" to use.
# download sdelete.exe into the windows guest.
# run command in windows guest - It zeros the free space and cleans it - Note: this will expand the disk to use 100% of the space you allocated, so be careful not to run out of space!
sdelete -p 1 -z -c c:
# shutdown the windows guest and run this command on host - the new file will be approximately the same size as the used space within the Windows guest.
VBoxManage modifyhd --compact "/path/to/Windows7.vdi"
VBoxManage clonehd "/path/to/Windows7.vdi" "/path/to/Windows7smaller.vdi" --variant Standard
There you have it, Virtualbox can be easily used without a GUI!
Bookmark & Share
Most Popular Articles
- Mass virtual hosting security tip when using a reverse proxy to connect to other servers
- Solution for MariaDB Field 'xxx' doesn't have a default value
- How to lock Windows immediately upon smart card removal
- Stop using sleep mode with Windows Bitlocker for better security. Learn how to use hibernate in Windows 8.
- Planning a system to visually create responsive data-driven web page layouts & widgets in the Jetendo CMS browser interface
- Is Google Public DNS actually better then your ISP?
- My dog survived eating a box of Oreos
- Pros and Cons of CFML vs PHP and other languages