Monday, January 10, 2011

Ubuntu in Windows with VirtualBox

I've used Windows all my computing life, and will for the foreseeable future. However, for certain things, it's very useful, sometimes necessary, to have a computer running Linux available for use. I tried setting up a separate computer for this, but I goof off often enough that it wasn't working out.

Enter VirtualBox. It lets you set up virtual machines running "guest" operating systems within your "host" operating system. Someone's already made an excellent tutorial on setting up VirtualBox on Windows to run Ubuntu: http://www.psychocats.net/ubuntu/virtualbox

I am running Windows 7 as my host OS and Ubuntu 10.10 Desktop as my guest OS, with Virtual Box 4.0.


Virtual machines have been around for a while, and Windows 7 Professional actually includes a downloadable feature called XP Mode to run Windows XP on a virtual machine. VirtualBox makes it very easy though, the setup required to get to the above screenshot is minimal. Networking worked for me without any extra setup, and keyboard/mouse transitions are almost seamless.

One thing that isn't in the guide above is setting up Ubuntu's resolution to match the VirtualBox window size. It's pretty simple.
  1. On your virtual machine windowbar, select Devices -> Install Guest Additions...
  2. This puts the Guest Additions in the virtual machine's CD drive. Mount it within Ubuntu, by selecting Places -> VBOXADDITIONS_4.0.0_69151.
  3. A window will pop up showing the contents of the CD. There should be a button marked "Open Autorun Prompt". Click it, and let Guest Additions install.
  4. When done, "eject" the CD from the Devices menu, and restart the virtual machine. From now on Ubuntu will resize its resolution to the VirtualBox window size. There's also some other cool features available in the Machine menu, like Seamless Mode.
The last modification I did to my setup was to allow SSHing and SCPing to my Ubuntu machine. VirtualBox necessarily passes all network traffic through the host OS, so an attempt to SSH or SCP to the guest will look like an attempt to the Windows host, which won't work. The solution is to add a networking rule to forward that traffic to the guest. Since Windows doesn't have anything using port 22, we can keep it simple.
  1. Shutdown your virtual machine and close all VirtualBox windows.
  2. Open up a command prompt in Windows. Start -> Programs -> Accessories -> Command Prompt
  3. Execute the following command, replacing YOUR_VM_NAME with your virtual machine's name. This creates a rule called "ssh" for your virtual machine, forwarding host port 22 to guest port 22.
     > C:\Program Files\Oracle\Virtualbox\VBoxManage.exe modifyvm "YOUR_VM_NAME" --natpf1 "ssh,tcp,,22,,22"  
    
  4. Open VirtualBox and restart your virtual machine. You will probably get a Windows firewall alert, allow VirtualBox what it is asking. Your virtual machine can now accept SSH connections from your local network if you're behind a router, or from anywhere on the Internet if you're directly connected or in your router's DMZ. 
That's all the VirtualBox setup I've needed. Almost all the benefits of Linux without the drawbacks of actually switching over!

No comments:

Post a Comment