Page 1 of 1

Running a VM using privileged ports on a Mac.

Posted: 22. Jul 2014, 13:02
by nphilly
The problem:
When running a VM using a network adapter configured to use NAT, you may find that you need to port forward privileged ports.

There are many reasons for this, mine in particular was; running an SSH server and a web server which use ports 22 and 80 respectively. At the same time using a bridged connection may not be an option for you, as you may have DNS pointing to your host machine.

On a Mac, this creates a problem because all ports up to 1024 are reserved and can only be opened with administrator privileges, meaning, if like me, you are using a normal user account on your Mac, opening any port under 1024 is not possible unless you do it as an administrator.

"Well thats simple, I will just run VirtualBox as an administrator and then Launch my VM" So I thought. When you do this, you will find that none of your VMs are available, as VirtualBox stores your info about your VMs in your local user account, which means, since you didn't create any VMs as administrator, VirtualBox won't find any.

The solution:
The trick here is to set the VBOX_USER_HOME environment variable to point to the path of your local user when running VirtualBox as an administrator. This will allow you to launch your VM using the vboxmanage command line tool, whilst logged into your Mac as a normal user and bind to privileged ports. Please look at the docs on the VBOX_USER_HOME environment variable for more details.

Now this can be a pain in the ass doing this from the command line. But Macs have something called the Automator which will allow you to script tasks with various actions and use AppleScript!

To make things easier, for me and hopefully for some of you, I have written a small piece of Automator AppleScript that will achieve the above without opening the terminal.

The code:

Code: Select all

on run {input, parameters}
	set promptTitle to "Choose a VM"
	set promptMessage to "Select a VM from the list to run as root"
	
	set pth to path to library folder from user domain as string
	set myUserLib to (POSIX path of pth) & "VirtualBox"
	
	set vms to do shell script "vboxmanage list vms"
	set vmList to paragraphs of vms
	set choices to {}
	
	repeat with _item in vmList
		set AppleScript's text item delimiters to " {"
		set vm_line to text item 1 of _item
		set end of choices to vm_line
	end repeat
	
	choose from list choices with title promptTitle with prompt promptMessage OK button name "Launch VM"
	
	set chosenVM to result
	
	if chosenVM is not false then
		do shell script "VBOX_USER_HOME='" & myUserLib & "' vboxmanage startvm " & chosenVM with administrator privileges
	end if
	
	return input
end run
You can run the above code by opening the "Automator" app, choosing a new "Application", in the Libarary, choose "Run Applescript" and then copy and paste the code above. Save this as something like "LaunchVM.app" to your desktop.

When you run LaunchVM.app, you should be presented with a menu listing all of your configured VirtualBox VMs (Similar to the image below). Select one and click "Launch VM" and your VM should just start as normal, allowing your services in the guest OS to bind to ports under 1024.

Image

If you don't want to go through the steps of creating the app in the Automator, you can download it here: https://www.dropbox.com/s/wnkpwunoh9rns ... VM.app.zip - (This is my personal dropbox, please don't abuse the link.)

I have only tested this on Mavericks, so I cannot guarantee how well it will work with other versions of OSX, but please let me know if it does!

Hope this helps :)