用 Vagrant 和 VirtualBox 来快速搭建一个开发环境是非常方便的。简单整理一下 Vagrant 搭建 VirtualBox 虚拟机的记录。
首先安装 Vagrant 和 VirtualBox,这一步就直接省略了。自行安装即可。
什么是 Vagrant,我们这里引用 OSChina 上的一段话来进行解释:
Vagrant 是一个基于 Ruby 的工具,用于创建和部署虚拟化开发环境。它使用 Oracle 的开源 VirtualBox 虚拟化系统,使用 Chef 创建自动化虚拟环境。功能特性:支持快速新建虚拟机 支持快速设置端口转发 支持自定义镜像打包(原始镜像方式、增量补丁方式) 基本上日常能用到的基础配置都能快速设置 支持开机启动自动运行命令 可以自己写扩展
https://www.oschina.net/p/vagrant?hmsr=aladdin1e1
1.创建一个没有中文路径的目录,我这里创建的路径是
e:\vagrant\mall-node
2.启动virtual box
3.vagrant的仓库
https://app.vagrantup.com/boxes/search
4.执行vagrant init命令
在新建的目录下执行 vagrant init 命令
- PS E:\vagrant\mall-node> 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.
执行完命令后,在 e:\vagrant\mall-node 下建立了一个 Vagrantfile 文件
5.在 Vagrantfile 文件所在的目录下执行 vagrant up 命令
- PS E:\vagrant\mall-node> vagrant up
- Bringing 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: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/mall-node/ => /vagrant
执行完成后,查看 virtual box 多了一台虚拟机
6.执行 vagrant ssh 访问虚拟机中的 CentOS
- PS E:\vagrant\mall-node> vagrant ssh
- [vagrant@localhost ~]$ ll
- total 0
- [vagrant@localhost ~]$ pwd
- /home/vagrant
7.查看虚拟机的网卡
- [vagrant@localhost ~]$ ip a
- 1: lo:
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:
mtu 1500 qdisc pfifo_fast state UP group default qlen 1000 - link/ether 52:54:00:4d:77:d3 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 69311sec preferred_lft 69311sec
- inet6 fe80::5054:ff:fe4d:77d3/64 scope link
- valid_lft forever preferred_lft forever
lo是回环地址,eth0是虚拟机的IP地址,IP地址是10.0.2.15
8.修改ip地址
打开Vagrantfile文件,搜索“private_network”,可以找到config.vm.network配置项。可以看到里面有个IP地址。我们查看一下我们本机的IP地址,命令如下:
ipconfig
可以看到virtual box虚拟网卡的ip地址如下:
- 以太网适配器 VirtualBox Host-Only Network #2:
-
- 连接特定的 DNS 后缀 . . . . . . . :
- 本地链接 IPv6 地址. . . . . . . . : fe80::d55e:2249:a3ec:9d26%3
- IPv4 地址 . . . . . . . . . . . . : 192.168.56.1
- 子网掩码 . . . . . . . . . . . . : 255.255.255.0
可以看到该IP地址为192.168.56.1,我们将对应的Vagrantfile的ip地址修改192.168.56.101,其实只要在一个网段就可以,我这里使用了100。
修改以后,将其保存。然后关闭虚拟机,启动虚拟机,连接虚拟机,查看它的IP地址。一顿操作命令如下:
- [vagrant@localhost ~]$ poweroff
- ==== AUTHENTICATING FOR org.freedesktop.login1.power-off ===
- Authentication is required for powering off the system.
- Authenticating as: root
- ==== AUTHENTICATION COMPLETE ===
- Connection to 127.0.0.1 closed by remote host.
- Connection to 127.0.0.1 closed.
- PS E:\vagrant\mall-node> vagrant up
- Bringing machine 'default' up with 'virtualbox' provider...
- ==> 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/mall-node/ => /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\mall-node> vagrant ssh
- Last login: Sun Nov 20 01:45:46 2022 from 10.0.2.2
- [vagrant@localhost ~]$ ip a
- 1: lo:
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:
mtu 1500 qdisc pfifo_fast state UP group default qlen 1000 - link/ether 52:54:00:4d:77:d3 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 86384sec preferred_lft 86384sec
- inet6 fe80::5054:ff:fe4d:77d3/64 scope link
- valid_lft forever preferred_lft forever
- 3: eth1:
mtu 1500 qdisc pfifo_fast state UP group default qlen 1000 - link/ether 08:00:27:10:a9:1d brd ff:ff:ff:ff:ff:ff
- inet 192.168.56.101/24 brd 192.168.56.255 scope global noprefixroute eth1
- valid_lft forever preferred_lft forever
- inet6 fe80::a00:27ff:fe10:a91d/64 scope link
- valid_lft forever preferred_lft forever
可以看到,在虚拟机中又添加了一块虚拟网卡,并且IP地址是192.168.56.101。
9.使用 finalshell 连接
打开 finalshell,然后建立 SSH 连接,编辑相关的登录信息,如下图
注意这里选择公钥登录,用户名填写 vagrant,私钥的文件在 E:\vagrant\mall-node\.vagrant\machines\default\virtualbox 目录下的 private_key 中,大家把路径替换成自己的路径即可。配置好以后进行连接就可以了。
注意:
如果在执行 vagrant init 后,执行 vagrant up 无法启动虚拟机的话,可以在 Vagrant 仓库中下载 centos/7 文件。如下图:
下载后通过 add 命令添加即可,命令如下:
- vagrant init
- vagrant box add centos/7 .box文件路径
- vagrant up
- vagrant ssh