window 10 使用vagrant 在virtualbox中安装centos7

安装部署 同时被 2 个专栏收录
23 篇文章 0 订阅
15 篇文章 0 订阅

下载 安装virtualbox

下载地址

virtualbox 官网: https://www.virtualbox.org

点击 Download VirtualBox xx

在这里插入图片描述

选择 Windows hosts

在这里插入图片描述

安装VirtualBox

  • 需要打开CPU虚拟化
    在这里插入图片描述

  • 直接下一步默认安装即可
    在这里插入图片描述

下载安装Vagran

下载地址

https://www.vagrantup.com/downloads
在这里插入图片描述

安装

  • 默认下一步安装即可
    在这里插入图片描述

  • 安装成功后重启

  • 验证安装是否成功
    cmd 窗口中输入 vagrant -v

C:\Users\herion>vagrant -v
Vagrant 2.2.16

使用 vagrant 安装centos7

寻找镜像

vagrant 镜像仓库:https://app.vagrantup.com/boxes/search
在这里插入图片描述

初始镜像

cmd 中使用vagrant init centos/7 初始化centos

C:\Users\herion>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.

创建虚拟机

C:\Users\herion>vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Importing base box 'centos/7'...
==> default: Matching MAC address for NAT networking...
==> default: Checking if box 'centos/7' version '2004.01' is up to date...
==> default: Setting the name of the VM: herion_default_1624117318673_52353
==> 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:
    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: Configuring and enabling network interfaces...
==> default: Rsyncing folder: /cygdrive/c/Users/herion/ => /vagrant

创建完成后virtualbox自动启动虚拟机
在这里插入图片描述

连接虚拟机

C:\Users\herion> vagrant ssh
[vagrant@localhost ~]$ ls
  • 退出虚拟机
[vagrant@localhost ~]$ exit
logout
Connection to 127.0.0.1 closed.

虚拟机配置固定IP

  • window 10 中查看VirtualBox IP
C:\Users\herion>ipconfig

Windows IP 配置


以太网适配器 以太网 2:

   媒体状态  . . . . . . . . . . . . : 媒体已断开连接
   连接特定的 DNS 后缀 . . . . . . . :

以太网适配器 VirtualBox Host-Only Network:

   连接特定的 DNS 后缀 . . . . . . . :
   本地链接 IPv6 地址. . . . . . . . : fe80::d32:4706:1a61:38a0%11
   IPv4 地址 . . . . . . . . . . . . : 192.168.56.1
   子网掩码  . . . . . . . . . . . . : 255.255.255.0
   默认网关. . . . . . . . . . . . . :

以太网适配器 以太网:

   连接特定的 DNS 后缀 . . . . . . . :
   IPv6 地址 . . . . . . . . . . . . : 2409:8a1e:91c7:7020:91ab:7d62:cc63:6945
   临时 IPv6 地址. . . . . . . . . . : 2409:8a1e:91c7:7020:90d1:5929:23d2:2a4c
   本地链接 IPv6 地址. . . . . . . . : fe80::91ab:7d62:cc63:6945%4
   IPv4 地址 . . . . . . . . . . . . : 192.168.1.52
   子网掩码  . . . . . . . . . . . . : 255.255.255.0
   默认网关. . . . . . . . . . . . . : fe80::1%4
                                       192.168.1.1
  • 找到用户下Vagrantfile 文件
    在这里插入图片描述
  • 打开Vagrantfile 添加下面配置,配置网络信息
config.vm.network "private_network", ip: "192.168.56.56"
  • 修改完成后,重启启动vagrant
C:\Users\herion>vagrant reload
==> default: Attempting graceful shutdown of VM...
==> default: Checking if box 'centos/7' version '2004.01' is up to date...
==> default: Clearing any previously set forwarded ports...
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuratio

验证IP配置

  • 在虚拟机中查看ip
C:\Users\herion>vagrant ssh
Last login: Sat Jun 19 14:06:42 2021 from 10.0.2.2
Last login: Sat Jun 19 14:06:42 2021 from 10.0.2.2
[vagrant@localhost ~]$ ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> 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: <BROADCAST,MULTICAST,UP,LOWER_UP> 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 86064sec preferred_lft 86064sec
    inet6 fe80::5054:ff:fe4d:77d3/64 scope link
       valid_lft forever preferred_lft forever
3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 08:00:27:0e:70:c6 brd ff:ff:ff:ff:ff:ff
    inet 192.168.56.56/24 brd 192.168.56.255 scope global noprefixroute eth1
       valid_lft forever preferred_lft forever
    inet6 fe80::a00:27ff:fe0e:70c6/64 scope link
       valid_lft forever preferred_lft forever
  • window 中ping 虚拟机ip
