Install VirtualBox with Ubuntu Desktop VM in bridge mode

My plan of purchasing HP N54L is having all services I deployed to ds213+ migrated to HP N54L. This allow my NAS become a centralized file server only. So I no longer need to worry my optware being corrupted by firmware update. You never know when will it break. And since I upgrade NAS every few years, I reinstall all those services like [openvpn, freeradius, openldap, bind, crontab, mysql, synopass, blah blah blah] periodically.

I am tired of these. Here are my plan:

Install Ubuntu server to HP N54L. Ubuntu server running no services, only run VirtualBox.

Then I run another Ubuntu VM on N54L. All services will run on the Ubuntu VM. I should have no more worry about switching hardware, crashing OS or whatever. All I need is to deploy my VM on the new VirtualBox and I should have all things back.

At the beginning I was going to run ESXi. But after reviewing the license things, I give up. I am going to use Virtual Box instead.

Here are my procedures to install VirtualBox with Ubuntu Desktop to my HP N54L running Ubuntu server.

Install VirtualBox

Open a terminal. Run the commands as root below to install.

# sudo echo "deb http://download.virtualbox.org/virtualbox/debian precise contrib" >> /etc/apt/sources.list
# sudo echo "deb http://download.virtualbox.org/virtualbox/debian oneiric contrib" >> /etc/apt/sources.list
# sudo echo "deb http://download.virtualbox.org/virtualbox/debian natty contrib" >> /etc/apt/sources.list
# sudo wget -q http://download.virtualbox.org/virtualbox/debian/oracle_vbox.asc -O- | sudo apt-key add -
# sudo apt-get update
# sudo apt-cache pkgnames | grep virtualbox

This will show a list of availble virtualbox. Install the latest one.

# sudo apt-get install virtualbox-4.3

Now download and install the extension pack. If you want to Remote desktop to the VM, you will need this. Note the version of extension pack, must be match the virtual box version that you installed above. Example below install 4.3.12.


# cd ~
# wget http://download.virtualbox.org/virtualbox/4.3.12/Oracle_VM_VirtualBox_Extension_Pack-4.3.12-93733.vbox-extpack
# sudo VBoxManage extpack install ./Oracle_VM_VirtualBox_Extension_Pack-4.3.12-93733.vbox-extpack

Create a new user vboxuser to run the virtualbox process.

# sudo adduser –ingroup vboxusers vboxuser

Now switch to the vboxuser, do not run following commands as root.

# sudo su vboxuser

Create Ubuntu VM

Adjust the parameter(s) to fit your need. Sample below create a VM named ‘deadcode’ with 1GB ram. My network adaptor is named em1 instead of eth0. Check your own setting by running ifconfig under shell.

Below is the config of the new VM

  • 1024MB RAM
  • One Intel network adapter, bridged with the LAN card of HPN54L.
  • One hard drive with 50GB
  • One CDROM mounted with Ubuntu Desktop 14.04.2 ISO

# cd ~
# VBoxManage createvm --register --ostype Ubuntu --name "deadcode"
# VBoxManage modifyvm “deadcode” —memory “1024”
# VBoxManage modifyvm “deadcode” —nic1 bridged —bridgeadapter1 ‘em1’
# VBoxManage modifyvm “deadcode” —boot1 dvd —boot2 disk
# VBoxManage storagectl “deadcode" --name "SATA Controller" --add sata --bootable on
# VBoxManage storagectl "deadcode" --name "IDE Controller" --add ide --bootable on
# VBoxManage createhd --filename “/home/vboxuser/VirtualBox VMs/deadcode/disk1" --size 50000 --format VDI --variant Standard
# mkdir ~/iso
# wget http://releases.ubuntu.com/14.04.2/ubuntu-14.04.2-desktop-i386.iso
# VBoxManage storageattach "deadcode" --storagectl "SATA Controller" --port 0 --device 0 --type hdd --medium “/home/vboxuser/VirtualBox VMs/deadcode/disk1.vdi”
# VBoxManage storageattach "deadcode" --storagectl "IDE Controller" --port 0 --device 1 --type dvddrive --medium “/home/vboxuser/iso/ubuntu-14.04.2-desktop-i386.iso"
# VBoxManage modifyvm "deadcode" --vrdeport 3389 --vrde on

Now the VM is created and configured, start it.

# VBoxHeadless –startvm “deadcode” –vrde config &

If success, following will be printed to console.

Oracle VM VirtualBox Headless Interface 4.1.22
(C) 2008-2012 Oracle Corporation
All rights reserved.Controlling the Virtual Machine


In order to allow devices in the network to reach the VM, a static route is needed to add to the broadband router. First grab the bridged adapter ip address.


