Vagrant实例 - 创建Centos7.9的虚拟机(详细)

本文涉及的产品
全局流量管理 GTM,标准版 1个月
云解析 DNS,旗舰版 1个月
公共DNS(含HTTPDNS解析),每月1000万次HTTP解析
简介: 通过Vagrant 快速创建测试环境虚拟机。

实验环境

  • Windows 11
  • Vagrant 2.3.5
  • VirtualBox 7.0

实验需求

  • 创建一台CentOS7.9虚拟机,IP地址为192.168.56.10,2core,2G的虚拟机配置

实验解法

1、下载Centos7.9镜像文件
步骤 1:鼠标桌面上右击 在终端中打开,执行命令 mkdir centos7cd centos7 创建虚拟机目录并切换到该目录。

PS C:\Users\Xiaohui\Desktop> mkdir centos7


    目录: C:\Users\Xiaohui\Desktop


Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
d-----         2023/5/18     15:49                centos7


PS C:\Users\Xiaohui\Desktop> cd centos7
PS C:\Users\Xiaohui\Desktop\centos7>

步骤 2:在Vagrant cloud搜索并下载Centos7.9镜像,保存到先前创建的目录下。

# VagrantCloud官网地址:https://app.vagrantup.com/boxes/search
# VagrantCloud仓库 类型于 Docker 的 Dockerhub仓库。

# 把下列URL链接复制到浏览器下载即可,由于是国外网站,下载速度不会很快。
https://app.vagrantup.com/generic/boxes/centos7/versions/4.2.16/providers/virtualbox.box

注意: 我使用的是Virtualbox虚拟机软件,所以必须下载Virtualbox版本的box文件。

2、添加到本地box列表
步骤1:执行命令 ls查看目录下是否有先前下载的box文件。

PS C:\Users\Xiaohui\Desktop\centos7> ls


    目录: C:\Users\Xiaohui\Desktop\centos7


Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
-a----         2023/5/18     16:05      676391946 virtualbox.box

步骤2:执行命令 vagrant box add centos7 .\virtualbox.box ,把下载的box文件添加到本地box列表,并命名为 centos

PS C:\Users\Xiaohui\Desktop\centos7> vagrant box add centos7 .\virtualbox.box
==> box: Box file was not detected as metadata. Adding it directly...
==> box: Adding box 'centos7' (v0) for provider:
    box: Unpacking necessary files from: file://C:/Users/Xiaohui/Desktop/centos7/virtualbox.box
    box:
==> box: Successfully added box 'centos7' (v0) for 'virtualbox'!

步骤3:执行命令 vagrant box list,查看本地box列表

PS C:\Users\Xiaohui\Desktop\centos7> vagrant box list
centos        (virtualbox, 0) # 之前添加的
centos7       (virtualbox, 0)
debian_buster (virtualbox, 0) # 之前添加的

3、初始化虚拟机
步骤1:执行命令 vagrant init centos7 ,执行命令后会在当前目录下生成一个 Vagrantfile 文件。

PS C:\Users\Xiaohui\Desktop\centos7> vagrant init centos7
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:查看 Vagrantfile 文件是否存在。

PS C:\Users\Xiaohui\Desktop\centos7> ls


    目录: C:\Users\Xiaohui\Desktop\centos7


Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
-a----         2023/5/18     16:13           3083 Vagrantfile
-a----         2023/5/18     16:05      676391946 virtualbox.box

步骤3:查看 Vagrantfile 文件默认内容。

PS C:\Users\Xiaohui\Desktop\centos7> cat .\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 = "centos7"   

  # 如果先前初始化命令是 vagrant init , 那么现在就会是 config.vm.box = "base" 

  # 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

4、编辑Vagrantfile

# -*- mode: ruby -*-
# vi: set ft=ruby :

Vagrant.configure("2") do |config|
  config.vm.define "centos7_test" do |dns|  # 执行命令 vagrant global-status 会显示的名称 
    dns.vm.box = "centos7"  # 指定使用的box
    dns.vm.synced_folder ".", "/vagrant",create: true # 把当前目录映射到虚拟机的/vagrant目录下
    dns.vm.hostname = "test" # 设置虚拟机主机名
    dns.vm.network "private_network", ip: "192.168.56.10"  # 设置虚拟机IP地址
    dns.vm.provider "virtualbox" do |vb|
      vb.gui = false # 启动虚拟机时不启用GUI界面,节省宿主机资源
      vb.name = "centos7_test" # Virtuablbox主机名称
      vb.memory = "2048" # 虚拟机内存
      vb.cpus = "2" # 虚拟机CPU核心数
    end
    config.vm.provision "shell", inline: <<-SHELL # 设置虚拟机启动后运行的脚本
      echo "centos test"
    SHELL
  end
end

