安裝vagrant和virtualBox for Mac

下載virtualBox

下載地址:https://www.virtualbox.org/wiki/Downloads

下載vagrant

下載地址:https://www.vagrantup.com/downloads.html

vagrant 使用說明

查看版本號

vagrant -v
Vagrant 2.2.6

安裝完畢,接下來開始體驗(yàn);

體驗(yàn)
注意:體驗(yàn)之前請?jiān)偃_認(rèn)virtualbox已經(jīng)安裝成功,最好是雙擊圖標(biāo)啟動virtualbox,確認(rèn)能夠啟動成功;

vagrant box list
There are no installed boxes! Use `vagrant box add` to add some.

檢查已有的虛擬機(jī)列表vagrant box list,提示還沒有任何虛擬機(jī):

查找鏡像

去虛擬機(jī)鏡像倉庫找個(gè)合適的鏡像,地址是:https://app.vagrantup.com/boxes/search ,如下圖,咱們用centos來完成初次體驗(yàn)吧,點(diǎn)擊紅框位置:

vagrant01.png

在新頁面中,點(diǎn)擊下圖紅框中的"New"按鈕,即可看到使用該虛擬機(jī)的命令:


vagrant02.png

vagrant init

按照上圖的提示,在命令行執(zhí)行vagrant init centos/7,即可在當(dāng)前目錄生成此虛擬機(jī)的配置文件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看看,如下圖,只有少量信息,看來主要用的都是默認(rèn)配置:


vagrant03.png

啟動

執(zhí)行命令啟動虛擬機(jī),接下來需要等待10分鐘左右,控制臺輸出以下信息表示啟動虛擬機(jī)成功

? 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

登錄成功后,查看虛擬機(jī)操作系統(tǒng)版本,可見是centos7.6:

[vagrant@localhost ~]$ cat /etc/redhat-release
CentOS Linux release 7.7.1908 (Core)

退出ssh,回到控制臺:

exit

基本命令

  • 關(guān)閉虛擬機(jī):
vagrant halt
  • 啟動虛擬機(jī):
vagrant up
  • 重啟虛擬機(jī):
vagrant reload
  • 刪除虛擬機(jī):
vagrant destroy

修改配置

接下來嘗試修改虛擬機(jī)的配置文件Vagrantfile,在Vagrantfile中添加如下圖紅框中的內(nèi)容,作用是將虛擬機(jī)內(nèi)存設(shè)置為2G:


vagrant04.png

執(zhí)行vagrant reload重啟虛擬機(jī),然后執(zhí)行vagrant ssh進(jìn)入虛擬機(jī),執(zhí)行free -m查看內(nèi)存情況,可見設(shè)置已經(jīng)生效:
(base) zhaoqindeMBP:18 zhaoqinvagrant 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安裝和體驗(yàn)就完成了,希望本文能給您一些參考,幫助您快速搭建虛擬機(jī)環(huán)境。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容