Skip to content
This repository has been archived by the owner on Jun 28, 2022. It is now read-only.

VM Setup: Setting up a Linux VM

pgoodman edited this page Sep 6, 2014 · 1 revision

Step 1: Getting a disk image.

Download a .iso file of your favourite Linux distribution. I usually go with Ubuntu Server Edition. The rest of this guide will assume that the downloaded file's name is distro.iso.

Step 2: Making a VM image.

Use the following command to create a 15 GiB VM image called vm.img:

qemu-img create -f qcow2 vm.img 15G

Step 3: Boot into your new VM image and install your distribution

Next, we need to boot into the VM and go through the Linux distribution's set-up process. To boot into the image, run:

$(which qemu-system-x86_64) -cpu host,level=9 -enable-kvm -m 2048 -hda vm.img -cdrom distro.iso -boot d

You will likely need to reboot your VM after walking through your distribution's installer. Closing the VM is sufficient.

Step 4: Telling Granary about vm.img

Granary has some convenient scripts for launching your VM, but it needs to know where to find the vm.img file. To tell Granary about this file, run the following command:

cd /path/to/Granary/
./scripts/make_qemu_img_link.sh vm.img

Step 5: Boot into your VM using Granary's launcher script

Run the following command:

cd /path/to/Granary/
./scripts/vmlaunch.sh

Step 6: Set up SSH.

Getting SSH

This assumes that your distribution uses aptitude. On both the host and guest, run the following command:

sudo apt-get install ssh openssh-server
Create a public/private keypair for your machine

Now, make sure that you've got a private key set up on your host machine. In case you don't, do the following:

ssh-keygen -t rsa -C "$USER@$HOST" -f ~/.ssh/id_rsa

This step will create two files under ~/.ssh/: your secret private key, id_rsa, and your shareable public key, id_rsa.pub. Be sure to never overwrite or lose your private key! In fact, why not set its permissions to read-only if ssh-keygen did not do so already:

chmod u=r,go= ~/.ssh/id_rsa
Setup your SSH client config

First, create the following directory on your host machine:

mkdir ~/.ssh/sessions/

Next, at the top of ~/.ssh/config (if it doesn't exist, then create it), add:

# The private key that is unique to your machine/account/person
IdentityFile ~/.ssh/id_rsa

# The first SSH session you initiate makes the connection and authenticates,
# and then any additional logins or file transfers will not need to repeat
# those steps as they will proxy through the already established control master
# connection
ControlMaster auto
ControlPath ~/.ssh/sessions/%h-%r-%p

For each VM or target host, add a section like this with the necessary user/host/port changes as applicable to your setup. In this case, we are associating the hostname slothvm with our VM. Any name can be specified here. We're also specifying root as the user so that we can avoid password prompts when loading Granary into the VM. You might need to enable root login on your guest VM. You might also need to set PermitRootLogin yes in /etc/ssh/sshd_config of the guest VM to enable remote root login.

Host slothvm
    User root
    Hostname localhost # Host -> Guest
    Port 5556
#    Hostname 10.0.2.2 # Guest -> Host
#    Port 22

    # For throw-away VM's, this is useful because otherwise any change to the
    # IP address of the hostname (due to DHCP, for example) will cause SSH to
    # be suspicious and ask for confirmation
    StrictHostKeyChecking no

Port 5556 is used because Granary's vmlaunch.sh script uses that port for communications going from the host machine to the guest VM.

At the end of your configuration file, also consider adding these as well:

# This adds these options for all hosts by default (the wildcard match)
Host *
    # Ensure we use the latest and most secure version of the SSH protocol
    Protocol 2
    VisualHostKey yes

    # Keep the master control connection alive indefinitely
    KeepAlive yes
    ServerAliveInterval 60
#    StrictHostKeyChecking yes
Authorize Each Host for Password-free Access

First, launch your VM with Granary's vmlaunch.sh script, as shown above. Then, ensure that your public key is authorized for each guest you setup. You will need to enter a password for the guest's root user during this step.

ssh-copy-id -i ~/.ssh/id_rsa.pub slothvm

Hereafter, you can simply ssh slothvm or scp somefile slothvm: or scp slothvm:otherfile otherfile and it will work without further authentication.

Making it easier to log in to your host from within your guest

Run the following command, or duplicate the above process but for your guest VM.

ssh-copy-id "<host-user-name>@10.0.2.2 -P 22"

In a typical setup, 10.0.2.2 is the IP of your host machine. In Granary's vmlaunch.sh script, TCP port 22 is manually specified as the port with which the guest VM communicates with the host machine. Therefore, if you are in the guest VM and wish to communicate with the host machine, you can do so with utilities like ssh with 10.0.2.2 as the hostname and 22 as the port number.