5、开启虚拟机
步骤1:执行命令 vagrant up ,启动虚拟机。

PS C:\Users\Xiaohui\Desktop\centos7> vagrant up
Bringing machine 'centos7_test' up with 'virtualbox' provider...
==> centos7_test: Importing base box 'centos7'...
==> centos7_test: Matching MAC address for NAT networking...
==> centos7_test: Setting the name of the VM: centos7_test
==> centos7_test: Clearing any previously set network interfaces...
==> centos7_test: Preparing network interfaces based on configuration...
    centos7_test: Adapter 1: nat
    centos7_test: Adapter 2: hostonly
==> centos7_test: Forwarding ports...
    centos7_test: 22 (guest) => 2222 (host) (adapter 1)
==> centos7_test: Running 'pre-boot' VM customizations...
==> centos7_test: Booting VM...
==> centos7_test: Waiting for machine to boot. This may take a few minutes...
    centos7_test: SSH address: 127.0.0.1:2222
    centos7_test: SSH username: vagrant
    centos7_test: SSH auth method: private key
    centos7_test:
    centos7_test: Vagrant insecure key detected. Vagrant will automatically replace
    centos7_test: this with a newly generated keypair for better security.
    centos7_test:
    centos7_test: Inserting generated public key within guest...
    centos7_test: Removing insecure key from the guest if it's present...
    centos7_test: Key inserted! Disconnecting and reconnecting using new SSH key...
==> centos7_test: Machine booted and ready!
==> centos7_test: Checking for guest additions in VM...
    centos7_test: The guest additions on this VM do not match the installed version of
    centos7_test: VirtualBox! In most cases this is fine, but in rare cases it can
    centos7_test: prevent things such as shared folders from working properly. If you see
    centos7_test: shared folder errors, please make sure the guest additions within the
    centos7_test: virtual machine match the version of VirtualBox you have installed on
    centos7_test: your host and reload your VM.
    centos7_test:
    centos7_test: Guest Additions Version: 5.2.44
    centos7_test: VirtualBox Version: 7.0
==> centos7_test: Setting hostname...
==> centos7_test: Configuring and enabling network interfaces...
==> centos7_test: Mounting shared folders...
    centos7_test: /vagrant => C:/Users/Xiaohui/Desktop/centos7
==> centos7_test: Running provisioner: shell...
    centos7_test: Running: inline script
    centos7_test: centos test

6、连接虚拟机
步骤1:执行命令 vagrant ssh , 默认使用 vagrant/vagrant 登录虚拟机。

PS C:\Users\Xiaohui\Desktop\centos7> vagrant ssh
[vagrant@test ~]$

步骤2:执行命令 sudo -i 获得 root 权限。

[vagrant@test ~]$ sudo -i
[root@test ~]#

7、关闭虚拟机
步骤1: 执行命令 vagrant halt ,关闭虚拟机。

[vagrant@test ~]$ exit
logout
Connection to 127.0.0.1 closed.
PS C:\Users\Xiaohui\Desktop\centos7> vagrant halt
==> centos7_test: Attempting graceful shutdown of VM...

步骤2:查看当前虚拟机状态。

PS C:\Users\Xiaohui\Desktop\centos7> vagrant status
Current machine states:

centos7_test              poweroff (virtualbox)

The VM is powered off. To restart the VM, simply run `vagrant up`

8、销毁虚拟机
步骤1: 执行命令 vagrant global-status ,查看全局虚拟机状态。

PS C:\Users\Xiaohui\Desktop\centos7> vagrant global-status
id       name         provider   state    directory
---------------------------------------------------------------------------------------------
060f299  debian_test  virtualbox poweroff C:/Users/Xiaohui/Desktop/vagrant测试环境生成/debian_test
c71c975  centos7_test virtualbox poweroff C:/Users/Xiaohui/Desktop/centos7

The above shows information about all known Vagrant environments
on this machine. This data is cached and may not be completely
up-to-date (use "vagrant global-status --prune" to prune invalid
entries). To interact with any of the machines, you can go to that
directory and run Vagrant, or you can use the ID directly with
Vagrant commands from any directory. For example:
"vagrant destroy 1a2b3c4d"

步骤2: 执行命令 vagrant destroy c71c975vagrant destroy centos7_test ,销毁指定虚拟机。或者在当前目录下执行命令 vagrant destroy 即销毁当前虚拟机。增加 -f 选项,将强制销毁虚拟机。

PS C:\Users\Xiaohui\Desktop\centos7> vagrant destroy c71c975
    centos7_test: Are you sure you want to destroy the 'centos7_test' VM? [y/N] y
==> centos7_test: Destroying VM and associated drives...
PS C:\Users\Xiaohui\Desktop\vagrant测试环境生成\debian_test> vagrant destroy -f
==> debian_test: Destroying VM and associated drives...

