Installing Windows Server 2012 on Ubuntu 12.04 with VirtualBox 4.3

My current day job is basically working in numerous schools looking after their networks of computers. I’ve tried to minimise the amount of running computers in my house due to space and rising electricity costs. I have the ed.gs dedicated server hosted by FastHosts, so I thought it would be good to create a constantly accessible Windows machine for testing and other shenanigans that arise from working with Windows networks, taking advantage of the spare resources available on the server. I’ve used VirtualBox many times before, but only with its GUI; the ed.gs box is an Ubuntu server with no GUI, so it was time to get my hands dirty. Below is the simplified process I went through to create a Window Server 2012 R2 installation, using only the command line and a single publicly accessible IP address. Running all the commands should be enough to get a basic image up and running on most servers, as long as you have the VirtualBox deb files and Windows Server ISO ready on your server.

Note: Windows Server 2012 R2 is specifically supported by VirtualBox 4.3 and above.

Download the latest version of VirtualBox from their site @ https://www.virtualbox.org/wiki/Linux_Downloads. You could use apt-get if you wish, but as of writing this the latest version available was 4.1 from official apt repositories and 4.2 was the latest PPA release. The deb file on their site does the job nicely, so just use that. Here’s the one I downloaded for Ubuntu 12.04 x64.

wget http://download.virtualbox.org/virtualbox/4.3.0/virtualbox-4.3_4.3.0-89960~Ubuntu~precise_amd64.deb

We also need to download the VirtualBox Extension Pack to enable remote access for installation, it can be downloaded from the VirtualBox downloads page or from the link below for the 4.3 compatible version.

wget http://download.virtualbox.org/virtualbox/4.3.0/Oracle_VM_VirtualBox_Extension_Pack-4.3.0-89960.vbox-extpack

Download the Windows Server 2012 R2 ISO and put it somewhere you’ll remember like your home directory.

Install VirtualBox.

dpkg -i virtualbox-4.3_4.3.0-89960~Ubuntu~precise_amd64.deb

If all that went ok we should now be able to create the virtual machine, so let’s create it.

VBoxManage createvm --name "YourVMNameHere" --register

This creates the initial vbox file that we will be using throughout. Change the YourVMNameHere to anything you like, make it something memorable and possible easy to type as we’ll be using it a fair bit.

Next we need to create the virtual hard drive so we can install Windows Server and have some storage.

VBoxManage createhd --filename "YourHDNameHere" --size 102400

Again, change the YourHDNameHere to whatever you like and remember it, I kept mine the same as the virtual machine’s name. The –size attribute is the size of the hard drive in megabytes, 102400 is 100GB. It won’t create the 100GB file straight away, but it will allow you to fill it over time up to that size.

We have to set the OS type and add some RAM next.

VBoxManage modifyvm "YourVMNameHere" --ostype Windows2012_64 --memory 2048 --acpi on --ioapic on --vram 128 --accelerate3d off --nic1 nat --nictype1 82545EM --cableconnected1 on --audio alsa --audiocontroller ac97

As you can see we used the modifyvm command to change the “ostype” to Windows2012_64, R2 isn’t shown as an option currently, and memory to 2048mb of RAM. We also need to enable ACPI and IOAPIC so that Windows Server will boot, if you miss either of these you’ll get stuck at the initial Windows boot screen where it will tell you something is corrupt. vram is video ram, you can add as little or as much as you like, it shouldn’t matter as we will be accessing the server via remote desktop connection. We need to disable accelerate3d as it may not be supported. nic1 creates the network connection, we want to use NAT as we only have one IP and no DHCP/DNS set up to handle IPs for us, we don’t need to make any either, it just makes it more complicated. nictype is the network card, 82545EM is an Intel 1000mb/s Server card which will suit us fine. Then we flag the cableconnected1 parameter so the VM knows it’s connected. Finally we add some audio support with the last two parameters.

Now we need to create a storage controller so we can add the HDD and DVD drive.

VBoxManage storagectl "YourVMNameHere" --name SATA --add sata --controller IntelAhci --bootable on
VBoxManage storagectl "YourVMNameHere" --name IDE --add ide --controller PIIX4 --bootable on

This creates the HDD and DVD drives but we still need to attach them, so we can do that with the following.

VBoxManage storageattach "YourVMNameHere" --storagectl SATA --port 0 --device 0 --type hdd --medium "/location/of/YourHDNameHere.vdi"

The location will most likely be in your home directory e.g. /home/ed/VirtualBox\ VMs/YourHDNameHere.vdi.

VBoxManage storageattach "YourVMNameHere" --storagectl IDE --port 0 --device 0 --type dvddrive --medium "/location/of/your/WindowsServer2012R2.iso"

Before starting VirtualBox up we need to activate the Remote Desktop server that will start when the VM starts up. We need to activate the extension pack we downloaded earlier using the command below.

