基于 VirtualBox 与 Vagrant 的 CentOS7 安装教程

633 阅读5分钟

这篇文章主要记录了我使用 VirtualBox 与 Vagrant 安装 CentOS7 的过程,希望可以帮助到大家~

一、VirtualBox 安装

首先在 VirtualBox 官网 下载 安装包,然后根据指引安装即可。

二、Vagrant 安装

首先在 Vagrant 官网 下载 安装包,然后根据指引安装即可。

由于 Vagrant 没有图形界面,可以通过命令行的方式执行 vagrant version 检查是否安装成功。

> vagrant version
Installed Version: 2.2.19
Latest Version: 2.2.19

三、CentOS7 安装

接下来开始安装 CentOS7,首先打开 Vagrant官方镜像库,找到 CentOS7box 名称,然后按下面步骤执行。

1、执行 vagrant init centos/7 命令,初始化 Vagrantfile 文件。

> 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.

2、执行 vagrant up 命令,启动虚拟机。

> vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Box 'centos/7' could not be found. Attempting to find and install...
    default: Box Provider: virtualbox
    default: Box Version: >= 0
==> default: Loading metadata for box 'centos/7'
    default: URL: https://vagrantcloud.com/centos/7
==> default: Adding box 'centos/7' (v2004.01) for provider: virtualbox
    default: Downloading: https://vagrantcloud.com/centos/boxes/7/versions/2004.01/providers/virtualbox.box
Download redirected to host: cloud.centos.org

由于需要去官方镜像库下载镜像,这块可能会卡住比较久,也没有进度条显示下载进度,所以我们可以通过下载工具的方式下载镜像。

3、下载 .box 文件后我们需要将其安装到 vagrant 中去。

> vagrant box add C:\virtualbox.box --name centos/7
==> box: Box file was not detected as metadata. Adding it directly...
==> box: Adding box 'centos/7' (v0) for provider:
    box: Unpacking necessary files from: file:///C:/virtualbox.box
    box:
==> box: Successfully added box 'centos/7' (v0) for 'virtualbox'!

其中 --name centos/7 就是指定该 box 的名字,与官方镜像库中的命名一致。

4、添加完 box 之后继续执行 vagrant up 命令,启动虚拟机。

> vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Importing base box 'centos/7'...
==> default: Matching MAC address for NAT networking...
==> default: Setting the name of the VM: vagrant_default_1644569370891_51624
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
    default: Adapter 1: nat
==> 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: Rsyncing folder: /cygdrive/c/data/vagrant/ => /vagrant

出现 Rsyncing folder: /cygdrive/c/data/vagrant/ => /vagrant 时就可以看到 VirtualBox 中虚拟机已经运行了。

5、执行 vagrant ssh 命令,将会使用 vagrant 用户连接虚拟机。

四、XShell 远程连接

安装虚拟机之后,我们一般会使用 XShell 之类的工具进行远程连接,而不是直接在命令行上操作,所以我们需要对网络配置进行修改。

1、首先打开之前生成的 Vagrantfile 文件。

# -*- 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 = "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
  #
  # 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
  # Ansible, Chef, Docker, Puppet and Salt 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

可以看到目前生效配置的只有 config.vm.box = "centos/7" 这一段,指定了虚拟机使用的镜像名称。

2、私有网络配置

# Create a private network, which allows host-only access to the machine
# using a specific IP.
config.vm.network "private_network", ip: "192.168.56.201"

私有网络也就是 VirtualBox 中的 仅主机(Host-Only)网络 ,只允许宿主机访问虚拟机。我们可以设置一个固定 IP 来让宿主机访问,其中 IP 的网段参考宿主机的网络适配器:VirtualBox Host-Only Network

详细的文档可以参考官方文档:Private Networks

3、公共网络配置

# 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", ip: "192.168.31.201"

公共网络也就是 VirtualBox 中的 桥接网卡 ,相当于局域网中的另一个物理设备。我们可以设置一个固定 IP 来让局域网中的用户访问,其中 IP 的网段参考宿主机的网络适配器:WLAN

如果没有局域网中其他主机连接虚拟机的需求,可以不配置 public_network

详细的文档可以参考官方文档:Public Networks

4、执行 vagrant reload 命令,重新加载配置。

> vagrant reload
==> default: Attempting graceful shutdown of VM...
==> 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: Adapter 3: bridged
==> 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/data/vagrant/ => /vagrant
==> default: Machine already provisioned. Run `vagrant provision` or use the `--provision`
==> default: flag to force provisioning. Provisioners marked to run always will still run.

5、XShell 访问

首先通过 vagrant ssh 连接上虚拟机,su root 切换到系统用户(密码:vagrant,如果密码不正确也可以通过 sudo passwd root 修改密码)。

然后设置允许密码登录,执行 vi /etc/ssh/sshd_config 找到 PasswordAuthenticationno 修改为 yes,然后执行 systemctl restart sshd 重启。

最后便可以通过 XShell 使用上面设置的 IP 进行远程连接。

五、虚拟机扩容

1、还是打开之前生成的 Vagrantfile 文件,找到下面这一段:

  # 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.gui = false
  vb.memory = 4096
  vb.cpus = 4
end

其中 vb.gui 代表虚拟机启动时是否显示用户界面,vb.memory 代表虚拟机的内存,vb.cpus 代表虚拟机的CPU个数。

2、执行 vagrant reload 命令,重新加载配置。