Replacing a Dev VPS with Linux on OSX

by Jean-Michel Lacroix on

Update: For a great discussion on the topic, see the Hacker News discussion page.

As much as I love my Mac, I'm sometimes having uncontrollable urges to install Linuxy stuff on it. Sometimes it works great (thanks mostly to homebrew), but at other times it's flaky or impossible.

So I've been looking for a Linux VPS, to have a Linux machine handy when I want to play around and try stuff. But, since I'm using QEMU (a lightweight virtualization tool) a lot at work, I tried using it as a background Linux virtual machine that's always available.

I now have a FREE, always-on (using negligible CPU and RAM when idle) Linux machine that is available whenever I need it, wherever I am. It's also quite useful to test tools so I don't mess with my stable environment: it's too easy to break the world when installing lots of stuff.

Installation

Download an ISO version of the distribution you want to install (in my case, the excellent Arch Linux) and fire up your favorite terminal.

Install QEMU 1:

$ brew install qemu

Create a Qcow2 image, that can expand up to 10GB:

$ qemu-img create -f qcow2 archlinux.qcow2 10G

Launch QEMU with your image attached as main hard drive and your ISO as cdrom:

$ qemu-system-x86_64 -m 512 -hda archlinux.qcow2 \
                     -cdrom archlinux.iso

Before shutting it down, make sure you enable the OpenSSH sever so you can log in later.

To access the VM via SSH from the local machine, boot it with a network redirection:

$ qemu-system-x86_64 -m 512 -hda archlinux.qcow2 \
                     -redir tcp:2222::22

This redirects all traffic from localhost port 2222 to VM port 22.

When it's done booting, try to connect to via SSH with the following command:

$ ssh username@localhost --port 2222

Once you get this working, shut it down.

Usage

There's many ways to start the machine, I use a bash function in my ~/.bashrc file to launch it when I need it:

function linux {
    qemu-system-x86_64 -m 512 -hda archlinux.qcow2 \
                       -redir tcp:2222::22 \
                       -nographic -daemonize
}

Notice the -nographic and -daemonize options: they ensure the machine is started as a background process.

For faster connection, add these settings in your ~/.ssh/config file:

Host linux
    Hostname localhost
    Port     2222
    User     jm

You can now connect and copy files to your new home easily with

$ ssh linux

and

$ scp file.txt linux:

For maximum fun, setup a shared folder on your local machine that your VM can access for easier file sharing. It's also a good idea to make a backup copy of your stable image so that you can replace a broken VM by a clean new one.

Have fun playing with your new development environment.

  1. There seems to be a problem with the BIOS in daemon mode in QEMU 0.15 on OSX, so make sure you install version 0.14.1.
    brew install https://raw.github.com/mxcl/homebrew/\
    bf2dd2bea04daf78a98888cf20fdf438fb777112/Library/Formula/qemu.rb