使用Vagrant安装初始化虚拟机

VirsualBox整合Vagrant使用

  1. 安装VirsualBox和vagrant这没啥好说的,就一直下一步安装Windows软件一样就行了。安装完vagrant之后可以验证是否安装成功
vagrant -v

打印如下图所示表示安装成功
在这里插入图片描述
2. 查询vagrant相关镜像地址

https://app.vagrantup.com/boxes/search
  1. 安装Centos 7镜像
PS E:\vagrant> vagrant init centos/7
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.
  1. 初始化/启动虚拟机
PS E:\vagrant> vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Box 'centos/7' could not be found. Attempting to find and install...
    default: Box Provider: virtualbox
    default: Box Version: >= 0
==> default: Loading metadata for box 'centos/7'
    default: URL: https://vagrantcloud.com/centos/7
==> default: Adding box 'centos/7' (v2004.01) for provider: virtualbox
    default: Downloading: https://vagrantcloud.com/centos/boxes/7/versions/2004.01/providers/virtualbox.box
Download redirected to host: cloud.centos.org
    default:
    default: Calculating and comparing box checksum...
==> default: Successfully added box 'centos/7' (v2004.01) for 'virtualbox'!
==> default: Importing base box 'centos/7'...
==> default: Matching MAC address for NAT networking...
==> default: Checking if box 'centos/7' version '2004.01' is up to date...
==> default: Setting the name of the VM: vagrant_default_1590547659090_2632
==> 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: No guest additions were detected on the base box for this VM! Guest
    default: additions are required for forwarded ports, shared folders, host only
    default: networking, and more. If SSH fails on this machine, please install
    default: the guest additions and repackage the box to continue.
    default:
    default: This is not an error message; everything may continue to work properly,
    default: in which case you may ignore this message.
==> default: Rsyncing folder: /cygdrive/e/vagrant/ => /vagrant

此时初始化Centos7完成,在virsualBox界面中可以看到有一个正在运行的虚拟机生成
在这里插入图片描述

  1. 连接vagrant创建好的虚拟机
PS E:\vagrant> vagrant ssh
Last login: Wed May 27 05:24:48 2020 from 10.0.2.2
Last login: Wed May 27 05:24:48 2020 from 10.0.2.2
[vagrant@localhost ~]$
  1. 退出虚拟机
[vagrant@localhost ~]$ exit
logout
Connection to 127.0.0.1 closed.
  1. 给vagrant虚拟机指定IP地址
    编辑Vagrantfile文件,添加
config.vm.network "private_network", ip: "192.168.56.10"

注意:需要和Windows主机中VirsualBox虚拟网卡处于一个IP端,如下图:
在这里插入图片描述

  1. 保存文件,并重启
PS E:\vagrant> vagrant reload
==> default: Attempting graceful shutdown of VM...
==> default: Checking if box 'centos/7' version '2004.01' is up to date...
==> 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: Adapter 2: hostonly
==> 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: Machine booted and ready!
==> default: Checking for guest additions in VM...
    default: No guest additions were detected on the base box for this VM! Guest
    default: additions are required for forwarded ports, shared folders, host only
    default: networking, and more. If SSH fails on this machine, please install
    default: the guest additions and repackage the box to continue.
    default:
    default: This is not an error message; everything may continue to work properly,
    default: in which case you may ignore this message.
==> default: Configuring and enabling network interfaces...
==> default: Rsyncing folder: /cygdrive/e/vagrant/ => /vagrant
==> default: Machine already provisioned. Run `vagrant provision` or use the `--provision`
==> default: flag to force provisioning. Provisioners marked to run always will still run.
PS E:\vagrant>
  1. 验证是否可以ping通主机
[vagrant@localhost ~]$ ping 192.168.1.1
PING 192.168.1.1 (192.168.1.1) 56(84) bytes of data.
64 bytes from 192.168.1.1: icmp_seq=1 ttl=63 time=1.70 ms
64 bytes from 192.168.1.1: icmp_seq=2 ttl=63 time=1.77 ms
64 bytes from 192.168.1.1: icmp_seq=3 ttl=63 time=2.40 ms
64 bytes from 192.168.1.1: icmp_seq=4 ttl=63 time=2.37 ms
64 bytes from 192.168.1.1: icmp_seq=5 ttl=63 time=1.29 ms
64 bytes from 192.168.1.1: icmp_seq=6 ttl=63 time=1.84 ms
64 bytes from 192.168.1.1: icmp_seq=7 ttl=63 time=1.55 ms
64 bytes from 192.168.1.1: icmp_seq=8 ttl=63 time=1.63 ms
^C
--- 192.168.1.1 ping statistics ---
8 packets transmitted, 8 received, 0% packet loss, time 7025ms
rtt min/avg/max/mdev = 1.296/1.823/2.408/0.363 ms
[vagrant@localhost ~]$
  1. 主机是否可以ping通vagrant虚拟机
C:\Users\Administrator>ping 192.168.56.10

正在 Ping 192.168.56.10 具有 32 字节的数据:
来自 192.168.56.10 的回复: 字节=32 时间<1ms TTL=64
来自 192.168.56.10 的回复: 字节=32 时间<1ms TTL=64
来自 192.168.56.10 的回复: 字节=32 时间<1ms TTL=64
来自 192.168.56.10 的回复: 字节=32 时间<1ms TTL=64

192.168.56.10 的 Ping 统计信息:
    数据包: 已发送 = 4,已接收 = 4,丢失 = 0 (0% 丢失),
往返行程的估计时间(以毫秒为单位):
    最短 = 0ms,最长 = 0ms,平均 = 0ms

C:\Users\Administrator>

这样就ok了,确实比自己去安装感觉要方便很多了。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值