Run ifconfig, looks for virbr0 entry.

# ifconfig

virbr0    Link encap:Ethernet  HWaddr xx:xx:xx:xx:xx:xx  
          inet addr:192.168.122.1  Bcast:192.168.122.255  Mask:255.255.255.0
          UP BROADCAST MULTICAST  MTU:1500  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)


The ip address is 192.168.122.1.


Add a static route


Network: 192.168.122.1
Gateway: 192.168.1.111 (ip address of HP N54L)

Web Interface (phpvirtualbox)

A great php management interface is provided by a great open source package phpvirtualbox.

To install it

Run the commands below as root

# sudo sh -c “echo “VBOXWEB_USER=vboxuser” >> /etc/default/virtualbox”
# sudo service vboxweb-service restart
# sudo apt-get install zip

Run the commands below as vboxuser

# sudo su vboxuser
# cd ~
# wget http://phpvirtualbox.googlecode.com/files/phpvirtualbox-4.1-10.zip
# unzip phpvirtualbox-4.1-10.zip
# rm phpvirtualbox-4.1-10.zip
# cd phpvirtualbox-4.1-10
# cp config.php-example config.php

Edit config.php, edit the user and password at lines 12 and 13.

# vim config.php

Create a symbolic link to apache.

# sudo ln -s /home/vboxuser/phpvirtualbox-4.1-10 /var/www/html/phpvirtualbox

Now you manage all the VM using a browser.

http://192.168.1.111/phpvirtualbox

Default username and password is admin/admin.

Create an auto start script to startup Virtualbox after server reboot

# sudo vim /etc/init.d/autostartvms.sh

Copy and paste the content below

#! /bin/sh
########################################
#### THIS IS A DEBIAN INIT.D SCRIPT ####
########################################
 
### BEGIN INIT INFO
# Provides:          autostartvms.sh
# Required-Start:    $all
# Required-Stop:     $all
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Start daemon at boot time
# Description:       Enable service provided by daemon.
### END INIT INFO
 
# The location of the file containing the list of vms to start
CONFFILE=/home/vboxuser/autostart.txt
# The name of the user running the vms
VMUSER=vboxuser
 
HOMEDIR=`eval "echo ~$VMUSER"`
 
get_vm_state()
{
        RC=0
        if [ ! -d "$HOMEDIR/VirtualBox VMs/$LINE" ]; then
                STATE="missing"
                RC=1
                return
        fi
        INFO=$(sudo -H -u $VMUSER /usr/bin/VBoxManage showvminfo "$VMNAME" 2>/dev/null)
        if [ $? = 0 ]; then
                STATE=$(echo "$INFO" | grep State | awk '{ print $2; }')
        else
                STATE="error"
                RC=1
        fi
}
 
case "$1" in
  start)
        echo "Starting VirtualBox VM..."
        cat $CONFFILE | while read LINE
        do
                VMNAME=$LINE
                get_vm_state
                if [ $STATE = "running" ]; then
                        echo "$VMNAME is already running"
                elif [ $RC = 0 ]; then
                        sudo -H -u $VMUSER /usr/bin/VBoxManage startvm "$VMNAME" -type vrdp
                else
                        echo "$VMNAME is $STATE"
                fi
        done
    ;;
  stop)
        echo "Saving state of Virtualbox VM..."
        tac $CONFFILE | while read LINE
        do
                VMNAME=$LINE
                get_vm_state
                if [ $STATE = "running" ]; then
                        echo "Stopping $VMNAME:"
                        sudo -H -u $VMUSER /usr/bin/VBoxManage controlvm "$VMNAME" savestate
                else
                        echo "$VMNAME isn't running"
                fi
        done
    ;;
  status)
        echo "Virtual Machines state:"
        cat $CONFFILE | while read LINE
        do
                VMNAME=$LINE
                get_vm_state
                echo "\"$VMNAME\" is: $STATE"
        done
    ;;
  *)
    echo "Usage: /etc/init.d/autostartvms.sh {start|stop|status}"
    exit 1
    ;;
esac
 
exit 0

Once the script is created, run the commands below to install the script.

# sudo chmod +x /etc/init.d/autostartvms.sh
# sudo update-rc.d autostartvms.sh defaults 90

Create a text file. This text file contains a list of VM to be auto start during server reboot.

# vim /home/vboxuser/autostart.txt

Sample content of autostart.txt. One VM per line.

deadcode
your_other_vm
blah_blah_blah

Now you should be able to use Remote Desktop to connect to the new VM and continue the Ubuntu Desktop installation.

Use Remote Desktop to connect to the VM with ip 192.168.1.111.

If you have firewall on, you need to allow Remote Desktop traffic.

