Start and connect to your VirtualBox VM with a simple script

The way I like to develop is to create specific virtual machines for my web development.

The chore is to start the machine, mount the file system and then open up your IDE. All a nuisance that takes time. So, I’ve taken to writing a simple Bash script that will do the grunt work for you. All you have to do is put in your settings, save it and run it!

This will mount the VM’s web folder onto your file system and let you know it’s connected. All you need to do is then start your IDE of choice! Continue reading Start and connect to your VirtualBox VM with a simple script

rsync and the Hard Link Limitation

I’ve been setting up a new backup regime on a hosted server that I’m helping to maintain.

One of the key issues right now is that it is there isn’t a solid provided backup regime, so I’ve spent the day playing around with different Linux backup solutions trying to get to the bottom of a simple, yet robust way of backing up.

I like rsync, but the problem is that you can’t use it’s hard linking feature over secure shell. As such, incremental backups are a pain.

I was about to give up on it, and work out a convoluted tar process when it occurred to me that the hard links might still be possible.

And to that end, I wrote this script: Continue reading rsync and the Hard Link Limitation

Tunnelling on the fly

I’ve just been tweaking with SSH. One of the common things that I have to do is reconfigure my SSH tunnels while I’m working remotely to gain access to desktops and the like.

In this situation, the cumbersome but easiest way always seemed to be to disconnect and change the command line. However, having used Putty I was fairly sure that there must be an easier way.


Changing the port forwarding in ssh is as easy, if not easier, than in Putty.

While you’re connected to the host, type a tilde (~) and an uppercase C.This will open the ssh shell. Just type in your port forwarding command like you would when you are typing in the command line option. eg:

<code>-L 3389:localhost:3389</code>

to tunnel an RDP socket to the machine that you are connected to.

And that’s it! If you’re stuck, type a question mark (?) and press ‘Enter’. This will show you the options available.

Access VirtualBox SSH and Web Server

One of the thing that differs VirtualBox from Microsoft’s Virtual PC is that VB puts guest on a subnet of the computer that you are working on. This means that it cannot be directly accessed from other computers on your network.

To enable access, you need to configure your computer to allow ports to be forwarded to the guest. Thankfully, this is quite straightforward with the VBoxManage tool, and Allister Sanchez covers it well here: Additions and SSH Access to a VirtualBox Guest.
I’m assuming that your Vitual Machine is called MyVBoxSystem. Replace this with the name of your VM.

To enable SSH:

On the host computer, run the following commands:

<code>VBoxManage setextradata MyVBoxSystem "VBoxInternal/Devices/pcnet/0/LUN#0/Config/ssh/HostPort" 22
VBoxManage setextradata </code><code>MyVBoxSystem</code><code> "VBoxInternal/Devices/pcnet/0/LUN#0/Config/ssh/GuestPort" 22
VBoxManage setextradata </code><code>MyVBoxSystem</code><code> "VBoxInternal/Devices/pcnet/0/LUN#0/Config/ssh/Protocol" TCP

This is essentially telling VirtualBox to map any access on port 22 (the host port) to 22 on the guest. On a Windows system, this is fine as you are unlikely to be running an ssh server. If you are though, just change the HostPort parameter to a port that you know is free.

To enable HTTP:

<code>VBoxManage setextradata </code><code>MyVBoxSystem</code><code> "VBoxInternal/Devices/pcnet/0/LUN#0/Config/http/HostPort" 80
VBoxManage setextradata </code><code>MyVBoxSystem</code><code> "VBoxInternal/Devices/pcnet/0/LUN#0/Config/http/GuestPort" 80
VBoxManage setextradata </code><code>MyVBoxSystem</code><code> "VBoxInternal/Devices/pcnet/0/LUN#0/Config/http/Protocol" TCP</code>

Once again, we’re passing TCP ports forward. If you already have a web server installed such as Apache or IIS, then you will probably need to change the HostPort to something like 8080.

Make sure that your VM isn’t running when you run these commands. To check that the settings are saved, you can run the following command:

<code>VBoxManage getextradata MyVBoxSystem enumerate</code>

Alternatively, open up the VM .xml file which is found in .VirtualBox/Machines/MyVBoxSystem/MyVBoxSystem.xml. The settings are found in the ExtraData node:

&lt;ExtraDataItem name="VBoxInternal/Devices/pcnet/0/LUN#0/Config/apache/HostPort" value="8080"/&gt;
&lt;ExtraDataItem name="VBoxInternal/Devices/pcnet/0/LUN#0/Config/apache/GuestPort" value="80"/&gt;
&lt;ExtraDataItem name="VBoxInternal/Devices/pcnet/0/LUN#0/Config/apache/Protocol" value="TCP"/&gt;
&lt;ExtraDataItem name="VBoxInternal/Devices/pcnet/0/LUN#0/Config/ssh/HostPort" value="222"/&gt;
&lt;ExtraDataItem name="VBoxInternal/Devices/pcnet/0/LUN#0/Config/ssh/GuestPort" value="22"/&gt;
&lt;ExtraDataItem name="VBoxInternal/Devices/pcnet/0/LUN#0/Config/ssh/Protocol" value="TCP"/&gt;

Mounting a remote system with SSH

One of the cool things I’ve been doing lately is using secure shell to mount remote file systems. The great thing about this method is that you’re not dependant of explicitly creating shares as you are in Samba, and you don’t have to rely on repeated scp commands.

As long as you have the sshfs package installed, you should be able to mount the file system. Here’s an example to create a mount point on your desktop:

<code>mkdir ~/Desktop/sshmnt
sshfs user@target-pc:/home ~/Desktop/sshmnt/</code>

That’s it! Once finished you can unmount the folder with the fusermount command:

<code>fusermount -u ~/Desktop/sshmnt</code>

If you have trouble with unmounting a connection (because the target may be switched off), you can force the connection to be unmounted:

<code>fusermount -z -u ~/Desktop/sshmnt</code>