-
Notifications
You must be signed in to change notification settings - Fork 6
Building Custom Vagrant box
Before going into building vagrant box. A little bit introduction about vagrant-
Vagrant is a tool for creating virtual test environments. By just three commands you will have your vagrant box set up and running.
vagrant init precise32
vagrant up
vagrant ssh
You just need 2 software
-
Virtualbox — If you dont have it installed you can do it by
sudo touch /etc/apt/sources.list.d/virtualbox.sources.list echo "deb http://download.virtualbox.org/virtualbox/debian xenial \ contrib" >> sudo /etc/apt/sources.list.d/virtualbox.sources.list wget -q https://www.virtualbox.org/download/oracle_vbox_2016.asc \ -O- | sudo apt-key add - wget -q https://www.virtualbox.org/download/oracle_vbox.asc \ -O- | sudo apt-key add - sudo apt update sudo apt install virtualbox-5.2
-
Vagrant — You can easily download it simply by
sudo apt install vagrant
We are going to build a vagrant box from scratch. In the end, we will have a package.boxfile it will have all the necessary components required to start a vagrant machine.
So let’s get started
First, we are going to create a virtual machine. For this tutorial, we are going to create a vagrant box for Linux mint Xfce. With following settings-
- Download Linux mint iso from here-https://www.linuxmint.com/download.php
- Open VirtualBox click new
- Set name
linuxmint-19-xfce-32bit
Type and version will be automatically set for you. - Set memory
1024 mb
- Virtual hard disk size
10 gb
. Hard disk file typevmdk
Then you need to go to settings of the VM
- Disable audio
- Disable USB ports
- Enable port forwarding. Go to
file > Preferences > Network
- Click on the green add icon to create a new network
- Then go to its settings
- Then in port forwarding set this rule
[Name: SSH, Protocol: TCP, Host IP: 127.0.0.1, Host Port: 2222, Guest IP: <VM IP address>, Guest Port: 22]
- Create the virtual machine with the help of ISO we downloaded
- Set vagrant user and password also vagrant
Open up the terminal and do a
sudo passwd root
It will prompt for the password twice. It is recommended to set the password to vagrant
Now we need to add vagrant user to sudoers list. For that, you need to sudo nano /etc/sudoers.d/vagrant
Add these following lines
# add vagrant user vagrant ALL=(ALL) NOPASSWD:ALL
Press CTRL+O
to save and then CTRL+X
to exit in nano
This vagrant insecure key is required for communication of Host machine to vagrant box. Don’t worry if the machine is insecure. As soon as you do a vagrant up, The vagrant will automatically replace this key with a new key pair for better security. Install this vagrant key by executing these commands on the terminal
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
It is recommended to update and upgrading before packaging the box
sudo apt update --yes
sudo apt upgrade --yes
apt install --yes openssh-server
sudo sed -i /etc/ssh/sshd_config -e \
"/#Author*/ c AuthorizedKeysFile %h/.ssh/authorized_keys"
sudo service ssh restart
sudo apt install --yes gcc dkms build-essential \
linux-headers-server
These guests tools are required. If this you forget to install these the synced folders will not work. However, there is a plugin in vagrant vagrant-vbguest
that can automatically install these. This plugin can also be useful if the vagrant box by default does not come with this plugin preinstalled.
Take a look that this for example — https://github.com/fujimakishouten/vagrant-boxes/issues/1
Inside VM window go to Devices > Insert Guest Edition CD image…
It should open up a disk File manager. Copy the path. It should be in the format of /media/vagrant/VBox_GAs_x.x.x/
. In my case it is 5.2.8
. You just have to add VBoxLinuxAdditions.run after the path. So that it looks something like this-
sudo /media/vagrant/VBox_GAs_5.2.8/VBoxLinuxAdditions.run
This is the best way to this. Otherwise, you would have to find the version and LinuxAddition from here manually(not recommended).
Then you should restart the box.
Congratulations we have almost created the vagrant box. Now we just have to create a package.box file. We can do it with the help of vagrant packagecommand. Create a linuxmint-19-xfce-32bit folder in your home directory
mkdir ~/linuxmint-19-xfce-32bit
cd ~/linuxmint-19-xfce-32bit
vagrant package --base linuxmint-19-xfce-32bit
==> mint: Attempting graceful shutdown of VM...
==> mint: Forcing shutdown of VM...
==> mint: Exporting VM...
==> mint: Compressing package to: /home/pc/linuxmint-19-xfce-32bit/package.box
Then you need to add this box and then initialize. You can do it by
vagrant box add linuxmint-19-xfce-32bit package.box
vagrant init linuxmint-19-xfce-32bit
vagrant up
Now you can ssh into the box by
vagrant ssh
Now you can use this like every other vagrant box. Congratulations! We have created a vagrant box from complete scratch.