How to build a virtual machine with vagrant+virtualBox

How to build a virtual machine with vagrant+virtualBox

1. Introduction

Vagrant is a tool for building and managing virtual machines (VirtualBox).

My environment: Mac + Vagrant + VirtualBox

Download address: https://pan.baidu.com/s/1LKacXPQcgh3MjgWiIZ0zhQ Password: n998

2. Installation

VirtualBox: https://www.virtualbox.org/wiki/Downloads
Vagrant: http://downloads.vagrantup.co

3. Create a virtual machine online

1. Find the operating system you want to install on the official website: https://app.vagrantup.com/boxes/search

2. Click in and execute the command to create and start the virtual machine: https://app.vagrantup.com/centos/boxes/7

Note: You need to create a separate directory and execute the vagrant command in the directory . One directory corresponds to one virtual machine. vagrant init creates a configuration file Vagrantfile

3. Create a virtual machine offline

1. Add

vagrant box add centOs7 /Users/xianbin.yang/Documents/vagrant/centos-7.0-x86_64.box

➜ vagrant vagrant box add centOs7 /Users/xianbin.yang/Documents/vagrant/centos-7.0-x86_64.box
==> box: Box file was not detected as metadata. Adding it directly...
==> box: Adding box 'centOs7' (v0) for provider:
  box: Unpacking necessary files from: file:///Users/xianbin.yang/Documents/vagrant/centos-7.0-x86_64.box
==> box: Successfully added box 'centOs7' (v0) for 'virtualbox'!

2. Initialization

vagrant init centOs7

➜ vagrant vagrant init centOs7
A `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
`vagrantup.com` for more information on using Vagrant.

3. Start

vagrant up

➜ vagrant vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Importing base box 'centOs7'...
==> default: Matching MAC address for NAT networking...
==> default: Setting the name of the VM: vagrant_default_1612583375721_1893
==> default: Clearing any previously set forwarded ports...
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
  default: Adapter 1: nat
==> default: Forwarding ports...
  default: 22 (guest) => 2222 (host) (adapter 1)
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
  default: SSH address: 127.0.0.1:2222
  default: SSH username: vagrant
  default: SSH auth method: private key
  default:
  default: Vagrant insecure key detected. Vagrant will automatically replace
  default: this with a newly generated keypair for better security.
  default:
  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...
==> default: Machine booted and ready!
==> default: Checking for guest additions in VM...
  default: The guest additions on this VM do not match the installed version of
  default: VirtualBox! In most cases this is fine, but in rare cases it can
  default: prevent things such as shared folders from working properly. If you see
  default: shared folder errors, please make sure the guest additions within the
  default: virtual machine matches the version of VirtualBox you have installed on
  default: your host and reload your VM.
  default:
  default: Guest Additions Version: 4.3.28
  default: VirtualBox Version: 6.1
==> default: Mounting shared folders...
  default: /vagrant => /Users/xianbin.yang/Documents/vagrant

4. Common commands

Order effect
vagrant box add Add box operation
vagrant init Initializing the box will generate the Vagrant configuration file Vagrantfile
vagrant up Start the local environment
vagrant ssh Log in to the virtual machine where the local environment is located via ssh
vagrant halt Close the local environment
vagrant suspend Pause the local environment
vagrant resume Restore the local environment
vagrant reload After modifying the Vagrantfile, make it effective (equivalent to halt first, then up)
vagrant destroy Completely remove the local environment
vagrant box list Display the list of boxes that have been added
vagrant box remove Delete the corresponding box
vagrant status Get the current state of the virtual machine


5. Common configuration of Vagrantfile

1. Configure fixed IP, memory, and CPU

# -*- mode: ruby ​​-*-
# vi: set ft=ruby :
 
Vagrant.configure("2") do |config|
 config.vm.box = "centOs7"
 config.vm.network "private_network", ip: "192.168.33.10"
 config.vm.provider "virtualbox" do |vb|
   vb.cpus = 4
   vb.memory = "8192"
 end
end

Note: vagrant reload is required after configuration

2. Configure root user and ssh password-free

Vagrant user login: vagrant ssh

Set the root password: sudo passwd root

Configure ssh without password: ssh-copy-id [email protected]

SSH login: ssh [email protected]

3. Turn off the firewall

Check the firewall status: systemctl status firewalld
Turn off the firewall: systemctl stop firewalld
Set to disable the firewall at startup: systemctl disable firewalld.service

Reference Links:

https://learn.hashicorp.com/tutorials/vagrant/getting-started-index?in=vagrant/getting-started

This is the end of this article about building a virtual machine with vagrant+virtualBox. For more information about building a virtual machine with virtualBox, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Graphic tutorial on deploying Vagrant and VirtualBox in Ubuntu system
  • Use virtualbox + vagrant to configure the ruby ​​development machine environment under win10
  • A tutorial on how to use VirtualBox to connect a virtual machine to the network
  • Detailed explanation of VirtualBox + CentOS virtual machine network card configuration
  • Detailed tutorial on installing Ubuntu on VirtualBox virtual machine (picture and text)

<<:  English: A link tag will automatically complete href in IE

>>:  Pure CSS3 to achieve pet chicken example code

Recommend

7 useful new TypeScript features

Table of contents 1. Optional Chaining 2. Null va...

Stop using absolute equality operators everywhere in JS

Table of contents Overview 1. Test for null value...

MySQL uses custom sequences to implement row_number functions (detailed steps)

After reading some articles, I finally figured ou...

A complete guide to some uncommon but useful CSS attribute operations

1. Custom text selection ::selection { background...

Example code for implementing div concave corner style with css

In normal development, we usually use convex roun...

Analyze the problem of pulling down the Oracle 11g image configuration in Docker

1. Pull the image docker pull registry.cn-hangzho...

New ways to play with CSS fonts: implementation of colored fonts

What if you designers want to use the font below ...

mysql5.7 create user authorization delete user revoke authorization

1. Create a user: Order: CREATE USER 'usernam...

Detailed explanation of MySQL master-slave inconsistency and solutions

1. MySQL master-slave asynchrony 1.1 Network Dela...

CSS realizes the mask effect when the mouse moves to the image

1. Put the mask layer HTML code and the picture i...

MySQL 5.7 installation and configuration method graphic tutorial

This tutorial shares the installation and configu...

Win10 installation Linux system tutorial diagram

To install a virtual machine on a Windows system,...

Import CSS files using judgment conditions

Solution 1: Use conditional import in HTML docume...