C:\Users\herion>ping 192.168.56.56

正在 Ping 192.168.56.56 具有 32 字节的数据:
来自 192.168.56.56 的回复: 字节=32 时间<1ms TTL=64
来自 192.168.56.56 的回复: 字节=32 时间<1ms TTL=64
来自 192.168.56.56 的回复: 字节=32 时间<1ms TTL=64
来自 192.168.56.56 的回复: 字节=32 时间<1ms TTL=64

192.168.56.56 的 Ping 统计信息:
    数据包: 已发送 = 4,已接收 = 4,丢失 = 0 (0% 丢失),
往返行程的估计时间(以毫秒为单位):
    最短 = 0ms,最长 = 0ms,平均 = 0ms
  • 虚拟机ping window IP
[vagrant@localhost ~]$ ping 192.168.56.1
PING 192.168.56.1 (192.168.56.1) 56(84) bytes of data.
64 bytes from 192.168.56.1: icmp_seq=1 ttl=128 time=0.110 ms
64 bytes from 192.168.56.1: icmp_seq=2 ttl=128 time=0.187 ms
64 bytes from 192.168.56.1: icmp_seq=3 ttl=128 time=0.128 ms

开启远程登陆root

修改sshd_config

开启远程登陆,vi /etc/ssh/sshd_config 修改使用密码认证,root默认密码为vagrant

PermitRootLogin yes 
PasswordAuthentication yes

然后重启SSHD

systemctl restart sshd

异常

异常信息

C:\Users\herion>vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Checking if box 'centos/7' version '2004.01' is up to date...
==> 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/c/Users/herion/ => /vagrant
There was an error when attempting to rsync a synced folder.
Please inspect the error message below for more info.

Host path: /cygdrive/c/Users/herion/
Guest path: /vagrant
Command: "rsync" "--verbose" "--archive" "--delete" "-z" "--copy-links" "--chmod=ugo=rwX" "--no-perms" "--no-owner" "--no-group" "--rsync-path" "sudo rsync" "-e" "ssh -p 2222 -o LogLevel=FATAL    -o IdentitiesOnly=yes -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -i 'C:/Users/herion/.vagrant/machines/default/virtualbox/private_key'" "--exclude" ".vagrant/" "/cygdrive/c/Users/herion/" "vagrant@127.0.0.1:/vagrant"
Error: rsync: [sender] send_files failed to open "/cygdrive/c/Users/herion/NTUSER.DAT": Device or resource busy (16)
rsync: [sender] send_files failed to open "/cygdrive/c/Users/herion/ntuser.dat.LOG1": Device or resource busy (16)
rsync: [sender] send_files failed to open "/cygdrive/c/Users/herion/ntuser.dat.LOG2": Device or resource busy (16)
rsync: write failed on "/vagrant/.VirtualBox/selectorwindow.log": No space left on device (28)
rsync error: error in file IO (code 11) at receiver.c(393) [receiver=3.1.2]

异常原因

使用vagrant创建虚拟机的时候,vagrant会默认的把Vagrantfile文件下的所有同级文件和目录都复制到创建的centos7的/vagrant目录下,导致虚拟机的硬盘爆满
在这里插入图片描述

解决方案

  • 找到当前虚拟机Vagrantfile
    在这里插入图片描述

  • 在Vagrantfile 同级目录中创建centos7 目录
    注意不是 在.vagrant.d 的Vagrantfile的同级目录中创建
    在这里插入图片描述

  • 修改Vagrantfile 文件
    修改前

Vagrant.configure("2") do |config|
  config.vm.base_mac = "5254004d77d3"
  config.vm.synced_folder ".", "/vagrant", type: "rsync"
end

修改后

Vagrant.configure("2") do |config|
  config.vm.base_mac = "5254004d77d3"
  config.vm.synced_folder "./centos7 ", "/vagrant", type: "rsync"
end
  • 重新初始化拟机
    vagrant init centos/7
C:\Users\herion>vagrant init centos/7
`Vagrantfile` already exists in this directory. Remove it before
running `vagrant init`.
  • 重新加载
    vagrant reload
C:\Users\herion>vagrant reload
==> default: Checking if box 'centos/7' version '2004.01' is up to date...
==> 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/c/Users/herion/centos7/ => /vagrant
  • 0
    点赞
  • 0
    评论
  • 0
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

©️2021 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值