# sudo ufw allow proto tcp from 192.168.1.110 to 192.168.1.111 port 3389
# sudo ufw allow proto tcp from 192.168.1.110 to 192.168.122.1 port 3389

Suppose 192.168.1.110 is the macbook that I used to Remote Desktop to the VM.
192.168.1.111 is the Ubuntu server running the VM.
192.168.122.1 is the bridge adapter.

Notes on Remote Desktop

It does not works initially. I don’t know why it didn’t work. After a few VM restart, it start working… be patient if you encountered the same scenario… Remote desktop is the only trouble I encountered during the whole installation. And it suddenly works… and once it works, it just works well.

2015-04-09 Notes on OpenVPN

To allow openvpn clients to access devices on the network, following must be done:

  • Add route to router
  • Setup IP Masquerade on the VM
  • push “192.168.x.x 255.255.255.0” in server.conf
  • Enable Promiscuous Mode to the VM’s network adapter

Credit

I’m following the instructions below to perform my installation. You will find the procedures are pretty much the same. Some essential steps were not mentioned though, I try to make the procedures more complete for my future reference. Thanks a lot to the author of the original post. The restart script is particular helpful.

http://amirshk.com/blog/resources/auto_start_vms/

Installation is a bit long but once things works, it is very rewarding. Now I can run all sort of OS on my Ubuntu server.

Update 2015-05-02 Install Guest Addition

Guest addition must be installed in VM otherwise remote desktop size will be VERY small resolution and cannot control the Ubuntu Desktop at all. Also the mouse will not able to synchronize with your actual mouse pointer.

Must install guest addition for GUI remote control.

In the host server, run following command to download Guest Addition ISO

# sudo apt-get install virtualbox-guest-additions-iso

Once downloaded, mount the iso using phpvirtualbox for use with the VM.

The ISO is here

/usr/share/virtualbox/VBoxGuestAdditions.iso

Then ssh to VM as normal user, i.e. ray.

For Ubuntu Desktop, login the Ubuntu Desktop, open a file explorer, the Virtual CD of Guest Addition should be shown.

Open a terminal.

$ sudo su
# cd /media/ray/VBOXADDITIONS_4.3.26_98988
# ./VBoxLinuxAdditions.run
Verifying archive integrity… All good.
Uncompressing VirtualBox 4.3.26 Guest Additions for Linux…………
VirtualBox Guest Additions installer
Copying additional installer modules …
Installing additional modules …
Removing existing VirtualBox DKMS kernel modules …done.
Removing existing VirtualBox non-DKMS kernel modules …done.
Building the VirtualBox Guest Additions kernel modules …done.
Doing non-kernel setup of the Guest Additions …done.
Starting the VirtualBox Guest Additions …done.
Installing the Window System drivers
Installing X.Org Server 1.16 modules …done.
Setting up the Window System to use the Guest Additions …done.
You may need to restart the hal service and the Window System (or just restart
the guest system) to enable the Guest Additions.

Installing graphics libraries and desktop services components …done.

Update 2015-05-02 Auto start VM after server reboot – The fix

The auto start procedures somehow, not working. I managed to fix by the link below

http://askubuntu.com/questions/404665/how-to-start-virtual-box-machines-automatically-when-booting

  • Create the file /etc/default/virtualbox

    # virtualbox defaults file
    VBOXAUTOSTART_DB=/etc/vbox
    VBOXAUTOSTART_CONFIG=/etc/vbox/vbox.cfg

  • Create the file /etc/vbox/vbox.cfg

    default_policy=allow

  • Execute commands below

    # sudo chgrp vboxusers /etc/vbox
    # sudo chmod 1775 /etc/vbox
    # sudo su vboxuser
    $ VBoxManage setproperty autostartdbpath /etc/vbox
    $ VBoxManage modifyvm “Name of VM” –autostart-enabled on
    $ exit
    # sudo service vboxautostart-service restart

Update 2015-05-02 Google Authenticator for SSH

None of the guide actually works. Got to write down the full procedures myself.

  1. sudo apt-get install libpam-google-authenticator
  2. vi /etc/pam.d/sshd, add following line to the top of the file

    auth required pam_google_authenticator.so nullok

  3. vi /etc/ssh/sshd_config, add/edit the following lines

    RSAAuthentication yes
    PubkeyAuthentication yes
    AuthorizedKeysFile .ssh/authorized_keys
    #PermitRootLogin without-password
    PermitEmptyPasswords no
    ChallengeResponseAuthentication yes
    AuthenticationMethods publickey,keyboard-interactive
    PasswordAuthentication no

  4. This setup ssh to required both publickey and google authenticator code to login. Login as root is rejected. Plain account password login is disabled.



Leave a Reply

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