Vagrant+Virtual Box 配置虚拟机 IP地址
查看 本地主机的ip地址(测试通信用) 和 virtual box的ip地址(配置本地虚拟机用)
win10 cmd 打开命令行窗口,输入 ipconfig 查看ip地址
如图注意到本机win10的ip地址,和VirtualBox网卡ip地址,根据子网掩码可知子网地址是高24为,所以在VirtualBox上新建的虚拟机,为其分配ip地址形如 192.168.56.x, 其中x不能是0,127,1

默认配置文件 Vagrantfile
新建虚拟机地址设为192.168.56.10
config.vm.network “private_network”, ip: “192.168.56.10”
# -*- 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 = "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: "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.56.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
ping命令测试本地虚拟机与本地主机之间的网络通信是否正常,测试本地虚拟机能否访问百度
事先确保本地虚拟机已正常启动
启动方式
默认配置文件 Vagrantfile所在目录下命令行输入 vagrant reload(配置文件已修改)或者 vagrant up(配置文件未修改)或者 从VirtualBox 图形界面右键正常启动
开始ping测试
- 本机 ping 本地虚拟机

-
本地虚拟机 ping 本机
ssh连接本地虚拟机:本机命令行输入 vagrant ssh

连接本地虚拟机后,输入 ip addr 确认本地虚拟主机ip地址配置成功

本地虚拟机ping本地主机

- 本地虚拟机 ping 百度网站首页

本文档详细介绍了如何在Windows 10环境下,利用Vagrant和VirtualBox配置虚拟机的IP地址。通过cmd命令行获取本地主机和VirtualBox网卡的IP地址,然后在Vagrantfile中设定虚拟机IP为192.168.56.10。接着,通过vagrant reload或vagrant up启动虚拟机,并使用ping命令测试本地主机与虚拟机间的网络通信。此外,还展示了如何通过ssh连接虚拟机以及验证本地虚拟机访问外部网络的能力。
538

被折叠的 条评论
为什么被折叠?



