virtualbox+vagrant创建CentOS-7虚拟机

参考资料:

参考视频: https://ke.gupaoedu.cn/play/288/5/37603?phaseId=6


准备材料:

1. 下载安装vagrant

01 访问Vagrant官网

https://www.vagrantup.com/

02 点击Download

Windows,MacOS,Linux等

03 选择对应的版本

04 傻瓜式安装

05 命令行输入vagrant,测试是否安装成功

2. 下载安装virtual box

01 访问VirtualBox官网

https://www.virtualbox.org/

02 选择左侧的"Downloads"

03 选择对应的操作系统版本

04 傻瓜式安装

05 win10中若出现安装virtualbox快完成时立即回滚,并提示安装出现严重错误

(1)打开服务

(2)找到Device Install Service和Device Setup Manager,然后启动

(3)再次尝试安装

3. 准备镜像

然后最好准备CentOS-7的镜像,virtualbox会自动从网上下载,但是速度很慢,所以建议提前准备。

首先需要登录网站: https://vagrantcloud.com/search,搜索**CentOS7进行下载**


安装虚拟镜像:

01 创建文件夹,并进入其中目录全路径不要有中文字符

02 在此目录下打开cmd,运行vagrant init my-centos/7

my-centos/7为镜像名可自定义,此时会在当前目录下生成Vagrantfile,同时指定使用的镜像为centos/7

然后用notepad++打开,修改相应的关键配置

ruby 复制代码
# -*- 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 = "my-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.33.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
    config.vm.provider "virtualbox" do |vb|
        vb.memory = "3000"
        vb.name= "jack-centos7"
        vb.cpus= 2
    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
  # Puppet, Chef, Ansible, Salt, and Docker 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

解释: 主要是修改下面的配置

1. 确认镜像名称
2. 设置网络模式(和主机共享网络)
3. 设置虚拟机的相关信息
3.1 provider 指定虚拟化工具

Vagrant 可以用 VMware 、virtualbox来创建虚拟机

3.2 内存、CPU、虚拟机名等

03 配置好文件后,将镜像名称和下载的镜像映射关联起来

执行以下命令将配置文件中的镜像名和具体位置关联在一起

vbnet 复制代码
vagrant box add my-centos/7 D:\software\virtualbox.box

这个时候,打开Oracle VM VirtualBox,可以看到虚拟机

04 然后执行命令安装启动虚拟机

一些vagrant常用的命令

bash 复制代码
vagrant box list   查看本地的box
vagrant up         启动Vagrantfile文件对应的虚拟机
vagrant halt       关闭虚拟机
vagrant ssh        进入创建的虚拟机中
vagrant status     查看虚拟机的状态
vagrant destroy    删除虚拟机

查看vagrant所有的box

启动虚拟机

这里可能报错:

Call to WHvSetupPartition failed: ERROR_SUCCESS (Last=0xc000000d/87) (VERR_NEM_VM_CREATE_FAILED).

应该是docker或者虚拟机的冲突,执行下面命令,并重启

bash 复制代码
bcdedit /set hypervisorlaunchtype off

创建账号,修改密码,连接:

在文件夹下,cmd查看配置

bash 复制代码
vagrant ssh-config

关注:Hostname Port IdentityFile

IP:127.0.0.1

port:2222

用户名:vagrant

密码:vagrant

文件:Identityfile指向的文件private-key

在文件夹下,cmd进入系统

bash 复制代码
vagrant ssh

切换到根目录

bash 复制代码
sudo -i

修改PasswordAuthentication yes

bash 复制代码
vi /etc/ssh/sshd_config

esc并且保存退出

passwd修改密码,比如abc123

重启网卡

bash 复制代码
systemctl restart sshd

然后就可以用finalShell进行连接

查看IP地址

bash 复制代码
ip a

默认端口:22

root的密码前面也改过

连接成功

相关推荐
大树8816 小时前
金刚石散热越强,管路越先见顶
大数据·运维·服务器·人工智能·ai
摇滚侠16 小时前
Linux CentOS7 rpm 安装 MySQL 5.7
linux·运维·mysql
霸道流氓气质16 小时前
领域驱动设计(DDD)在 Spring Boot 微服务中的实践指南
运维·spring boot·微服务
Inhand陈工17 小时前
基于台达PLC与映翰通IG502的智慧水产养殖精准投喂与远程运维解决方案
运维·人工智能·物联网·阿里云·信息与通信
酣大智18 小时前
ARP代理--工作原理
运维·网络·arp·arp代理
shushangyun_18 小时前
2026年快消品B2B系统推荐:支持终端门店订货、促销政策自动化的工具?
java·运维·网络·数据库·人工智能·spring·自动化
施努卡机器视觉19 小时前
SNK施努卡侧滑门锁上滑轮总成自动化装配线,从零件到组件,全流程精密制造方案
运维·自动化·制造
AC赳赳老秦19 小时前
用 OpenClaw 搭建服务器故障应急响应系统,自动处理 80% 常见运维故障
android·运维·服务器·python·rxjava·deepseek·openclaw
2601_9618752420 小时前
决战申论100题2026|最新|范文
linux·容器·centos·debian·ssh·fabric·vagrant
java_cj20 小时前
深入kube-apiserver认证机制:从Bearer Token到mTLS的完整认证链解析
linux·运维·服务器·云原生·容器·kubernetes