Virtualbox + Vagrant Centos7 installation

“This is the 7th day of my participation in the Gwen Challenge in November. Check out the details: The Last Gwen Challenge in 2021.”

1. Install the Vagrant

Vagrant download

Vagrant-v view version

2. Install virtualbox

Virtualbox download

Note: Note that there are version compatibility issues with Vagrant

My version

Vagrant virtualbox
Vagrant 2.2.18 6.1.22

Note:

  • VirtualBox and VMware cannot use instructions when Hyper-V is enabled
  • Virtualbox requires your computer to turn off virtualization support
  • To use Docker for Windows as a development environment, the virtual machine must use Hyper-V, VirtualBox and Docker for Windows cannot be used at the same time

4. Add a local centos7 mirror

  • Vagrant is a very slow installation process. The vagrant is a very slow installation process

Download the centos7 image

  • Add the downloaded Centos7 image to your local box

vagrant box add centos/7 E:\VirtualBox\myConf\CentOS-7-x86_64-Vagrant-2004_01.VirtualBox.box

  • Query whether an image has been added to the box

vagrant box list

5. Create a VM environment

  1. Create an empty folder

  1. Initialize a Vagrantfile file

In this directory is the VagrantFile VIRTUAL machine configuration file generated by the CMD command. Vagrant generates the corresponding VIRTUAL machine from the VagrantFile configuration file

vagrant init

  1. Modify the Vagrantfile file

3.1 Configuration 1: Creating a VM from a File Modify the VagrantFile file as follows

# -*- 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|
  # The most common configuration options are documented and commented below.
  # For a complete reference, please see the online documentation at
  # https://docs.vagrantup.com.

  # Every Vagrant development environment requires a box. You can search for
  # boxes at https://vagrantcloud.com/search.
  Pull mirror centos/7
  config.vm.box = "centos/7"

  # Disable automatic box update checking. If you disable this, then
  # boxes will only be checked for updates when the user runs
  # `vagrant box outdated`. This is not recommended.
  # config.vm.box_check_update = false

  # Create a forwarded port mapping which allows access to a specific port
  # within the machine from a port on the host machine. In the example below,
  # accessing "localhost:8080" will access port 80 on the guest machine.
  # NOTE: This will enable public access to the opened port
  # config.vm.network "forwarded_port", guest: 80, host: 8080

  # Create a forwarded port mapping which allows access to a specific port
  # within the machine from a port on the host machine and only allow access
  # via 127.0.0.1 to disable public access
  # config.vm.network "forwarded_port", guest: 80, host: 8080, host_ip:"

  # Create a private network, which allows host-only access to the machine
  # using a specific IP.
  # config.vm.network "private_network", ip: "192.168.33.10"

  # Create a public network, which generally matched to bridged network.
  # Bridged networks make the machine appear as another physical device on
  # your network.
  # config.vm.network "public_network"
  
  # Adopt bridge network and share host network
  config.vm.network "public_network"
  

  # Share an additional folder to the guest VM. The first argument is
  # the path on the host to the actual folder. The second argument is
  # the path on the guest to mount the folder. And the optional third
  # argument is a set of non-required options.
  # config.vm.synced_folder ".. /data", "/vagrant_data"

  # Provider-specific configuration so you can fine-tune various
  # backing providers for Vagrant. These expose provider-specific options.
  # Example for VirtualBox:
  #
  # config.vm.provider "virtualbox" do |vb|
  # # Display the VirtualBox GUI when booting the machine
  # vb.gui = true
  #
  # # Customize the amount of memory on the VM:
  # vb.memory = "1024"
  # end
  #
   # virtual machine name ljw-centos7, memory, number of cores
	config.vm.provider "virtualbox" do |vb|
	vb.memory = "4096"
	vb.name= "ljw-centos7"
	vb.cpus= 2
	end
  # View the documentation for the provider you are using for more
  # information on available options.

  # Enable provisioning with a shell script. Additional provisioners such as
  # Ansible, Chef, Docker, Puppet and Salt are also available. Please see the
  # documentation for more information about their specific syntax and use.
  # config.vm.provision "shell", inline: <<-SHELL
  # apt-get update
  # apt-get install -y apache2
  # SHELL
end

Copy the code

3.2 Configuration 2: Creating three VM configurations in one file

Modify the VagrantFile file as follows

Vagrant.configure("2") do|config| (1.. 3).eachdo |i|
        config.vm.define "kafka#{i}" do |node|
            To set the virtual machine Box, use centos/7, which has been downloaded locally and added to Vagrant
            node.vm.box = "centos/7"

            # Set the host name of the VM
            node.vm.hostname="kafka#{i}"
            # Adopt bridge network and share host network
            #node.vm.network "public_network
            # Set the VM IP address
            node.vm.network "private_network", ip: "192.168.56. # {10 + I}", netmask: "255.255.255.0"

            # Set shared directories for hosts and VMS
            # node.vm.synced_folder "~/Documents/vagrant/share", "/home/vagrant/share"

            # VirtaulBox related configuration
            node.vm.provider "virtualbox" do |v|
                # Set the name of the VM
                v.name = "kafka#{i}"
                Set the vm memory size
                v.memory = 3072
                # Set the number of cpus for the VM
                v.cpus = 3
            end
        end
   end
end
Copy the code

6. Create and start a VM

Run the VirtualBox command first

vagrant up

7. Connection configuration login with password, not with Vagrant login

  1. Enter the corresponding server, Vagrant SSH kafka1

  2. Enter root user sudo -i

  3. Modify the sshd_config file so that the root user can use the password to log in to the vi /etc/ssh/sshd_config file

PasswordAuthentication yes
Log in as user root
PermitRootLogin yes
Copy the code
  1. Run the passwd command to set the new password to root

  2. Run the systemctl restart SSHD command to restart the password service

  3. Run IP add or IP route show to check the IP address of the eth1 network adapter to log in to user root using a remote tool. Root/root and vagrant/vagrant

Note:

  • The passwords are all Vagrant
  • The vagrant SSH command cannot automatically log in. You need to manually enter the account password on the GUI interface
    • The default path for the private key is. Vagrant \machines\default\virtualbox\private_key
  • Window The config file is configured but the private key path is not configured
    • Path: C: \ Users \ PC SSH \ config

Note:

  • Vagrant + Centos7 installation error reported
  • Window Generates the key: ssh-keygen -t rsa

Vagrant often commands

  • Vagrant init: Initialization
  • Vagrant Up: Starts the VM
  • Vagrant HALT: Shut down the virtual machine
  • Vagrant Reload: Restarts the VM
  • Vagrant SSH: SSH to a VM
  • Vagrant Status: View the VM running status
  • Vagrant Destroy: Destroys the current VM