• Vagrant 搭建虚拟机环境


            用 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 命令 

    1. PS E:\vagrant\mall-node> vagrant init centos/7
    2. A `Vagrantfile` has been placed in this directory. You are now
    3. ready to `vagrant up` your first virtual environment! Please read
    4. the comments in the Vagrantfile as well as documentation on
    5. `vagrantup.com` for more information on using Vagrant.

            执行完命令后,在 e:\vagrant\mall-node 下建立了一个 Vagrantfile 文件

    5.在 Vagrantfile 文件所在的目录下执行 vagrant up 命令

    1. PS E:\vagrant\mall-node> vagrant up
    2. Bringing machine 'default' up with 'virtualbox' provider...
    3. ==> default: Importing base box 'centos/7'...
    4. ==> default: Matching MAC address for NAT networking...
    5. ==> default: Setting the name of the VM: mall-node_default_1668852218722_77593
    6. ==> default: Clearing any previously set network interfaces...
    7. ==> default: Preparing network interfaces based on configuration...
    8. default: Adapter 1: nat
    9. ==> default: Forwarding ports...
    10. default: 22 (guest) => 2222 (host) (adapter 1)
    11. ==> default: Booting VM...
    12. ==> default: Waiting for machine to boot. This may take a few minutes...
    13. default: SSH address: 127.0.0.1:2222
    14. default: SSH username: vagrant
    15. default: SSH auth method: private key
    16. default:
    17. default: Vagrant insecure key detected. Vagrant will automatically replace
    18. default: this with a newly generated keypair for better security.
    19. default:
    20. default: Inserting generated public key within guest...
    21. default: Removing insecure key from the guest if it's present...
    22. default: Key inserted! Disconnecting and reconnecting using new SSH key...
    23. ==> default: Machine booted and ready!
    24. ==> default: Checking for guest additions in VM...
    25. default: No guest additions were detected on the base box for this VM! Guest
    26. default: additions are required for forwarded ports, shared folders, host only
    27. default: networking, and more. If SSH fails on this machine, please install
    28. default: the guest additions and repackage the box to continue.
    29. default:
    30. default: This is not an error message; everything may continue to work properly,
    31. default: in which case you may ignore this message.
    32. ==> default: Rsyncing folder: /cygdrive/e/vagrant/mall-node/ => /vagrant

    执行完成后,查看 virtual box 多了一台虚拟机

    6.执行 vagrant ssh 访问虚拟机中的 CentOS 

    1. PS E:\vagrant\mall-node> vagrant ssh
    2. [vagrant@localhost ~]$ ll
    3. total 0
    4. [vagrant@localhost ~]$ pwd
    5. /home/vagrant

    7.查看虚拟机的网卡

    1. [vagrant@localhost ~]$ ip a
    2. 1: lo: mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    3. link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    4. inet 127.0.0.1/8 scope host lo
    5. valid_lft forever preferred_lft forever
    6. inet6 ::1/128 scope host
    7. valid_lft forever preferred_lft forever
    8. 2: eth0: mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    9. link/ether 52:54:00:4d:77:d3 brd ff:ff:ff:ff:ff:ff
    10. inet 10.0.2.15/24 brd 10.0.2.255 scope global noprefixroute dynamic eth0
    11. valid_lft 69311sec preferred_lft 69311sec
    12. inet6 fe80::5054:ff:fe4d:77d3/64 scope link
    13. 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地址如下:

    1. 以太网适配器 VirtualBox Host-Only Network #2:
    2. 连接特定的 DNS 后缀 . . . . . . . :
    3. 本地链接 IPv6 地址. . . . . . . . : fe80::d55e:2249:a3ec:9d26%3
    4. IPv4 地址 . . . . . . . . . . . . : 192.168.56.1
    5. 子网掩码 . . . . . . . . . . . . : 255.255.255.0

            可以看到该IP地址为192.168.56.1,我们将对应的Vagrantfile的ip地址修改192.168.56.101,其实只要在一个网段就可以,我这里使用了100。

            修改以后,将其保存。然后关闭虚拟机,启动虚拟机,连接虚拟机,查看它的IP地址。一顿操作命令如下:

    1. [vagrant@localhost ~]$ poweroff
    2. ==== AUTHENTICATING FOR org.freedesktop.login1.power-off ===
    3. Authentication is required for powering off the system.
    4. Authenticating as: root
    5. ==== AUTHENTICATION COMPLETE ===
    6. Connection to 127.0.0.1 closed by remote host.
    7. Connection to 127.0.0.1 closed.
    8. PS E:\vagrant\mall-node> vagrant up
    9. Bringing machine 'default' up with 'virtualbox' provider...
    10. ==> default: Clearing any previously set forwarded ports...
    11. ==> default: Clearing any previously set network interfaces...
    12. ==> default: Preparing network interfaces based on configuration...
    13. default: Adapter 1: nat
    14. default: Adapter 2: hostonly
    15. ==> default: Forwarding ports...
    16. default: 22 (guest) => 2222 (host) (adapter 1)
    17. ==> default: Booting VM...
    18. ==> default: Waiting for machine to boot. This may take a few minutes...
    19. default: SSH address: 127.0.0.1:2222
    20. default: SSH username: vagrant
    21. default: SSH auth method: private key
    22. ==> default: Machine booted and ready!
    23. ==> default: Checking for guest additions in VM...
    24. default: No guest additions were detected on the base box for this VM! Guest
    25. default: additions are required for forwarded ports, shared folders, host only
    26. default: networking, and more. If SSH fails on this machine, please install
    27. default: the guest additions and repackage the box to continue.
    28. default:
    29. default: This is not an error message; everything may continue to work properly,
    30. default: in which case you may ignore this message.
    31. ==> default: Configuring and enabling network interfaces...
    32. ==> default: Rsyncing folder: /cygdrive/e/vagrant/mall-node/ => /vagrant
    33. ==> default: Machine already provisioned. Run `vagrant provision` or use the `--provision`
    34. ==> default: flag to force provisioning. Provisioners marked to run always will still run.
    35. PS E:\vagrant\mall-node> vagrant ssh
    36. Last login: Sun Nov 20 01:45:46 2022 from 10.0.2.2
    37. [vagrant@localhost ~]$ ip a
    38. 1: lo: mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    39. link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    40. inet 127.0.0.1/8 scope host lo
    41. valid_lft forever preferred_lft forever
    42. inet6 ::1/128 scope host
    43. valid_lft forever preferred_lft forever
    44. 2: eth0: mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    45. link/ether 52:54:00:4d:77:d3 brd ff:ff:ff:ff:ff:ff
    46. inet 10.0.2.15/24 brd 10.0.2.255 scope global noprefixroute dynamic eth0
    47. valid_lft 86384sec preferred_lft 86384sec
    48. inet6 fe80::5054:ff:fe4d:77d3/64 scope link
    49. valid_lft forever preferred_lft forever
    50. 3: eth1: mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    51. link/ether 08:00:27:10:a9:1d brd ff:ff:ff:ff:ff:ff
    52. inet 192.168.56.101/24 brd 192.168.56.255 scope global noprefixroute eth1
    53. valid_lft forever preferred_lft forever
    54. inet6 fe80::a00:27ff:fe10:a91d/64 scope link
    55. 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 命令添加即可,命令如下:

    1. vagrant init
    2. vagrant box add centos/7 .box文件路径
    3. vagrant up
    4. vagrant ssh

  • 相关阅读:
    算法学习-单调双端队列
    PMP有什么答题技巧?
    idea中启动maven项目报错-java: 程序包lombok.extern.slf4j不存在问题如何解决
    使用SpringBoot+Dubbo搭建微服务笔记
    HashMap
    java毕业生设计重工教师职称管理系统计算机源码+系统+mysql+调试部署+lw
    android user版本(不分平台+不分安卓几)实现root功能
    【RTOS学习】单片机中的C语言
    学会这些VRay渲染器HDRI照明技巧,轻松搞定3ds Max
    代码随想录算法训练营第五十二天|123.买卖股票的最佳时机III、188.买卖股票的最佳时机IV
  • 原文地址:https://blog.csdn.net/EasySecurity/article/details/128111853