VBoxManage extpack install /path/to/Oracle_VM_VirtualBox_Extension_Pack-4.3.0-89960.vbox-extpack

Now we need to modify the VM to accept the remote connections and forward through all connections on the RDP port we’re using later on.

VBoxManage modifyvm "YourVMNameHere" --vrde on --vrdeport 3390 --vrdeaddress 0.0.0.0
VBoxManage modifyvm "YourVMNameHere" --natpf1 "winrdp,tcp,,3389,,3389"

If you’re using iptables, which I hope you are, you will also need to allow the connection through.

/sbin/iptables -A INPUT -p tcp -m tcp --dport 3390 -j ACCEPT

We can now start the VM.

VBoxHeadless --startvm "YourVMNameHere" -e "TCP/Ports=3390" &

You can now use the Microsoft Remote Desktop Connection tool to connect to the VM at your host server’s IP and port 3390 e.g. running

"mstsc 102.134.142.13:3390"

on Windows. There’s no username or password to connect.

Setup Windows Server as you wish. Enable remote desktop connections in Server Manager and disable the firewall for all connections.

Shutdown Windows Server and disable the vrde flag with the following.

VBoxManage modifyvm "YourVMServerName" --vrde off

Enable port 3389 in iptables. You may also want to add the line to your /etc/rc.local so it will recreate the firewall rule on system startup.

/sbin/iptables -A INPUT -p tcp -m tcp --dport 3389 -j ACCEPT

Create an init.d script so you can start and stop the VM at will. Edit all of the YourVMNameHere placeholders.

#! /bin/sh
# /etc/init.d/YourVMNameHere
# Edit these variables!

VMUSER=YourUsername
VMNAME="YourVMNameHere"

case "$1" in
  start)
    echo "Starting VirtualBox VM..."
    sudo -H -b -u $VMUSER VBoxHeadless --vrdp=off --startvm "$VMNAME"
    ;;
  stop)
    echo "Saving state of Virtualbox VM..."
    sudo -H -u  $VMUSER /usr/bin/VBoxManage controlvm "$VMNAME" savestate
    ;;
  poweroff)
    echo "Powering off Virtualbox VM..."
    sudo -H -u  $VMUSER /usr/bin/VBoxManage controlvm "$VMNAME" poweroff
    ;;
  *)
    echo "Usage: /etc/init.d/StartVM {start|stop|poweroff}"
    exit 1
    ;;
esac

exit 0

Add the init.d script to run at startup and stop before the rest of the server shuts down.

update-rc.d YourVMNameHere defaults 99 01

You can now start/stop/poweroff the server using the following commands

service YourVMNameHere start
service YourVMNameHere stop
service YourVMNameHere poweroff

Have fun, as always if anything isn’t working or I’ve made a mistake let me know in the comments or contact page.

9 Comments

  • This is a very comprehensive explanation of how to setup the Windows Server on Ubuntu headless box. I tried with other guides and never got the job completed. Excellent guide. Thanks for this.

  • Hi Ed,

    Thanks for this guide but i got a question. Hope you can answer it.

    I need to add Host-Only adapter because i want to play in gameservers from my windows.

    I got ubuntu 14.04 and i follow this guide to install windows server 2008 r2. All it’s ok but the ip is not the same as my dedicated server.

    But if i use windows remote desktop i use the real ip (dedicated server). If i type “ipconfig” on Windows i get other ip 10.0.2.15 instead my ip’s dedicated 37.49.59.xxx

    So, how i can add that ip for my windows?

    I was searching and the solution it’s add host-only networking?

    Hope you can help me.

    Regards

    • I can’t fully remember if mine just worked on the dedicated server IP with the use of IPTables as I don’t use the VM anymore, but if I remember correctly all I’d ever done with it was redirect a port to the VM ip address. I’m sure there’s a way to create a hostonly adapter with vboxmanage, just make sure you set all the nictype etc. As you mentioned on Twitter, you were trying to use nic1 and nic2, did you need both? Try it with just the hostonly on nic1.

  • Hello
    When i use the scrypt i have got an error :
    /etc/init.d/Test7: 6: /etc/init.d/Test7: Syntax error: newline unexpected

    the line number 6 is : ใ€€start)

    What is wrong?

    • Sorry about that, it appears that some HTML code got into the code on the actual page. I’ve corrected it, so if you try and copy and paste it in again now, changing the parts that need changing, it should work for you now.

  • Hello
    Thanks now it’s ok.

    But how can i start the service background as screen?
    beacouse now i must connect by ssh start service and i cant close my ssh connection beacouse my VirtualBox machine go off.

    Thanks for replay.

    • The service will continue to run if you leave your SSH session; I think there’s a bug in the init.d script that makes it appear to run in the foreground, but you’ll be fine to leave your session.

Leave a Reply

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

css.php