实验环境
- Windows 11
- Vagrant 2.3.5
- VirtualBox 7.0
实验需求
- 创建一台CentOS7.9虚拟机,IP地址为192.168.56.10,2core,2G的虚拟机配置
实验解法
1、下载Centos7.9镜像文件
步骤 1:鼠标桌面上右击 在终端中打开
,执行命令 mkdir centos7
和 cd centos7
创建虚拟机目录并切换到该目录。
PS C:\Users\Xiaohui\Desktop> mkdir centos7
目录: C:\Users\Xiaohui\Desktop
Mode LastWriteTime Length Name
---- ------------- ------ ----
d----- 2023/5/18 15:49 centos7
PS C:\Users\Xiaohui\Desktop> cd centos7
PS C:\Users\Xiaohui\Desktop\centos7>
步骤 2:在Vagrant cloud搜索并下载Centos7.9镜像,保存到先前创建的目录下。
# VagrantCloud官网地址:https://app.vagrantup.com/boxes/search
# VagrantCloud仓库 类型于 Docker 的 Dockerhub仓库。
# 把下列URL链接复制到浏览器下载即可,由于是国外网站,下载速度不会很快。
https://app.vagrantup.com/generic/boxes/centos7/versions/4.2.16/providers/virtualbox.box
注意: 我使用的是Virtualbox虚拟机软件,所以必须下载Virtualbox版本的box文件。
2、添加到本地box列表
步骤1:执行命令 ls
查看目录下是否有先前下载的box文件。
PS C:\Users\Xiaohui\Desktop\centos7> ls
目录: C:\Users\Xiaohui\Desktop\centos7
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 2023/5/18 16:05 676391946 virtualbox.box
步骤2:执行命令 vagrant box add centos7 .\virtualbox.box
,把下载的box文件添加到本地box列表,并命名为 centos
。
PS C:\Users\Xiaohui\Desktop\centos7> vagrant box add centos7 .\virtualbox.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://C:/Users/Xiaohui/Desktop/centos7/virtualbox.box
box:
==> box: Successfully added box 'centos7' (v0) for 'virtualbox'!
步骤3:执行命令 vagrant box list
,查看本地box列表
PS C:\Users\Xiaohui\Desktop\centos7> vagrant box list
centos (virtualbox, 0) # 之前添加的
centos7 (virtualbox, 0)
debian_buster (virtualbox, 0) # 之前添加的
3、初始化虚拟机
步骤1:执行命令 vagrant init centos7
,执行命令后会在当前目录下生成一个 Vagrantfile
文件。
PS C:\Users\Xiaohui\Desktop\centos7> 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.
步骤2:查看 Vagrantfile 文件是否存在。
PS C:\Users\Xiaohui\Desktop\centos7> ls
目录: C:\Users\Xiaohui\Desktop\centos7
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 2023/5/18 16:13 3083 Vagrantfile
-a---- 2023/5/18 16:05 676391946 virtualbox.box
步骤3:查看 Vagrantfile 文件默认内容。
PS C:\Users\Xiaohui\Desktop\centos7> cat .\Vagrantfile
# -*- 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.
config.vm.box = "centos7"
# 如果先前初始化命令是 vagrant init , 那么现在就会是 config.vm.box = "base"
# 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: "127.0.0.1"
# 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"
# 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
#
# 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
4、编辑Vagrantfile
# -*- mode: ruby -*-
# vi: set ft=ruby :
Vagrant.configure("2") do |config|
config.vm.define "centos7_test" do |dns| # 执行命令 vagrant global-status 会显示的名称
dns.vm.box = "centos7" # 指定使用的box
dns.vm.synced_folder ".", "/vagrant",create: true # 把当前目录映射到虚拟机的/vagrant目录下
dns.vm.hostname = "test" # 设置虚拟机主机名
dns.vm.network "private_network", ip: "192.168.56.10" # 设置虚拟机IP地址
dns.vm.provider "virtualbox" do |vb|
vb.gui = false # 启动虚拟机时不启用GUI界面,节省宿主机资源
vb.name = "centos7_test" # Virtuablbox主机名称
vb.memory = "2048" # 虚拟机内存
vb.cpus = "2" # 虚拟机CPU核心数
end
config.vm.provision "shell", inline: <<-SHELL # 设置虚拟机启动后运行的脚本
echo "centos test"
SHELL
end
end
5、开启虚拟机
步骤1:执行命令 vagrant up
,启动虚拟机。
PS C:\Users\Xiaohui\Desktop\centos7> vagrant up
Bringing machine 'centos7_test' up with 'virtualbox' provider...
==> centos7_test: Importing base box 'centos7'...
==> centos7_test: Matching MAC address for NAT networking...
==> centos7_test: Setting the name of the VM: centos7_test
==> centos7_test: Clearing any previously set network interfaces...
==> centos7_test: Preparing network interfaces based on configuration...
centos7_test: Adapter 1: nat
centos7_test: Adapter 2: hostonly
==> centos7_test: Forwarding ports...
centos7_test: 22 (guest) => 2222 (host) (adapter 1)
==> centos7_test: Running 'pre-boot' VM customizations...
==> centos7_test: Booting VM...
==> centos7_test: Waiting for machine to boot. This may take a few minutes...
centos7_test: SSH address: 127.0.0.1:2222
centos7_test: SSH username: vagrant
centos7_test: SSH auth method: private key
centos7_test:
centos7_test: Vagrant insecure key detected. Vagrant will automatically replace
centos7_test: this with a newly generated keypair for better security.
centos7_test:
centos7_test: Inserting generated public key within guest...
centos7_test: Removing insecure key from the guest if it's present...
centos7_test: Key inserted! Disconnecting and reconnecting using new SSH key...
==> centos7_test: Machine booted and ready!
==> centos7_test: Checking for guest additions in VM...
centos7_test: The guest additions on this VM do not match the installed version of
centos7_test: VirtualBox! In most cases this is fine, but in rare cases it can
centos7_test: prevent things such as shared folders from working properly. If you see
centos7_test: shared folder errors, please make sure the guest additions within the
centos7_test: virtual machine match the version of VirtualBox you have installed on
centos7_test: your host and reload your VM.
centos7_test:
centos7_test: Guest Additions Version: 5.2.44
centos7_test: VirtualBox Version: 7.0
==> centos7_test: Setting hostname...
==> centos7_test: Configuring and enabling network interfaces...
==> centos7_test: Mounting shared folders...
centos7_test: /vagrant => C:/Users/Xiaohui/Desktop/centos7
==> centos7_test: Running provisioner: shell...
centos7_test: Running: inline script
centos7_test: centos test
6、连接虚拟机
步骤1:执行命令 vagrant ssh
, 默认使用 vagrant/vagrant
登录虚拟机。
PS C:\Users\Xiaohui\Desktop\centos7> vagrant ssh
[vagrant@test ~]$
步骤2:执行命令 sudo -i
获得 root 权限。
[vagrant@test ~]$ sudo -i
[root@test ~]#
7、关闭虚拟机
步骤1: 执行命令 vagrant halt
,关闭虚拟机。
[vagrant@test ~]$ exit
logout
Connection to 127.0.0.1 closed.
PS C:\Users\Xiaohui\Desktop\centos7> vagrant halt
==> centos7_test: Attempting graceful shutdown of VM...
步骤2:查看当前虚拟机状态。
PS C:\Users\Xiaohui\Desktop\centos7> vagrant status
Current machine states:
centos7_test poweroff (virtualbox)
The VM is powered off. To restart the VM, simply run `vagrant up`
8、销毁虚拟机
步骤1: 执行命令 vagrant global-status
,查看全局虚拟机状态。
PS C:\Users\Xiaohui\Desktop\centos7> vagrant global-status
id name provider state directory
---------------------------------------------------------------------------------------------
060f299 debian_test virtualbox poweroff C:/Users/Xiaohui/Desktop/vagrant测试环境生成/debian_test
c71c975 centos7_test virtualbox poweroff C:/Users/Xiaohui/Desktop/centos7
The above shows information about all known Vagrant environments
on this machine. This data is cached and may not be completely
up-to-date (use "vagrant global-status --prune" to prune invalid
entries). To interact with any of the machines, you can go to that
directory and run Vagrant, or you can use the ID directly with
Vagrant commands from any directory. For example:
"vagrant destroy 1a2b3c4d"
步骤2: 执行命令 vagrant destroy c71c975
或 vagrant destroy centos7_test
,销毁指定虚拟机。或者在当前目录下执行命令 vagrant destroy
即销毁当前虚拟机。增加 -f
选项,将强制销毁虚拟机。
PS C:\Users\Xiaohui\Desktop\centos7> vagrant destroy c71c975
centos7_test: Are you sure you want to destroy the 'centos7_test' VM? [y/N] y
==> centos7_test: Destroying VM and associated drives...
PS C:\Users\Xiaohui\Desktop\vagrant测试环境生成\debian_test> vagrant destroy -f
==> debian_test: Destroying VM and associated drives...
本文完!