Mac で Vagrant

急にLinux環境が必要になったのでせっかくだからVagrantで用意してみようかというメモ。

環境

Vagrant のインストール

Vagrant サイトから Mac OS X 用の dmg ファイルをダウンロードしてインストールする。

www.vagrantup.com

バージョン確認vagrant -vをしてインストールが完了したことを確認する。

$ vagrant -v
Vagrant 1.8.1

CentOS のBOXをインストール

事前に VirtualBox が必要となるので dmg ファイルをダウンロードしてインストールしておく。

Oracle VM VirtualBox - Downloads | Oracle Technology Network | Oracle

BOXは

A list of base boxes for Vagrant - Vagrantbox.es

にて提供されているCentOSVagrantコマンドvagrant box add {title} {URL}でインストールする。

$ vagrant box add centos71 https://github.com/CommanderK5/packer-centos-template/releases/download/0.7.1/vagrant-centos-7.1.box
==> box: Box file was not detected as metadata. Adding it directly...
==> box: Adding box 'centos71' (v0) for provider: 
    box: Downloading: https://github.com/CommanderK5/packer-centos-template/releases/download/0.7.1/vagrant-centos-7.1.box
==> box: Successfully added box 'centos71' (v0) for 'virtualbox'!

インストールしたBOXをVagrantコマンドvagrant box listで確認する。

$ vagrant box list
centos71 (virtualbox, 0)

インストールしたBOXをディレクトリcentos71作成して、Vagrantコマンドvagrant initでそのディレクトリに初期化する。

$ mkdir centos71
$ cd centos71/
$ vagrant init centos71
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.

初期化したBOXをVagrantコマンドvagrant upで起動する。

$ vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Importing base box 'centos71'...
==> default: Matching MAC address for NAT networking...
==> default: Setting the name of the VM: centos71_default_1462176011682_74019
==> 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: Warning: Remote connection disconnect. Retrying...
    default: Warning: Remote connection disconnect. Retrying...
    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: The guest additions on this VM do not match the installed version of
    default: VirtualBox! In most cases this is fine, but in rare cases it can
    default: prevent things such as shared folders from working properly. If you see
    default: shared folder errors, please make sure the guest additions within the
    default: virtual machine match the version of VirtualBox you have installed on
    default: your host and reload your VM.
    default: 
    default: Guest Additions Version: 4.3.30
    default: VirtualBox Version: 5.0
==> default: Mounting shared folders...
    default: /vagrant => /Users/tokunaga.keita/centos71

起動したVagrantBOXの状態をVagrantコマンドvagrant statusで確認する。

$ vagrant status
Current machine states:

default                   running (virtualbox)

The VM is running. To stop this VM, you can run `vagrant halt` to
shut it down forcefully, or you can run `vagrant suspend` to simply
suspend the virtual machine. In either case, to restart it again,
simply run `vagrant up`.

起動が確認できたのでVagrantコマンドvagrant sshで接続してみる。

$ vagrant ssh