You can simply create an OVF applicance with VirtualBox, and then import this multiple times:
- 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
- Install an SSH server and upload your SSH key.
- On linux, run
sudo rm -rf /etc/udev/rules.d/ to remove the persistent network name associations (otherwise, you'll have to reconfigure networking).
- Shutdown the machine (you may want to remove some caches before in order to cut down on machine size).
- 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" \
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.