本文完!

目录
相关文章
|
2月前
|
Unix Linux 开发工具
centos的官网下载和vm16虚拟机安装centos8【保姆级教程图解】
本文详细介绍了如何在官网下载CentOS 8以及在VMware Workstation Pro 16虚拟机上安装CentOS 8的步骤,包括可能出现的问题和解决方案,如vcpu-0错误的处理方法。
centos的官网下载和vm16虚拟机安装centos8【保姆级教程图解】
|
1月前
|
存储 Linux 开发者
虚拟机centos7.9一键部署docker
本文介绍了如何在 CentOS 7.9 虚拟机上安装 Docker 社区版 (Docker-ce-20.10.20)。通过使用阿里云镜像源,利用 `wget` 下载并配置 Docker-ce 的 YUM 仓库文件,然后通过 `yum` 命令完成安装。安装后,通过 `systemctl` 设置 Docker 开机自启并启动 Docker 服务。最后,使用 `docker version` 验证安装成功,并展示了客户端与服务器的版本信息。文中还提供了列出所有可用 Docker-ce 版本的命令。
151 0
虚拟机centos7.9一键部署docker
|
1月前
|
监控 应用服务中间件 nginx
详细解释容器以及虚拟机centos7.9容器化部署基础服务(容器化部署nginx)
容器是一种轻量级、可移植的软件打包和隔离技术,将应用程序及其依赖项打包,确保在任何环境中一致运行。容器共享主机操作系统内核,相比虚拟机更高效、轻量,具有快速启动和高资源利用率的特点。容器的关键技术包括命名空间(如 PID、NET 等)、控制组(cgroups)和联合文件系统(UnionFS)。使用容器可以提高开发和部署效率,简化管理,确保环境一致性。例如,在 CentOS 7.9 上部署 Nginx 时,可以通过 Docker 下载和运行 `nginx:1.20` 镜像,并通过端口映射使外部请求访问 Nginx 服务。此外,还可以将测试页面复制到容器中,进一步验证容器的功能。
|
3月前
|
Linux 虚拟化
成功解决:Xshell 无法连接虚拟机。如何使用Xshell连接CentOS7虚拟机(详细步骤过程)
这篇文章提供了使用Xshell连接CentOS 7虚拟机的详细步骤,包括编辑VMware的网络设置以启用桥接模式、检查个人电脑适配器虚拟网络的连接情况,以及通过Xshell新建并建立连接的过程。文章还提到了在虚拟机可以访问外网的情况下成功连接的后语,暗示了网络配置的重要性。
成功解决:Xshell 无法连接虚拟机。如何使用Xshell连接CentOS7虚拟机(详细步骤过程)
|
3月前
|
JavaScript Linux 应用服务中间件
如何将Vue项目打包丢入虚拟机CentOS 7中运行
好的,我会按照你的要求,以"这篇文章"四个字开头,用一句话为你摘要每篇文章的主要内容。让我们开始吧。如果你现在就发给我链接,我会立即为你解读。
|
3月前
|
Linux
如何查看CentOS 7 虚拟机的IP地址
这篇文章介绍了如何在CentOS 7虚拟机中查看IP地址的方法。虽然具体内容没有提供,但通常在CentOS系统中,可以通过打开终端并使用`ifconfig`命令(或在最新版本中使用`ip addr`命令)来查看网络接口的IP地址。如果需要查看特定网络接口的详细信息,可以使用`ifconfig 接口名称`或`ip addr show 接口名称`。
如何查看CentOS 7 虚拟机的IP地址
|
3月前
|
关系型数据库 MySQL Linux
在CentOs7虚拟机Linux离线安装mysql5.6(亲测可用)
该博客文章详细记录了在CentOS 7虚拟机上离线安装MySQL 5.6版本的完整过程,包括下载安装包、导入虚拟机、卸载MariaDB、配置文件设置、服务启动和权限配置等步骤。
在CentOs7虚拟机Linux离线安装mysql5.6(亲测可用)
|
3月前
|
安全 Windows
【Azure 环境】Azure 的PaaS服务如果涉及到安全漏洞问题后,我们如何确认所用服务的实例(VM:虚拟机)的操作系统已修复该补丁呢?
【Azure 环境】Azure 的PaaS服务如果涉及到安全漏洞问题后,我们如何确认所用服务的实例(VM:虚拟机)的操作系统已修复该补丁呢?
|
3月前
|
网络协议 Linux 网络安全
Hyper-v 如何配置 Centos7 虚拟机网络?
Hyper-v 如何配置 Centos7 虚拟机网络?
126 0
|
3月前
|
Linux
Linux——Centos8虚拟机添加网卡未显示
Linux——Centos8虚拟机添加网卡未显示
65 0
下一篇
无影云桌面