IThaiのブログ

IT関連の話題やタイに関する様々なことを書いていきます。

vagrantで仮想環境を構築する

今更ですが、忘備録として残しておきます。

Download Vagrant - Vagrant

Downloads – Oracle VM VirtualBox

vagrantは1.7.1、virtualBoxは4.3.20でした。

Macにパッケージからウィザードに従ってインストールします。

f:id:kkv:20141220203505p:plain

インストールされたか確認します。

$ vagrant -v
Vagrant 1.7.1

OSのboxファイルをダウンロードします。

*今回は作成するだけなので、boxファイルは下記サイトからとってきます。

A list of base boxes for Vagrant - Vagrantbox.es

(CentOS 6.4 x86_64を作成します。)

$ vagrant box add centos64 https://github.com/2creatives/vagrant-centos/releases/download/v6.4.2/centos64-x86_64-20140116.box
==> box: Adding box 'centos64' (v0) for provider: 
    box: Downloading: https://github.com/2creatives/vagrant-centos/releases/download/v6.4.2/centos64-x86_64-20140116.box
==> box: Successfully added box 'centos64' (v0) for 'virtualbox'!

Vagrantfileを作成します。

$ vagrant init centos64

仮想環境を起動します。

$ vagrant up
Bringing machine 'default' up with 'virtualbox' provider... ==> default: Importing base box 'centos64'... ==> default: Matching MAC address for NAT networking... ==> default: Setting the name of the VM: user_default_1419077811106_33646 ==> 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: Warning: Connection timeout. Retrying... 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 its present... default: Key inserted! Disconnecting and reconnecting using new SSH key... ==> default: Machine booted and ready! ==> default: Checking for guest additions in VM... ==> default: Mounting shared folders... default: /vagrant => /Users/user

Warningが出ているが、キーペアを作成しているっぽいので気にしなくても良さそう。

実際にsshで接続してみます。

$ vagrant ssh
[vagrant@vagrant-centos64 ~]$

無事にログインできました。

また、仮想環境を閉じるときは

$ vagrant halt
==> default: Attempting graceful shutdown of VM...

です。

box addしたリストを表示したいときは、

$ vagrant box list
centos64 (virtualbox, 0)
centos7  (virtualbox, 0)

boxを削除したいときは

$ vagrant box remove centos7
Removing box 'centos7' (v0) with provider 'virtualbox'...

boxを変更したいときは、Vagrantfileを削除して再度initする

$ rm Vagrantfile 
$ vagrant init centos64 
Vagrant入門ガイド

Vagrant入門ガイド