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

Web project development VUE mixing and inheritance principle

Table of contents Mixin Mixin Note (duplicate nam...

Several common methods for setting anchor positioning in HTML

There are several ways I know of to set anchor pos...

Detailed explanation of Vue3 life cycle functions and methods

1. Overview The so-called life cycle function is ...

Analysis of the solution to Nginx Session sharing problem

This article mainly introduces the solution to th...

Centos7 installation of FFmpeg audio/video tool simple document

ffmpeg is a very powerful audio and video process...

Linux command line operation Baidu cloud upload and download files

Table of contents 0. Background 1. Installation 2...

How to adjust the log level of nginx in Docker

Table of contents Intro Nginx Dockerfile New conf...

How to use vue filter

Table of contents Overview Defining filters Use o...

Implementation of mysql8.0.11 data directory migration

The default storage directory of mysql is /var/li...

Detailed explanation of script debugging mechanism in bash

Run the script in debug mode You can run the enti...

Implementation of automatic completion of Docker commands

Preface I don't know how long this friend has...

Several situations that cause MySQL to perform a full table scan

Table of contents Case 1: Case 2: Case 3: To summ...

Solve the problem of MySQL Threads_running surge and slow query

Table of contents background Problem Description ...

Unicode signature BOM (Byte Order Mark) issue for UTF-8 files

I recently encountered a strange thing when debug...