This post describes on how to create Vagrant Box from scratch. For this post, we will be using VirtualBox 6.0 as the provider to create Lubuntu-16.04-Desktop vagrant box.
Create the Virtual Machine.
Create a new Virtual Machine in VirtualBox using the following details.
- Name: Lubuntu 16.04.5 Desktop
- Type: Linux
- Version: Ubuntu (64-bits)
- Memory: 1024 MB
- Virtual Harddisk: 10 GB, VDI (Dynamically allocated)
- Network Adapter 1: Enabled as NAT
- Processors: 1 CPU
You might also want to disable USB controller and audio if it's not used.
Installing the Guest OS (Linux 16.04 Desktop)
In the newly created virtual machine, install linux OS from lubuntu-16.04.5-desktop-amd64.iso
During installation, use the following details for login setup.
- Full name: vagrant
- User name: vagrant
- Password: vagrant
- Computer name: lubuntu-16.04.5-desktop
Setting up the Guest OS (Linux 16.04 Desktop)
Configure vagrant user to use sudo without tty and password.
$ sudo visudo
Add or edit the following lines:
Defaults env_keep = "SSH_AUTH_SOCK"
vagrant ALL=(ALL) NOPASSWD:ALL # <= Place this at the end of file
Install and configure SSH access for vagrant user.
# Configure ssh access for user vagrant
$ mkdir -p /home/vagrant/.ssh
$ wget --no-check-certificate https://raw.githubusercontent.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 ssh server
$ sudo apt-get install openssh-server
Configure ssh server to use key authentication
# /etc/ssh/sshd_config
Port 22
PubKeyAuthentication yes
AuthorizedKeysFile %h/.ssh/authorized_keys
PermitEmptyPasswords no
Restart ssh service
$ sudo systemctl restart ssh
Installing VirtualBox Guest Tools
Guest Tools are used by Vagrant to create shares folder between Host and Guest OS.
In VirtualBox menu bar, select Devices > Insert Guest Additions CD image... to mount the Guest Additions CD.
$ cd /media/vagrant/VBox_GAs_6.0.4
$ sudo ./VBoxLinuxAdditions.run
$ cd ~
$ sudo umount /media/vagrant/VBox_GAs_6.0.4
Finalizing and clean up OS Guest
We need to make the box as small as possible by cleaning up the VM disk space.
# Remove APT cache
$ sudo apt-get clean
# 'Zero out' the drive
$ sudo dd if=/dev/zero of=/EMPTY bs=1M
$ sudo rm -f /EMPTY
# Clear Bash history
$ cat /dev/null > ~/.bash_history && history -c
$ sudo shutdown -h now
Package the Vagrant Box
Change into directory where you want to store this vagrant box and run vagrant package command. In this case, the VM name is Lubuntu 16.04.5 Desktop.
$ vagrant package --base "Lubuntu 16.04.5 Desktop" --output lubuntu-16.04.5-desktop.box
$ vagrant box add lubuntu-16.04.5-desktop lubuntu-16.04.5-desktop.box
Using the Vagrant Box
$ vagrant init lubuntu-16.04.5-desktop
$ vagrant up
$ vagrant ssh
References:
- Creating a Custom Box from Scratch
- How to Create a Vagrant Base Box from an Existing One
- How to build a Vagrant base box from a VirtualBox VM
Comments
Post a Comment