Vagrant安装centos7的4.1.36,VirtualBox运行

发布于:2022-10-13 ⋅ 阅读:(538) ⋅ 点赞:(0)
  • Vagrant是一种用于在单个工作流中构建和管理虚拟机环境的工具
  • VirtualBox
    • 是一款功能强大的 x86 和 AMD64/Intel64虚拟化产品
    • VirtualBox 是用于 x86 硬件的通用全虚拟化器,针对服务器、桌面和嵌入式使用
  • 在这里我们使用Vagrant下载安装虚拟环境,使用VirtualBox来运行虚拟机

下载和安装


  1. 下载安装Vagrant 下载:https://www.vagrantup.com/downloads
  • 安装一直点下一步,会自动配置好环境

![image.png](https://img-blog.csdnimg.cn/img_convert/21d8727d2eaa77383f8eff05af4b8745.png#clientId=u682819d5-41fd-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=34&id=u53fb3841&margin=[object Object]&name=image.png&originHeight=43&originWidth=262&originalType=binary&ratio=1&rotation=0&showTitle=false&size=3714&status=done&style=none&taskId=ub20cf681-4fa7-4a2f-828c-e8e656b2fad&title=&width=209.6)

  1. 根据Vagrant2.3.1版本选择VirtualBox版本

![image.png](https://img-blog.csdnimg.cn/img_convert/b6548c9f3214c336007fa5c01e0edfeb.png#clientId=u682819d5-41fd-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=77&id=u6df4055f&margin=[object Object]&name=image.png&originHeight=96&originWidth=1095&originalType=binary&ratio=1&rotation=0&showTitle=false&size=22939&status=done&style=none&taskId=u04b81b87-9754-4fe4-94c0-5ee25cbb819&title=&width=876)

参考官网选择VirtualBox版本

  1. 下载安装window版VirtualBox6.1.38:https://www.virtualbox.org/wiki/Downloads
    1. 目前最新有7.0版,不兼容,报错如下:
No usable default provider could be found for your system.

Vagrant relies on interactions with 3rd party systems, known as
"providers", to provide Vagrant with resources to run development
environments. Examples are VirtualBox, VMware, Hyper-V.

The easiest solution to this message is to install VirtualBox, which
is available for free on all major platforms.

If you believe you already have a provider available, make sure it
is properly installed and configured. You can see more details about
why a particular provider isn't working by forcing usage with
`vagrant up --provider=PROVIDER`, which should give you a more specific
error message for that particular provider.
  1. 所有还是重新下载VirtualBox6.1.38版
  2. 设置VirtualBox的默认虚拟电脑位置(默认在C盘)
  • 管理–全局设定

![image.png](https://img-blog.csdnimg.cn/img_convert/9562da977cad01fafef41b151cf73a30.png#clientId=u682819d5-41fd-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=160&id=u8cad7db2&margin=[object Object]&name=image.png&originHeight=200&originWidth=752&originalType=binary&ratio=1&rotation=0&showTitle=false&size=29352&status=done&style=none&taskId=ud8b9dc1a-2cb5-43a1-8d9d-ffae9e1384e&title=&width=601.6)

搭建虚拟机


Vagrant官方镜像仓库:https://app.vagrantup.com/boxes/search

使用官方的方法搭建

  1. 在官方镜像仓库搜索(这里我安装centos7的4.1.36版本)

![image.png](https://img-blog.csdnimg.cn/img_convert/9e84af7db8c7ce2a04245a43fa31a398.png#clientId=u682819d5-41fd-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=222&id=u37c7afb7&margin=[object Object]&name=image.png&originHeight=277&originWidth=1357&originalType=binary&ratio=1&rotation=0&showTitle=false&size=58357&status=done&style=none&taskId=uf740a8d7-bc23-439b-a1af-5e290b55ec1&title=&width=1085.6)
![image.png](https://img-blog.csdnimg.cn/img_convert/2dcd42a6be021c00dfa1fc9f71586d0b.png#clientId=u682819d5-41fd-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=241&id=u7d8a529d&margin=[object Object]&name=image.png&originHeight=301&originWidth=752&originalType=binary&ratio=1&rotation=0&showTitle=false&size=21231&status=done&style=none&taskId=u5690a132-b62f-42e7-9f15-e49f23d487f&title=&width=601.6)

  1. 在你指定的目录下打开控制台,执行vagrant init generic/centos7
  • 这个命令会在你目录下生成**Vagrantfile**文件

![image.png](https://img-blog.csdnimg.cn/img_convert/3c92b91caadbb10fbc60d467ec73b54e.png#clientId=u682819d5-41fd-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=110&id=uf7aec3c3&margin=[object Object]&name=image.png&originHeight=138&originWidth=732&originalType=binary&ratio=1&rotation=0&showTitle=false&size=10618&status=done&style=none&taskId=ua296150d-2ca6-464c-8a40-b3934209658&title=&width=585.6)

  • 重复执行会提示报错:Vagrantfile already exists in this directory. Remove it before running vagrant init.
  1. 然后执行vagrant up启动。会自动加载到VirtualBox,成功我们可以在VirtualBox管理器上看到这个虚拟机已经启动
  • 默认账号vagrant,密码也是vagrant

![image.png](https://img-blog.csdnimg.cn/img_convert/ee7f25b47a25ac3a8b41cda64d3cb6ea.png#clientId=u682819d5-41fd-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=286&id=u1596c6f8&margin=[object Object]&name=image.png&originHeight=357&originWidth=871&originalType=binary&ratio=1&rotation=0&showTitle=false&size=52913&status=done&style=none&taskId=u77d40043-9a06-4c6a-96e7-5fa63a03031&title=&width=696.8)
![image.png](https://img-blog.csdnimg.cn/img_convert/673e411baed2f45cf46f352d7af4d63f.png#clientId=u682819d5-41fd-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=214&id=u46907a4b&margin=[object Object]&name=image.png&originHeight=267&originWidth=1033&originalType=binary&ratio=1&rotation=0&showTitle=false&size=53449&status=done&style=none&taskId=udd1e826c-2977-426a-8ea3-6f57e87366f&title=&width=826.4)

  1. 如果不使用命令,可以在你指定目录下新建**Vagrantfile**文件,然后在这个目录下打开控制台,执行vagrant up启动
  • 文件内容复制如下

![image.png](https://img-blog.csdnimg.cn/img_convert/ee283e3ca7edb004a5257c10df55ff93.png#clientId=u682819d5-41fd-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=241&id=u5e196c6c&margin=[object Object]&name=image.png&originHeight=301&originWidth=745&originalType=binary&ratio=1&rotation=0&showTitle=false&size=24987&status=done&style=none&taskId=ua9d98a17-a93c-42a6-9273-17d5c894fbc&title=&width=596)

自己下载box文件搭建

Vagrant官方镜像仓库:https://app.vagrantup.com/boxes/search
第三方仓库:http://www.vagrantbox.es
Ubuntu 官方仓库:http://cloud-images.ubuntu.com/
Ubuntu 清华大学镜像仓库:https://mirror.tuna.tsinghua.edu.cn/ubuntu-cloud-images/
Centos 官方仓库:http://cloud.centos.org/centos/

  1. 推荐Vagrant官方镜像仓库下载

![image.png](https://img-blog.csdnimg.cn/img_convert/1420a3bb80c06a5975d54599698928a5.png#clientId=u682819d5-41fd-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=354&id=uadf1387f&margin=[object Object]&name=image.png&originHeight=442&originWidth=1445&originalType=binary&ratio=1&rotation=0&showTitle=false&size=47593&status=done&style=none&taskId=ua0b490ef-07f9-4c72-8b7c-08a06e68231&title=&width=1156)

  1. 方式一:直接导入,将下载好的文件解压,就有.ovf文件

![image.png](https://img-blog.csdnimg.cn/img_convert/3af42f30495cef578577fecfd1a6cc8b.png#clientId=u682819d5-41fd-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=490&id=uc8da2b4e&margin=[object Object]&name=image.png&originHeight=612&originWidth=1031&originalType=binary&ratio=1&rotation=0&showTitle=false&size=77791&status=done&style=none&taskId=u3c148b84-dbaf-4831-a489-9863a044e3c&title=&width=824.8)

  1. 方式二:使用命令
    1. vagrant box add centos7 路径和文件名
    • 有的下载下来文件名有**.box**后缀,有的没有,跟我这个一样是一串字符串,跟文件名没有关系

![image.png](https://img-blog.csdnimg.cn/img_convert/3714802ac6b300e2c9825da677e1ca8d.png#clientId=u682819d5-41fd-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=107&id=u5698da5b&margin=[object Object]&name=image.png&originHeight=134&originWidth=952&originalType=binary&ratio=1&rotation=0&showTitle=false&size=24920&status=done&style=none&taskId=uce155e8c-c1e5-4098-9179-cadd01aa148&title=&width=761.6)

  1. 执行:vagrant init 名字
  2. 启动(结果跟使用官方方法一样):vagrant up

登录虚拟机


使用SSH登录

  1. 进入到
    1. 默认虚拟电脑位置(管理–全局设置)可以看到
    2. 或者进入这两个文件所在的目录

![image.png](https://img-blog.csdnimg.cn/img_convert/c739662b0b3c687a84c4fdd6a3877cb3.png#clientId=u682819d5-41fd-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=136&id=ue8fa5e17&margin=[object Object]&name=image.png&originHeight=170&originWidth=845&originalType=binary&ratio=1&rotation=0&showTitle=false&size=16773&status=done&style=none&taskId=u072f3ff0-eba1-4f8e-bb65-c62c92183b8&title=&width=676)

  1. 进入到控制台,执行vagrant ssh

![image.png](https://img-blog.csdnimg.cn/img_convert/00a305a32870c5e4af24de6ca40e7a84.png#clientId=u682819d5-41fd-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=42&id=u8dfa5cc5&margin=[object Object]&name=image.png&originHeight=52&originWidth=432&originalType=binary&ratio=1&rotation=0&showTitle=false&size=5270&status=done&style=none&taskId=ucb379d01-54db-4f2c-b31a-165344b1623&title=&width=345.6)

使用账号密码登录

  1. 到VirtualBox显示虚拟机显示器
  2. 默认账号vagrant,密码也是vagrant

vagrant centos虚拟机首次用root登录


  • 我们先使用SSH或vagrant账号登录,然后执行下面命令
#为root账户设置密码
sudo passwd root

#设置root密码,输入两次

#切换到root用户
su -

虚拟机网络配置跟xshell可以root账号连接


  • 新建的虚拟机只有“NAT模式”网卡,我们虚拟机用“NAT模式”来上网,再新建“仅主机模式”网卡用来主机连接虚拟机

简单了解

  • “仅主机模式”是虚拟机和主机形成局域网,所以你可以在主机使用ssh连接虚拟机,但是这种实模式虚拟机不能上外网。默认使用
  • “NAT模式”:虚拟机可以上外网,但是主机默认不能ssh连接虚拟机,虚拟机ip不会变。默认使用
  • “桥接模式”:虚拟机可以上外网,但是主机默认不能ssh连接虚拟机,虚拟机ip会变化。默认使用

进行网络配置

  1. 打开VitrualBox:管理–主机网络管理器
  • 没有就新建

![image.png](https://img-blog.csdnimg.cn/img_convert/3674db7a3d2b234a500561b9b155c945.png#clientId=ubbb32bda-d54a-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=258&id=u5214a8d2&margin=[object Object]&name=image.png&originHeight=322&originWidth=865&originalType=binary&ratio=1&rotation=0&showTitle=false&size=17353&status=done&style=none&taskId=u286193ad-c43a-44b7-a8b5-f4a2a8730a2&title=&width=692)

  • DHCP服务器就是自动给电脑分配IP地址和子网掩码的服务器。比如我们设置了路由器的DHCP服务器,让DHCP服务器自动分配IP地址,从192.168.182.3到192.168.182.254(子网掩码会自动设置,我们无需设置)(也就是我们的有效网段),如下图所示。

![image.png](https://img-blog.csdnimg.cn/img_convert/65f09261467be6dacefb96274001f99c.png#clientId=udf86894c-9429-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=166&id=u2e2c9601&margin=[object Object]&name=image.png&originHeight=207&originWidth=855&originalType=binary&ratio=1&rotation=0&showTitle=false&size=6007&status=done&style=none&taskId=u1bfd6479-6dbb-4b89-8d3d-bdcfad6ca3c&title=&width=684)

  • 你能在主机电脑的网络连接上找到你新建的

![image.png](https://img-blog.csdnimg.cn/img_convert/d0a3797861d493ba48a638405b55cc14.png#clientId=ubbb32bda-d54a-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=162&id=u2067f996&margin=[object Object]&name=image.png&originHeight=202&originWidth=860&originalType=binary&ratio=1&rotation=0&showTitle=false&size=32555&status=done&style=none&taskId=u228d2fda-e42d-4f8b-a35a-72fe482ea7c&title=&width=688)

  1. 查看我们的虚拟机网卡,只有NAT模式,但又可以使用SSH密钥连接,是因为默认有一个端口转发规则

![image.png](https://img-blog.csdnimg.cn/img_convert/d7e70d2eb59ce6e601bf7bf03c0cb739.png#clientId=uf346fa7b-5660-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=149&id=ufe847e29&margin=[object Object]&name=image.png&originHeight=186&originWidth=568&originalType=binary&ratio=1&rotation=0&showTitle=false&size=11562&status=done&style=none&taskId=u437a8c50-18a6-4827-b639-e4d87089913&title=&width=454.4)
![image.png](https://img-blog.csdnimg.cn/img_convert/2f71cf19a23e9677aedaf9bd5351d8b8.png#clientId=uf346fa7b-5660-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=130&id=u64d2efcc&margin=[object Object]&name=image.png&originHeight=162&originWidth=646&originalType=binary&ratio=1&rotation=0&showTitle=false&size=9346&status=done&style=none&taskId=ub8a5f4dd-5b53-42d9-a9ec-835994b0478&title=&width=516.8)

  1. 再添加一个”仅主机模式“模式网卡用来主机连接虚拟机
  • 关机后添加

![1665620740010-605e4c30-4dcd-42b7-bc33-07536c88c9f1.png](https://img-blog.csdnimg.cn/img_convert/8b3a911a20a42505719b963114504198.png#clientId=u6149ba6a-c6ea-4&crop=0&crop=0&crop=1&crop=1&from=drop&id=u8f7a1cca&margin=[object Object]&name=1665620740010-605e4c30-4dcd-42b7-bc33-07536c88c9f1.png&originHeight=287&originWidth=708&originalType=binary&ratio=1&rotation=0&showTitle=false&size=15958&status=done&style=none&taskId=ud46f1067-d518-4788-81fa-efb77ee543f&title=)

  1. 配置网卡
    1. 切换成root,进入到/etc/sysconfig/network-scripts目录

![image.png](https://img-blog.csdnimg.cn/img_convert/11f8ab43dbed1d61989e958a5f8aaedc.png#clientId=uf346fa7b-5660-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=34&id=u09057560&margin=[object Object]&name=image.png&originHeight=42&originWidth=375&originalType=binary&ratio=1&rotation=0&showTitle=false&size=3895&status=done&style=none&taskId=u1ebdb223-1d57-4ea0-8bae-62a52fd7b67&title=&width=300)

  1. 新建文件touch ifcfg-eth1ifcgh-后缀自定义
1. su - (切换root) 
2. cd /etc/sysconfig/network-scripts (切换到网卡配置文件)
3. touch ifcfg-eth1 (新建文件ifcgh-后缀自定义)
4. vim ifcfg-eth1
HWADDR=08:00:27:13:8F:5D #跟你的网卡MAC地址一样,每两个数字使用:隔开
TYPE=Ethernet
BOOTPROTO=static #设置成静态IP
DEFROUTE=yes
PEERDNS=yes
PEERROUTES=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_PEERDNS=yes
IPV6_PEERROUTES=yes
IPV6_FAILURE_FATAL=no
NAME=eh1 #文件后缀名
UUID=f1d8f70c-1520-495b-8c1b-f830823d0611
ONBOOT=yes
IPADDR=192.168.60.100 #要在你的有效的网段
NETMASKE=255.255.255.0
GATEWAY=192.168.6.1 #要跟你主机的一致 没有可以删掉
DNS1=192.168.6.1 #要跟你主机的一致 没有可以删掉
  1. 设置root账号连接
1. cd /etc/ssh
2. vim sshd_config (修改下面为yes,没有就添加上去)
	PasswordAuthentication yes
	PermitRootLogin yes
3. systemctl restart sshd (重启SSH服务)

![image.png](https://img-blog.csdnimg.cn/img_convert/111d867d935b373bcaea782080451050.png#clientId=u6149ba6a-c6ea-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=50&id=u0a89d7e1&margin=[object Object]&name=image.png&originHeight=63&originWidth=623&originalType=binary&ratio=1&rotation=0&showTitle=false&size=7776&status=done&style=none&taskId=u0d81e43f-8b83-4ade-9443-228cb280872&title=&width=498.4)

  1. 关掉防火墙
查看防火墙
命令:systemctl status firewalld.service
下方可度以查看得到“active(running)”,此时说明防火墙已经被打开了

关掉防火墙
命令:systemctl stop firewalld.service

然后再使用命令systemctl status firewalld.service,
在下方出现disavtive(dead),这权样就说明防火墙已经关闭。

永久关闭防火墙
再在命令行中输入命令:systemctl disable firewalld.service
  1. xshell连接成功
  • 如果xshell不能连接成功,主机电脑ping 虚拟机ip也不能成功,检查防火墙,ifcfg-eth1网卡配置,再不行把VitrualBox的主机网络管理器里面的网络配置删掉,重新新建

![image.png](https://img-blog.csdnimg.cn/img_convert/2196e41cc059cbb7f89c660c7015a659.png#clientId=ubbb32bda-d54a-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=18&id=u7dfa8075&margin=[object Object]&name=image.png&originHeight=22&originWidth=296&originalType=binary&ratio=1&rotation=0&showTitle=false&size=1284&status=done&style=none&taskId=udf68c390-d566-489a-9482-792815c4259&title=&width=236.8)

Vagrant常用命令


  • vargrant -h:查看 Vagrant 帮助
  • vagrant box list:查看所有盒子列表
  • vagrant box remove :删除指定名字的盒子

好博客
https://blog.csdn.net/hunter1000/article/details/122871352
https://blog.csdn.net/daxiang52/article/details/103518589

本文含有隐藏内容,请 开通VIP 后查看

网站公告

今日签到

点亮在社区的每一天
去签到