Default Browser Trick for Virtualized Guests

Author: Jeff Anderson

Long story short, I use a Windows virtual machine on my workstation at work. I use qemu/kvm, and I use the user-mode networking stack for simplicity in setting up the VM. This trick should work with just about any virtualization package out there.

I wanted to be able to click a link inside the VM and have my browser on my host Linux machine open the link. I have no reason to run another browser inside the VM, and figured that it'd be nice to get this set up.

My host workstation is Arch Linux, and I use Firefox. From the command line, if I'd like to open a linux in firefox, I can run firefox <URL>, where <URL> is where I want to go. If firefox is running this opens the link in a new tab. This only works if two environmental variables are set: DISPLAY and XAUTHORITY. I created a small script to find the currently running firefox, figure out the values of these variables, set its invironment as such, and then run firefox with any arguments that are passed in. Here's the script:

$(cat /proc/`pidof firefox`/environ | sed 's/\x00/\n/g' | grep '^DISPLAY=\|XAUTHORITY=' | sed 's/^/export /g') && firefox $@

Now I can ssh in from another machine, and run that script with the arguments I want to pass to Firefox (don't forget to make it executable!):

ssh host ./

This should open inside a new tab on the currently running firefox. Another way to grab the values would be to change pidof firefox to pidof fluxbox since I use fluxbox as my window manager. That way, if Firefox isn't running, it will be able to start up a new instance.

Now, to set up the default browser on my virtualized Windows machine. I always install Cygwin when I install Windows, and I use the Cygwin OpenSSH package with this trick. You should also be able to get this working just fine with plink.exe from the Putty suite of tools.

The first thing I need to do is generate a new SSH key that is passwordless. I made the filename of mine something like id_remotefirefox_rsa. (You can also use an ssh key with a key agent like ssh-agent or Putty's pageant, but you'll need to re-enter the password each time you start a new login session, before you can click on any links.) Put the public key into your authorized_keys file on the host. You'll want to put some special options on the key to lock down its access:

from="",no-pty,no-port-forwarding,no-X11-forwarding,no-user-rc,no-agent-forwarding ssh-rsa AAAAB3NzaC1yc2EAAAAD...eq+kuvGK1wvDowwFDh7 jeff@windows-guest

The from="" option is what I need in my case since I'm using user-mode networking, and I can use the IP address that qemu provides inside the usermode networking stack ( that's essentially an alias for the host machine's If you're using another networking mode, or another virtualization package, you'll need to update or remove this option to reflect your VM's configuration.

Now, I can test using my script from the VM. From a command prompt in Windows (run "cmd" from the Start..Run menu), run the following:

c:\cygwin\bin\ssh.exe -i /home/jeff/.ssh/id_remotefirefox_rsa jeff@ ./

This should result in your host's Firefox opening in a new tab.

Once you can do it manually from the command prompt, you can set the file association in Windows. In Windows XP, this is done from the "File Types" tab in the "Folder Options" dialog. You'll need to change this for "URL:HyperText Transfer Protocol" and "URL:HyperText Transfer Protocol with Privacy" which are the http and https protocols. You should also be able to do this for any protocol that your Host machine's Firefox supports. Highlight the protocol you want to change, and click "Advanced". Highlight the "open" option and click "Edit". The "Application used to perform action:" box should contain the path to your browser's .exe file, followed by some options, and a %1 where the URL will be inserted. I set mine to the following:

c:\cygwin\bin\ssh.exe -i /home/jeff/.ssh/id_remotefirefox_rsa jeff@ ./ -url "%1"

Also note, that since ssh.exe won't provide a DDE response, you'll need to uncheck the "Use DDE" box, otherwise you'll get messages in Windows about your links failing to open, even though they really did open just fine.

The process for setting this up on Windows Vista/7 will be similar, but you'll probably need to dive into the registry to add your custom command as a browser that Windows can use.

This script/hack is only at a "proof of concept" level, and would do well to have some sanity checking built in, and a method to fall back if it fails-- maybe save the URL to a text file, or use some notification method that it failed to do its job so the URL doesn't just disappear.

I'd love to see any improvements or variations. This concept should be adaptable for any guest OS, and the same idea can be re-implemented for other types of hosts.

Posted: Apr 18, 2011 | Tags: Virtualization Open Source Linux Firefox

Comments are closed.