前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Mac下vagrant从安装到体验

Mac下vagrant从安装到体验

作者头像
程序员欣宸
发布2019-08-29 14:17:55
1.6K0
发布2019-08-29 14:17:55
举报
文章被收录于专栏:实战docker实战docker

Mac下使用虚拟机时,通过vagrant+virtualbox的组合是个不错的方案,简便快捷;

安装步骤

  1. virtualbox官网下载,地址:https://www.virtualbox.org/wiki/Downloads ,如下,下载完毕后双击安装:
在这里插入图片描述
在这里插入图片描述
  1. vagrant官网下载,地址:https://www.vagrantup.com/downloads.html ,如下,下载完毕后双击安装:
在这里插入图片描述
在这里插入图片描述
  1. 安装完成后在命令行执行命令vagrant -v,查看版本号如下:
(base) zhaoqindeMBP:~ zhaoqin$ vagrant -v
Vagrant 2.2.5

安装完毕,接下来开始体验;

体验

注意:体验之前请再三确认virtualbox已经安装成功,最好是双击图标启动virtualbox,确认能够启动成功;

  1. 检查已有的虚拟机列表vagrant box list,提示还没有任何虚拟机:
(base) zhaoqindeMBP:~ zhaoqin$ vagrant box list
There are no installed boxes! Use `vagrant box add` to add some.
  1. 去虚拟机镜像仓库找个合适的镜像,地址是:https://app.vagrantup.com/boxes/search ,如下图,咱们用centos来完成初次体验吧,点击红框位置:
在这里插入图片描述
在这里插入图片描述
  1. 在新页面中,点击下图红框中的"New"按钮,即可看到使用该虚拟机的命令:
在这里插入图片描述
在这里插入图片描述
  1. 按照上图的提示,在命令行执行vagrant init centos/7,即可在当前目录生成此虚拟机的配置文件Vagrantfile:
base) zhaoqindeMBP:18 zhaoqin$ 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.
  1. 打开Vagrantfile看看,如下图,只有少量信息,看来主要用的都是默认配置:
在这里插入图片描述
在这里插入图片描述
  1. 执行命令启动虚拟机,接下来需要等待10分钟左右,控制台输出以下信息表示启动虚拟机成功:
(base) zhaoqindeMBP:18 zhaoqin$ 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' (v1905.1) for provider: virtualbox
    default: Downloading: https://vagrantcloud.com/centos/boxes/7/versions/1905.1/providers/virtualbox.box
    default: Download redirected to host: cloud.centos.org
==> default: Successfully added box 'centos/7' (v1905.1) for 'virtualbox'!
==> default: Importing base box 'centos/7'...
==> default: Matching MAC address for NAT networking...
==> default: Checking if box 'centos/7' version '1905.1' is up to date...
==> default: Setting the name of the VM: 18_default_1566103400563_66799
==> 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: /Users/zhaoqin/temp/201908/18/ => /vagrant
  1. ssh登录:
vagrant ssh
  1. 登录成功后,查看虚拟机操作系统版本,可见是centos7.6:
[vagrant@localhost ~]$ cat /etc/redhat-release 
CentOS Linux release 7.6.1810 (Core)
  1. 退出ssh,回到控制台:
exit

基本命令

  1. 关闭虚拟机:
vagrant halt
  1. 启动虚拟机:
vagrant up
  1. 重启虚拟机:
vagrant reload
  1. 删除虚拟机:
vagrant destroy

修改配置

  1. 接下来尝试修改虚拟机的配置文件Vagrantfile,在Vagrantfile中添加如下图红框中的内容,作用是将虚拟机内存设置为2G:
在这里插入图片描述
在这里插入图片描述
  1. 执行vagrant reload重启虚拟机,然后执行vagrant ssh进入虚拟机,执行free -m查看内存情况,可见设置已经生效:
(base) zhaoqindeMBP:18 zhaoqin$ vagrant ssh
Last login: Sun Aug 18 08:09:43 2019 from 10.0.2.2
[vagrant@localhost ~]$ free -m
              total        used        free      shared  buff/cache   available
Mem:           1838          70        1655           8         112        1624
Swap:          2047           0        2047

至此,Mac下的vagrant安装和体验就完成了,希望本文能给您一些参考,帮助您快速搭建虚拟机环境。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019年08月18日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 安装步骤
  • 体验
  • 基本命令
  • 修改配置
相关产品与服务
容器镜像服务
容器镜像服务(Tencent Container Registry,TCR)为您提供安全独享、高性能的容器镜像托管分发服务。您可同时在全球多个地域创建独享实例,以实现容器镜像的就近拉取,降低拉取时间,节约带宽成本。TCR 提供细颗粒度的权限管理及访问控制,保障您的数据安全。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档