Vagrant 搭建虚拟机环境


Vagrant 搭建虚拟机环境

文章插图
 
用 Vagrant 和 VirtualBox 来快速搭建一个开发环境是非常方便的 。简单整理一下 Vagrant 搭建 VirtualBox 虚拟机的记录 。
首先安装 Vagrant 和 VirtualBox , 这一步就直接省略了 。自行安装即可 。
什么是 Vagrant , 我们这里引用 OSChina 上面的一段话来进行解释:
Vagrant 是一个基于 Ruby 的工具 , 用于创建和部署虚拟化开发环境 。它使用 Oracle 的开源 VirtualBox 虚拟化系统 , 使用 Chef 创建自动化虚拟环境 。功能特性:支持快速新建虚拟机 支持快速设置端口转发 支持自定义镜像打包(原始镜像方式、增量补丁方式) 基本上日常能用到的基础配置都能快速设置 支持开机启动自动运行命令 可以自己写扩展
https://www.oschina.NET/p/vagrant?hmsr=aladdin1e1
1.创建一个没有中文路径的目录 , 我这里创建的路径是
e:vagrantmall-node
2.启动virtual box
3.vagrant的仓库
【Vagrant 搭建虚拟机环境】https://App.vagrantup.com/boxes/search
4.执行vagrant init命令
在新建的目录下执行 vagrant init 命令
PS E:vagrantmall-node> vagrant init centos/7A `Vagrantfile` has been placed in this directory. You are nowready to `vagrant up` your first virtual environment! Please readthe comments in the Vagrantfile as well as documentation on`vagrantup.com` for more information on using Vagrant.执行完命令后 , 在 e:vagrantmall-node 下建立了一个 Vagrantfile 文件
5.在 Vagrantfile 文件所在的目录下执行 vagrant up 命令
PS E:vagrantmall-node> vagrant upBringing machine 'default' up with 'virtualbox' provider...==> default: Importing base box 'centos/7'...==> default: Matching MAC address for NAT networking...==> default: Setting the name of the VM: mall-node_default_1668852218722_77593==> 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:2222default: SSH username: vagrantdefault: SSH auth method: private keydefault:default: Vagrant insecure key detected. Vagrant will automatically replacedefault: 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! Guestdefault: additions are required for forwarded ports, shared folders, host onlydefault: networking, and more. If SSH fails on this machine, please installdefault: 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/mall-node/ => /vagrant执行完成后 , 查看 virtual box 多了一台虚拟机
Vagrant 搭建虚拟机环境

文章插图
 
6.执行 vagrant ssh 访问虚拟机中的 CentOS
PS E:vagrantmall-node> vagrant ssh[vagrant@localhost ~]$ lltotal 0[vagrant@localhost ~]$ pwd/home/vagrant7.查看虚拟机的网卡
[vagrant@localhost ~]$ ip a1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00inet 127.0.0.1/8 scope host lovalid_lft forever preferred_lft foreverinet6 ::1/128 scope hostvalid_lft forever preferred_lft forever2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000link/ether 52:54:00:4d:77:d3 brd ff:ff:ff:ff:ff:ffinet 10.0.2.15/24 brd 10.0.2.255 scope global noprefixroute dynamic eth0valid_lft 69311sec preferred_lft 69311secinet6 fe80::5054:ff:fe4d:77d3/64 scope linkvalid_lft forever preferred_lft foreverlo是回环地址 , eth0是虚拟机的IP地址 , IP地址是10.0.2.15
8.修改ip地址
打开Vagrantfile文件 , 搜索“private_network” , 可以找到config.vm.network配置项 。可以看到里面有个IP地址 。我们查看一下我们本机的IP地址 , 命令如下:
ipconfig可以看到virtual box虚拟网卡的ip地址如下:


推荐阅读