virtualbox+vagrant创建CentOS-7虚拟机

张开发
2026/4/10 5:39:06 15 分钟阅读

分享文章

virtualbox+vagrant创建CentOS-7虚拟机
参考资料参考视频 https://ke.gupaoedu.cn/play/288/5/37603?phaseId6准备材料1. 下载安装vagrant01 访问Vagrant官网https://www.vagrantup.com/02 点击DownloadWindowsMacOSLinux等03 选择对应的版本04 傻瓜式安装05 命令行输入vagrant测试是否安装成功2. 下载安装virtual box01 访问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/7my-centos/7为镜像名可自定义此时会在当前目录下生成Vagrantfile同时指定使用的镜像为centos/7然后用notepad打开修改相应的关键配置# -*- mode: ruby -*- # vi: set ftruby : # All Vagrant configuration is done below. The 2 in Vagrant.configure # configures the configuration version (we support older styles for # backwards compatibility). Please dont change it unless you know what # youre 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 配置好文件后将镜像名称和下载的镜像映射关联起来执行以下命令将配置文件中的镜像名和具体位置关联在一起vagrant box add my-centos/7 D:\software\virtualbox.box这个时候打开Oracle VM VirtualBox可以看到虚拟机04 然后执行命令安装启动虚拟机一些vagrant常用的命令vagrant box list 查看本地的box vagrant up 启动Vagrantfile文件对应的虚拟机 vagrant halt 关闭虚拟机 vagrant ssh 进入创建的虚拟机中 vagrant status 查看虚拟机的状态 vagrant destroy 删除虚拟机查看vagrant所有的box启动虚拟机这里可能报错Call to WHvSetupPartition failed: ERROR_SUCCESS (Last0xc000000d/87) (VERR_NEM_VM_CREATE_FAILED).应该是docker或者虚拟机的冲突执行下面命令并重启bcdedit /set hypervisorlaunchtype off创建账号修改密码连接在文件夹下cmd查看配置vagrant ssh-config关注:Hostname Port IdentityFileIP:127.0.0.1port:2222用户名:vagrant密码:vagrant文件:Identityfile指向的文件private-key在文件夹下cmd进入系统vagrant ssh切换到根目录sudo -i修改PasswordAuthentication yesvi /etc/ssh/sshd_configesc并且保存退出passwd修改密码比如abc123重启网卡systemctl restart sshd然后就可以用finalShell进行连接查看IP地址ip a默认端口22root的密码前面也改过连接成功

更多文章