カタカタブログ

SIerで働くITエンジニアがカタカタした記録を残す技術ブログ。Java, Oracle Database, Linuxが中心です。たまに数学やデータ分析なども。

MacにVagrant (1.6.5) でCent OS(6.4)をインストールしてみる

1年ほど(?)前にVagrantを少し触ってみて放置していたが、Macにまた仮想マシンを入れたくなって、せっかくなので最新のVagrantで入れてみることにした。当時はVagrant 1.0.7をgemでインストールしていたが、今はインストーラがあるよう(当時からもあったのかも?)。簡単な方法があるのは素晴らしい!、ということで公式サイトからインストーラ(.dmg)を入手してインストールすることにする。なお、仮想ソフトウェアはインストール済みのVirtual Box 4.2.2を使用する。

古いバージョンのVagrantをアンインストール

その前に、古いVagrantをアンインストールしておく。

$ gem uninstall vagrant
Remove executables:
vagrant

in addition to the gem? [Yn]  Y
Removing vagrant
Successfully uninstalled vagrant-1.0.7

最新のVagrant 1.6.5をインストール

さて、最新のVagrantを以下の公式サイトから入手する。
https://www.vagrantup.com/downloads.html
MACの現在の最新(vagrant_1.6.5.dmg)をダウンロードしインストールする。

Vagrantインストール後にコマンドを実行すると、バージョンが1.6.5であることを確認。正しくインストールできたよう。

$ vagrant -v
Vagrant 1.6.5

続いて仮想マシンのテンプレートであるBoxをvagrnatbox.esのサイトから入手する。以下のURLのvagrantboxから、今回は64bit版のCentOS6.4 Minimalのテンプレートを選択し、のURLをコピーする。
http://www.vagrantbox.es/

f:id:osn_th:20141020064221p:plain
http://developer.nrel.gov/downloads/vagrant-boxes/CentOS-6.4-x86_64-v20131103.box

以下のコマンドでBoxを取得する。

$ vagrant box add centos6_4 http://developer.nrel.gov/downloads/vagrant-boxes/CentOS-6.4-x86_64-v20131103.box
==> box: Adding box 'centos6_4' (v0) for provider:
    box: Downloading: http://developer.nrel.gov/downloads/vagrant-boxes/CentOS-6.4-x86_64-v20131103.box
    box: Progress: 61% (Rate: 2543k/s, Estimated time remaining: 0:01:21)load: 2.13  cmd: curl 8111 waiting 1.40u 4.62s
==> box: Successfully added box 'centos6_4' (v0) for 'virtualbox'!

仮想マシン初期化する。仮想マシンごとにディレクトリを作成する必要があるので、$HOME/vagrant_vms/<仮想マシン名>という構成でディレクトリを切ることにする。

$ mkdir vagrant_vms
$ cd vagrant_vms/
$ mkdir centos6_4
$ cd centos6_4/

仮想マシンを初期化する。仮想マシン名は「centos6_4」とする。

$ vagrant init centos6_4
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.

initが成功し、vagrantファイルが作成されたことを確認

$ ls
Vagrantfile

仮想マシンを起動する。

$ vagrant up
Bringing machine 'default' up with 'virtualbox' provider...==> default: Importing base box 'centos6_4'...==> default: Matching MAC address for NAT networking...==> default: Setting the name of the VM: centos6_4_default_1413703643064_7382==> default: Clearing any previously set network interfaces...==> default: Preparing network interfaces based on configuration...
    default: Adapter 1: nat==> default: Forwarding ports...
    default: 22 => 2222 (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: Connection timeout. Retrying...==> 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.2
    default: VirtualBox Version: 4.2==> default: Mounting shared folders...
    default: /vagrant => /Users/user/vagrant_vms/centos6_4

仮想マシンに接続し、ログインできることを確認。

$ vagrant ssh
Welcome to your Vagrant-built virtual machine.
[vagrant@localhost ~]$ 

仮想マシンのネットワーク(IPアドレス)の設定をしておく。Vagrantfileをviで編集する。

$ vi Vagrantfile

以下の行をコメントインして保存

 27  config.vm.network"private_network", ip:"192.168.33.10"

この設定により、ホストオンリーアダプタとして仮想マシンのIPアドレスが”192.168.33.10"に設定される。

IPアドレス設定を有効化するために、仮想マシンを再起動する。

$ vagrant reload

とりあえずここまで。