Vagrant and Virtual Box build centos virtual machine

There are many ways to build a virtual machine, such as direct use of virtual workstation+mirror, virtual Box+mirror, etc. In this article, we introduce the use of virtual box + vagrant to build a virtual machine. Before that, we need to prepare VirtualBox-6.0.12-133076- Win, vagrant_2.2.6_x86_64, centos7. For VirtualBox-6.0.12-133076-Win, the installation of vagrant_2.2.6_x86_64 is relatively simple, only a fool-type installation is required, and I will not explain it here. It is straightforward to note that you need to install vagrant first, then install VirtualBox, the installation is successful After that, you need to restart the local computer, otherwise an error will be reported when starting the virtual machine: No usable default provider could be found for your system. If the error is still reported after restarting, it may be caused by the wrong version of vagrant and virtual box, you can do it yourself Baidu solutions. After the above two software are installed, we can start to create our virtual machine.

First, we create a centos file on this machine, and then enter the file directory H:\>cd centos7. Create a Vagrantfile file in this directory, which is a vagrant virtualization configuration file, which can be created by the vagrant command, the command is as follows:

vagrant init centos/7

The centos/7 in the above command is the name of the mirror. Then we need to specify a mirror file for centos/7, that is, add a mirror file for centos/7. Here we use the file named virtualbox.box, we You can use the command to add a mirror, the command is as follows:

vagrant box add centos/7 H:\virtualbox.box

With such simple steps, we have successfully created a virtual machine, but the virtual machine has not been started. We can still use the vagrant command to start and close the virtual machine, and open the virtual box to observe the virtual machine's startup process. The following are the commands for starting and closing vagrant. We need to enter the directory where Vagrantfile is located, which is the centos7 directory:

vagrant up     正常启动
vagrant halt   优雅关闭

After using vagrant up to start, we can see the following output in the console, and see the virtual machine in the virtual box has been successfully started, as shown in the following figure:

 In addition to using vagrant to start and shut down the virtual machine, we can also use the vagrant command to connect to the started virtual machine, view the status of the virtual machine, delete the virtual machine, and other operations. You can also write script commands in the Vagrantfile. The following are common commands for Vagrant:

vagrant ssh    进入刚才创建的centos7中
vagrant status 查看centos7的状态
vagrant destroy 删除centos7
vagrant status 查看当前vagrant创建的虚拟机
vagrant reload 重新加载 Vagrantfile文件
vagrant ssh-config  查看ssh配置

By default, after starting with vagrant up, an ip network segment will be automatically generated. We can't ping this address when we ping this address on our local machine, so we need to configure Vagrantfile as public-network. And you can configure the memory and other characteristics of the virtual machine, the configuration is as follows:

config.vm.network "public_network"
config.vm.provider "virtualbox" do |vb|
# 使用使用GUI
vb.gui = true
# 自定义VM内存
vb.memory = "1024"
end

Then we can view the ip of the virtual machine through the ip addr command, the command and output are as follows:

H:\centos7>vagrant ssh
Last login: Sat Sep  5 02:57:46 2020 from 10.0.2.2
[vagrant@localhost ~]$ ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 52:54:00:8a:fe:e6 brd ff:ff:ff:ff:ff:ff
    inet 10.0.2.15/24 brd 10.0.2.255 scope global noprefixroute dynamic eth0
       valid_lft 86394sec preferred_lft 86394sec
    inet6 fe80::5054:ff:fe8a:fee6/64 scope link
       valid_lft forever preferred_lft forever
3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 08:00:27:7b:86:f3 brd ff:ff:ff:ff:ff:ff
    inet 192.168.88.11/24 brd 192.168.88.255 scope global noprefixroute dynamic eth1
       valid_lft 594sec preferred_lft 594sec
    inet6 fe80::a00:27ff:fe7b:86f3/64 scope link
       valid_lft forever preferred_lft forever

Then we can log in through xshell, but before that we need to configure the user name and password for the root user. First, we switch to the root user through sudo -i, and then edit the sshd_config file, the command is as follows, change the value of PasswordAuthentication to yes, the default is false

vi /etc/ssh/sshd_config

Then modify the password of the root user through the passwd command, the command is as follows:

[root@localhost ~]# passwd root
Changing password for user root.
New password:
BAD PASSWORD: The password is shorter than 8 characters
Retype new password:
passwd: all authentication tokens updated successfully.

After entering the password twice, I restarted the sshd service through the systemctl restart sshd command, and then I could log in to our centos virtual machine through xshell. So far, the installation of our virtual machine is over. We can install Tomcat, jdk, database and other services on the virtual machine. When we install services in the future, the first choice is docker installation, so we will introduce the installation and deployment of docker in the next article.

Guess you like

Origin blog.csdn.net/wk19920726/article/details/108416863