Using vagrant to build virtualbox

Using vagrant to build environment

Vagrant is a popular tool in recent years, through which we can automatically create a running environment and realize infrastructure as code. vargrant has the following great movies.

  • Convenient sharing environment By sharing the code needed to build the environment, anyone can rebuild the same environment
  • Transparent creation process By looking at the code, you can easily see how and by which the local development environment is built
  • Convenient maintenance As long as you modify the code, you can maintain the environment. The code is the specific operation steps of the construction environment, so there is no inconsistency between the operation steps and the environment information.

vagrant is a virtual organization building tool provided by Hashicorp. It can automatically generate the virtual machine environment you want.

  • Create operating system
  • Configure the system (host name, network address)
  • Environment configuration after system installation (build middleware or deploy application)

Basic users of Vagrant

In the installation of vagrant, VirtualBox, Hyper-V and Docker are supported by default. In addition to these default types, we can also provide support for other platforms by installing corresponding services. In the next case, we will use VirtualBox for corresponding explanation. We can visit virtualbox official website Click Downloads to enter the download interface, and select the corresponding platform to download and install.

Vagrant can access Download Interface , to select the corresponding platform to download.

New Vagrant We can generate a project directory of Vagrant by the following command

mkdir demo
cd demo
vagran init

After execution, the system will return the following output

Vagrantfile has been placed in this directory. You are now ready to vagrant up your first virtual environment! Please read the comments in the Vagrantfile as well as documentation on for more information on using Vagrant.

A file named "Vagrantfile" will also be created under the current directory Now let's open this file and update it to the following

# -*- mode: ruby -*-
# vi: set ft=ruby :

# All Vagrant configuration is done below. The "2" in Vagrant.configure
# configures the configuration version (we support older styles for
# backwards compatibility). Please don't change it unless you know what
# you're doing.
Vagrant.configure("2") do |config| = "centos/7"
  config.vm.hostname = "demo" :private_network, ip: ""
  config.vm.synced_folder ".", "/home/vargrant/sync" ,disabled: true

From Vagrant.configure("2") do |config | to end, the virtual machine is set accordingly. The number 2 indicates the current version of vagrant. represents the images used by the current virtual machine, which are provided by Hashicorp. If we want other virtual reality, we can use Hashicorp website To choose what you want to mirror. config.vm.hostname represents the current hostname private network, IP: "" specifies the current host network number

After changing the configuration file, we can run it with the following command > vagrant up

The output of this command is as follows.

