Skip to content

Instantly share code, notes, and snippets.

@simbo
Last active August 29, 2015 14:21
Show Gist options
  • Save simbo/a93ed83f3d74fda6db07 to your computer and use it in GitHub Desktop.
Save simbo/a93ed83f3d74fda6db07 to your computer and use it in GitHub Desktop.
Create a vagrant box

Create a vagrant box

Configure the virtual hardware

Create a new Virtual Machine in VirtualBox with the following settings:

  • Name: my-vagrant-box
  • Type: Linux
  • Version: Ubuntu64
  • Memory Size: 512MB (to taste)
  • New Virtual Disk: [Type: VMDK, Size: 40 GB]
  • Disable audio
  • Disable USB
  • Ensure Network Adapter 1 is set to NAT
  • Add this port-forwarding rule:
    • Name: SSH
    • Protocol: TCP
    • Host IP: blank
    • Host Port: 2222
    • Guest IP: blank
    • Guest Port: 22

Install the system

Mount the Linux Distro ISO, boot up and install.

Create a main user called vagrant with password vagrant. Set root password to vagrant (sudo passwd root).

Super user privileges for vagrant user

Add vagrant user to sudoers list:

sudo visudo -f /etc/sudoers.d/vagrant

Add this line, save and exit:

vagrant ALL=(ALL) NOPASSWD:ALL

Test if you still need a password for sudo:

sudo pwd

Update the operating system

Check for updates and bring everything up to date.

sudo apt-get update
sudo apt-get upgrade

Install the vagrant insecure key

mkdir -p /home/vagrant/.ssh
chmod 0700 /home/vagrant/.ssh
wget --no-check-certificate \
    https://raw.github.com/mitchellh/vagrant/master/keys/vagrant.pub \
    -O /home/vagrant/.ssh/authorized_keys
chmod 0600 /home/vagrant/.ssh/authorized_keys
chown -R vagrant /home/vagrant/.ssh

Install and configure openssh server

If not already installed, install openssh server now:

sudo apt-get install openssh-server

Find and uncomment the following line:

AuthorizedKeysFile %h/.ssh/authorized_keys

Restart openssh server:

sudo service ssh restart

Install VirtualBox guest additions

Install requirements for guest additions:

sudo apt-get install gcc build-essential linux-headers-server

Insert the guest additions image via Devices menu in VirtualBox, then mount and run:

sudo mount /dev/cdrom /mnt 
cd /mnt
sudo ./VBoxLinuxAdditions.run

Cleanup

Remove unnecessary and downloaded packages.

sudo apt-get autoremove
sudo apt-get clean

Fix fragmentation issues with the underlying disk to alloe more efficient compression later:

sudo dd if=/dev/zero of=/EMPTY bs=1M
sudo rm -f /EMPTY

Clear history and shutdown:

cat /dev/null > ~/.bash_history && history -c && sudo shutdown -h now

Package the box

Use vagrant package to package and compress your VirtualBox machine called my-vagrant-box:

mkdir ~/my-vagrant-box_package
cd ~/my-vagrant-box_package
vagrant package --base my-vagrant-box

Test the box

vagrant box add test package.box
vagrant init test
vagrant up
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment