Install centos7 using VirtualBox and Vagrant

Reference video: 0. Vagrant builds a local development environment
Reference blog post: Nanny-level installation tutorial for Vagrant and VirtualBox

Install a VirtualBox virtual machine

Download & install VirtualBox https://www.virtualbox.org/ , to enable CPU virtualization
Link: https://pan.baidu.com/s/14k-teejvNgy7nzUAdvyNEQ
Extraction code: zrdb

Install Vagrant

Download & Install Vagrant
https://app.vagrantup.com/boxes/search Vagrant Official Mirror Warehouse
https://www.vagrantup.com/downloads.html Vagrant Download
Link: https://pan.baidu.com/ s/1xbCgVeLniWeHMkXjyXhrCw
Extraction code: fn07
After the installation is complete, enter Vagrant in cmd. If there is a response, the installation is successful.
Enter vagrant -v to view the version number

vagrant -v

Install a centos 7 system

Go to the official website to download a centos/7.box file under the VirtualBox system https://app.vagrantup.com/centos/boxes/7
insert image description here
After downloading, you will get a box file CentOS-7-x86_64-Vagrant-2004_01.VirtualBox.box , change it to a shorter name for easy use, and then save it for later use. Here, change its name to centos7 and
store it in the E:\Vagrantfile folder. At this time, its file path is: E:\Vagrantfile\centos7.box.

  • Open a command line window (cmd) in the directory of the box file, then add it to the list of Vagrant-managed boxes:
vagrant box add centos7  CentOS-7-x86.box
  • Check if the addition is successful
vagrant box list

insert image description here

Create a virtual machine

Execute the command to create a virtual machine

vagrant init centos7

centos7 is the name given to the box earlier. After the command is executed, a Vagrantfile file will be generated in the folder, in which you can configure information about the virtual machine. Modify the
insert image description here
Vagrantfile configuration file

Vagrant.configure("2") do |config|
  config.vm.box = "centos7"             # box 名称
  config.vm.hostname = "centos7"        # 虚拟机名称
  config.vm.box_check_update = false    # 盒子不检查更新
  config.vm.network "private_network", ip: "192.168.33.10"  # 配置成私有网络,类似主机模式
  # 虚拟机供应者参数配置,相当于在 virtualbox 中配置
  config.vm.provider "virtualbox" do |vb|    
    vb.name = "centos7" # 虚拟机名称    
    vb.gui = false      # 启动机器时显示 VirtualBox GUI    
    vb.memory = "2048"  # 虚拟机存储大小,mb    
    vb.cpus = 2         # cpu 大小
  end
end

After completing the configuration, start the virtual machine contos7, and you can see some basic configuration information of the virtual machine during the startup process:

vagrant up

The startup is successful, and a virtual machine that has already started appears in VirtualBox. insert image description here
When executing the vagrant up command, an error is reported: /embedded/gems/2.2.7/gems/vagrant-2.2.7/lib/vagrant/util/io.rb:32 :in `encode': “\xAF\xE6” from GBK to UTF-8 (Encoding::UndefinedConversionError) Follow the method in the above blog post and then execute the vagrant up command successfully
insert image description here

Enter the virtual machine

Enter the command in the cmd window just now to enter the virtual machine

vagrant ssh

insert image description here

  • The default user for the virtual machine is vagrant, and the password for the root user is vagrant.

  • After entering, you can use the command line to operate like centos7, but it is not convenient to use cmd. It is recommended to use Xshell to cooperate with Xftp to operate. There is a free educational version.

  • Compared with the virtual machine management software using the iso image file to install the virtual machine, it is much more convenient to use the Vagrant management tool, and the installation tutorial ends here.

vagrant basic commands

The operation of adding a box

vagrant box add 

Initialize the operation of the box

vagrant init

Display the list of currently added boxes

vagrant box list

Delete the corresponding box that vagrant has installed

vagrant box remove

Start the operation of the virtual machine

vagrant up

shut down the virtual machine

vagrant halt 

Operations for logging in to the virtual machine

vagrant ssh

Stops the currently running virtual machine and destroys all created resources

vagrant destroy

Packaging command, which can package the current running virtual machine environment

vagrant package

Used to install and uninstall plugins

vagrant plugin

Restart the virtual machine, mainly for reloading configuration files

vagrant reload

Restore the previously suspended state

vagrant resume

output some information for ssh connection

vagrant ssh-config

Get the status of the current virtual machine

vagrant status

Suspend the current virtual machine

vagrant suspend

problems

The solution for the virtualbox window is too small

-bash: ifconfig: command not found

Linux installation error: ifconfig command not found solution
Execute the command:

sudo yum install net-tools

Configure port forwarding between virtual machines and physical machines

  1. Enter the command on the physical machine to view the ip of VirtualBox
    insert image description here
  2. In the Vagrantfile file generated by the vagrant init centos7 command, there is such a line of command. This line is used to configure the network information of the virtual machine. The address of the ip4 of VirtualBox is 192.168.56.1, so we only need to configure the address of the virtual machine as On the 192.168.56. segment, here we use 192.168.56.10
config.vm.network "private_network", ip: "192.168.56.10"  # 配置成私有网络,类似主机模式

After changing the Vagrantfile, restart the virtual machine and reload the configuration file

vagrant reload

After the restart is successful, use the ipconfig command to check the ip address of the wireless LAN adapter WLAN of the physical machine,
insert image description here
ping 192.168.0.103 in the virtual machine, and the ping is successful
In the virtual machine, use ipconfig to check whether the ip is 192.168.56.10, and
ping in the physical machine 192.168.56.10, the ping is successful.
This completes the communication between the physical machine and the virtual machine.

The newly installed virtual machine cannot log in remotely

The newly installed virtual machine does not support username and password login by default, so when we log in with tools such as xshell, it will pop up to let us enter the ssh key, or the selected user key is not registered on the remote host.
solution:

  1. If you use the vargrant ssh command to log in as the vargrant user, there are many commands that do not have permission to execute, so we need to use VirtualBox to log in to the virtual machine, use the root user,
    user name: root
    password: vagrant
  2. Enter the /etc/ssh directory, there is a sshd_config file, edit the sshd_config file
vi sshd_config

insert image description here

  1. In the sshd_config file there is a line
# 把整理的 no 改成 yes
PasswordAuthentication no
  1. restart service
service sshd restart

Modify the yum source of linux

  1. Backup the original yum source
# 进入目录
cd  /etc/yum.repos.d/
# 备份原 yum 源
mv CentOS-Base.repo CentOS-Base.repo.backup
  1. Use the new yum source
# 安装 curl
yum install curl
# 下载新 yum 源 , 下载完成后目录下回多出一个
curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.163.com/.help/CentOS7-Base-163.repo
  1. generate cache
yum makecache

insert image description here

Guess you like

Origin blog.csdn.net/qq_44154912/article/details/126209498