下载virtualBox
下载地址:https://www.virtualbox.org/wiki/Downloads
下载vagrant
下载地址:https://www.vagrantup.com/downloads.html
vagrant 使用说明
查看版本号
vagrant -v
Vagrant 2.2.6
安装完毕,接下来开始体验;
体验
注意:体验之前请再三确认virtualbox已经安装成功,最好是双击图标启动virtualbox,确认能够启动成功;
vagrant box list
There are no installed boxes! Use `vagrant box add` to add some.
检查已有的虚拟机列表vagrant box list,提示还没有任何虚拟机:
查找镜像
去虚拟机镜像仓库找个合适的镜像,地址是:https://app.vagrantup.com/boxes/search ,如下图,咱们用centos来完成初次体验吧,点击红框位置:
在新页面中,点击下图红框中的"New"按钮,即可看到使用该虚拟机的命令:
vagrant init
按照上图的提示,在命令行执行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.
打开Vagrantfile看看,如下图,只有少量信息,看来主要用的都是默认配置:
启动
执行命令启动虚拟机,接下来需要等待10分钟左右,控制台输出以下信息表示启动虚拟机成功
➜ virtualBoxVMs 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 '1811.02' is up to date...
==> default: A newer version of the box 'centos/7' for provider 'virtualbox' is
==> default: available! You currently have version '1811.02'. The latest is version
==> default: '1905.1'. Run `vagrant box update` to update.
==> default: Setting the name of the VM: virtualBoxVMs_default_1576860342143_34384
==> 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] No Virtualbox Guest Additions installation found.
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: ftp.sjtu.edu.cn
* extras: ftp.sjtu.edu.cn
* updates: mirrors.163.com
Resolving Dependencies
--> Running transaction check
---> Package centos-release.x86_64 0:7-6.1810.2.el7.centos will be updated
---> Package centos-release.x86_64 0:7-7.1908.0.el7.centos will be an update
--> Finished Dependency Resolution
Dependencies Resolved
================================================================================
Package Arch Version Repository Size
================================================================================
Updating:
centos-release x86_64 7-7.1908.0.el7.centos base 26 k
Transaction Summary
================================================================================
Upgrade 1 Package
Total download size: 26 k
Downloading packages:
No Presto metadata available for base
Public key for centos-release-7-7.1908.0.el7.centos.x86_64.rpm is not installed
warning: /var/cache/yum/x86_64/7/base/packages/centos-release-7-7.1908.0.el7.centos.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID f4a80eb5: NOKEY
Retrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
Importing GPG key 0xF4A80EB5:
Userid : "CentOS-7 Key (CentOS 7 Official Signing Key) <security@centos.org>"
Fingerprint: 6341 ab27 53d7 8a78 a7c2 7bb1 24c6 a8a7 f4a8 0eb5
Package : centos-release-7-6.1810.2.el7.centos.x86_64 (@anaconda)
From : /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Updating : centos-release-7-7.1908.0.el7.centos.x86_64 1/2
Cleanup : centos-release-7-6.1810.2.el7.centos.x86_64 2/2
Verifying : centos-release-7-7.1908.0.el7.centos.x86_64 1/2
Verifying : centos-release-7-6.1810.2.el7.centos.x86_64 2/2
Updated:
centos-release.x86_64 0:7-7.1908.0.el7.centos
Complete!
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: ftp.sjtu.edu.cn
* extras: ftp.sjtu.edu.cn
* updates: mirrors.163.com
ssh登录:
vagrant ssh
登录成功后,查看虚拟机操作系统版本,可见是centos7.6:
[vagrant@localhost ~]$ cat /etc/redhat-release
CentOS Linux release 7.7.1908 (Core)
退出ssh,回到控制台:
exit
基本命令
- 关闭虚拟机:
vagrant halt
- 启动虚拟机:
vagrant up
- 重启虚拟机:
vagrant reload
- 删除虚拟机:
vagrant destroy
修改配置
接下来尝试修改虚拟机的配置文件Vagrantfile,在Vagrantfile中添加如下图红框中的内容,作用是将虚拟机内存设置为2G:
执行vagrant reload重启虚拟机,然后执行vagrant ssh进入虚拟机,执行free -m查看内存情况,可见设置已经生效:
(base) zhaoqindeMBP:18 zhaoqin free -m
total used free shared buff/cache available
Mem: 1838 70 1655 8 112 1624
Swap: 2047 0 2047
至此,Mac下的vagrant安装和体验就完成了,希望本文能给您一些参考,帮助您快速搭建虚拟机环境。