Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

On Amazon EC2 or other cloud/IaaS services, I can easily create a new machine without much installation required. Basically, I just select RAM/HDD/CPU and provide a public SSH key and get a brand-new machine within seconds.

How can I replicate this locally, with VirtualBox (or any other virtualization software)? If possible, I'd prefer a solution that needs minimal modification of the actual system.

share|improve this question
add comment

2 Answers

You can use pre-built virtual machines to get the same effect. Create, customize and then save as your base template. Just as you'd use an empty office document with your preferred settings (fonts, page setup, etc), so you can create a virtual machine (in any virtualization technology) that you can easily copy and customized when you need a new instance. Over the years, I have a library of base-* OS for different versions and flavors of Linux, Windows, and Macintosh.

share|improve this answer
    
The linked pre-built machines seem fairly restricted to Oracle only. I added a detailed instruction with a script to document how one can configure ssh as well. –  phihag Feb 26 '13 at 6:13
add comment

You can simply create an OVF applicance with VirtualBox, and then import this multiple times:

  1. Create the VM with all your preferences - install the software you like (say, htop) and configure it to your wishes (for example, enable passwordless sudo, set UseDNS No in /etc/ssh/sshd_conf to speed up login, and a smaller timeout in /boot/grub/grub.cfg).
  2. Install an SSH server and upload your SSH key.
  3. On linux, run sudo rm -rf /etc/udev/rules.d/ to remove the persistent network name associations (otherwise, you'll have to reconfigure networking).
  4. Shutdown the machine (you may want to remove some caches before in order to cut down on machine size).
  5. Export the VM, either through the GUI with File -> Export Appliance or with VBoxManage export VM_NAME -o ./myvm.ova

You can now import the created ova file as often as you want (With File -> Import Applicance or VBoxManage import. Every time you do that, VirtualBox will generate a new VM name and allow you to change the basic parameters (CPU, RAM size, attached disks, etc.).

You will, however, manually have to edit the ssh port forwarding rule. You can easily do so with the following script:

OVA=vm.ova # Change this to the name of your ova file
VM_NAME=$(VBoxManage import "$OVA" | sed -n 's#.*VM name "\(.*\)".*#\1#p')
sshport=$(python -c 'import random;print(random.randint(1025,65535))')
VBoxManage modifyvm $VM_NAME --natpf1 ssh,tcp,127.0.0.1,$sshport,,22
/bin/echo -e "\nHost vbox-$VM_NAME\n\tHostName localhost\n\tPort $sshport\n" \
          >>~/.ssh/config
echo "Start the VM with    VBoxManage startvm $VM_NAME"
echo "Then, type   ssh vbox-$VM_NAME   to log in to your VM"

You can now ssh into the machine with ssh vbox-$VM_NAME. If you want to deploy the machine, you must change the manually change the SSH server keys.

Instead of exporting and importing, you can also just clone an existing machine.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.