>wcs[@iMac](  ~/git/devops/vargrant-demo1  proxychains4 vagrant uplibproxychains4.dylib
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Box 'centos/7' could not be found. Attempting to find and install...
    default: Box Provider: virtualbox
    default: Box Version: >= 0
==> default: Loading metadata for box 'centos/7'
    default: URL:
==> default: Adding box 'centos/7' (v1905.1) for provider: virtualbox
    default: Downloading:
==> default: Box download is resuming from prior download progress
    default: Download redirected to host:
==> default: Successfully added box 'centos/7' (v1905.1) for 'virtualbox'!
==> default: Importing base box 'centos/7'...
==> default: Matching MAC address for NAT networking...
==> default: Checking if box 'centos/7' version '1905.1' is up to date...
==> default: Setting the name of the VM: vargrant-demo1_default_1585058574248_24020
==> default: Fixed port collision for 22 => 2222. Now on port 2250.
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
    default: Adapter 1: nat
    default: Adapter 2: hostonly
==> default: Forwarding ports...
    default: 22 (guest) => 2250 (host) (adapter 1)
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
    default: SSH address:
    default: SSH username: vagrant
    default: SSH auth method: private key
    default: Vagrant insecure key detected. Vagrant will automatically replace
    default: this with a newly generated keypair for better security.
    default: Inserting generated public key within guest...
    default: Removing insecure key from the guest if it's present...
    default: Key inserted! Disconnecting and reconnecting using new SSH key...
[proxychains] Strict chain  ...  ...  ...  OK
==> default: Machine booted and ready!
==> default: Checking for guest additions in VM...
    default: No guest additions were detected on the base box for this VM! Guest
    default: additions are required for forwarded ports, shared folders, host only
    default: networking, and more. If SSH fails on this machine, please install
    default: the guest additions and repackage the box to continue.
    default: This is not an error message; everything may continue to work properly,
    default: in which case you may ignore this message.
==> default: Setting hostname...
==> default: Configuring and enabling network interfaces...
==> default: Rsyncing folder: /Users/wcs/git/devops/vargrant-demo1/ => /vagrant

//When we open virtualbox, we will find that the system has automatically created the corresponding virtual machine.

//We can enter the virtual machine by running the following command
>wcs[@iMac](  ~/git/devops/vargrant-demo1  vagrant ssh                                                                     127 ↵  5479  22:13:14
[vagrant[@demo]( ~]$

View the host name of the virtual machine

> [vagrant@demo ~]$ uname -n demo

View the virtual ip through the following command

> [vagrant[@demo]( ~]$ ip addr show dev eth1
3: eth1: <broadcast,multicast,up,lower_up> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 08:00:27:be:15:92 brd ff:ff:ff:ff:ff:ff
    inet ** brd** scope global noprefixroute eth1
       valid_lft forever preferred_lft forever
    inet6 fe80::a00:27ff:febe:1592/64 scope link
       valid_lft forever preferred_lft forever
[vagrant[@demo]( ~]$

Stop the corresponding virtual machine By running vagrant halt, you can stop the virtual machine

wcs[@iMac](  ~/git/devops/vargrant-demo1  vagrant halt                                                                 
==&gt; default: Attempting graceful shutdown of VM... 

Delete virtual machine

Delete the newly created virtual machine by running vagrant destroy

wcs[@iMac](  ~/git/devops/vargrant-demo1  vagrant destroy      
&gt; default: Are you sure you want to destroy the 'default' VM? [y/N] y
==&gt; default: Destroying VM and associated drives...

The above explains the basic operation of vagrant, but also sees its convenience. It can be developed once and used many times, and a command can create a corresponding environment with one click.

vagrant environment 2

In addition to being able to configure the environment, vagrant can execute the corresponding shell script through the configuration file to realize the configuration of the application environment.

1# -*- mode: ruby -*-
2# vi: set ft=ruby :
4# All Vagrant configuration is done below. The "2" in  Vagrant.configure
6# configures the configuration version (we support older styles for
7# backwards compatibility). Please don't change it unless you know what
8# you're doing.
9Vagrant.configure("2") do |config|
9 = "centos/7"
10  config.vm.hostname = "demo"
11 :private_network, ip:""
12  config.vm.synced_folder ".", "/home/vagrant/sync", disabled:true
13  config.vm.provision "shell", inline: $script
14 end
15 $script = &lt;<script 16 yum -y install epel-release 17 nginx 18 echo "hello, vagrant"> /usr/share/nginx/html/index.html
19  systemctl start nginx

config.vm.provision on line 13. Can automatically execute 15-20 corresponding shell script, which mainly completes the following functions.

  • Install EPEL (Enterprise Linux add-on package) and nginx
  • Replace the contents of index.html file
  • Start nginx

There are two ways to execute the provision in vagrant

  1. reconstruction
    1. vagrant destroy delete old virtual machine
    2. Modify the Vagrantfile file
    3. Run vagrant up to build a new virtual machine
  2. Update old virtual machine
    1. Execute the vagrant provision to directly update the original virtual machine

After execution, we can verify whether the service is running normally by running curl. Through the above command, we found that vagrant did help us install nginx and modify the files while creating the virtual machine.


Tags: Operation & Maintenance CentOS network VirtualBox ssh

Posted on Wed, 25 Mar 2020 13:36:15 -0400 by Goose