【Vagrant】 Vagrant ~ 初期構築 / Linux 編 ~

■ 構築手順

※ 構築環境 : CentOS7
[1] 必要なライブラリのインストール
[2] VirtualBox のインストール
[3] Vagrantのインストール
[4] 仮想マシンを立ち上げる
[5] 仮想マシンにログインする

[1] 必要なライブラリのインストール

sudo yum install epel-release -y
sudo yum install dkms -y
sudo yum -y install gcc make qt libgomp patch
sudo yum -y install kernel-headers kernel-devel binutils glibc-headers glibc-devel fontforge

[2] VirtualBox のインストール

cd /etc/yum.repos.d/
sudo curl -OL http://download.virtualbox.org/virtualbox/rpm/el/virtualbox.repo

sudo yum search VirtualBox
...
VirtualBox-4.3.x86_64 : Oracle VM VirtualBox
VirtualBox-5.0.x86_64 : Oracle VM VirtualBox
VirtualBox-5.1.x86_64 : Oracle VM VirtualBox
VirtualBox-5.2.x86_64 : Oracle VM VirtualBox << 最新

# インストール
sudo yum -y install VirtualBox-5.2

[3] Vagrantのインストール

cd /tmp
curl -OL https://releases.hashicorp.com/vagrant/2.1.2/vagrant_2.1.2_x86_64.rpm
sudo yum install vagrant_2.1.2_x86_64.rpm -y
vagrant -v

[4] 仮想マシンを立ち上げる

mkdir ~/hello-vagrant
cd ~/hello-vagrant

# 以下のサイトから希望のOSを検索する(「centos7」で検索し「bento/centos-7.4」を見つける)
https://app.vagrantup.com/boxes/search
vagrant box add bento/centos-7.4

==> box: Loading metadata for box 'bento/centos-7.4'
    box: URL: https://vagrantcloud.com/bento/centos-7.4
This box can work with multiple providers! The providers that it
can work with are listed below. Please review the list and choose
the provider you will be working with.

1) hyperv
2) parallels
3) virtualbox << 3を選択
4) vmware_desktop

Enter your choice: 3
==> box: Adding box 'bento/centos-7.4' (v201803.24.0) for provider: virtualbox
    box: Downloading: https://vagrantcloud.com/bento/boxes/centos-7.4/versions/201803.24.0/providers/virtualbox.box
==> box: Successfully added box 'bento/centos-7.4' (v201803.24.0) for 'virtualbox'!

vagrant box list

# 仮想マシン設定用のVagrantfileを作る
vagrant init bento/centos-7.4

# Vagrantfileを編集して仮想マシンIPアドレスを192.168.33.10にする
vi Vagrantfile
~~~~
【修正前】
  # config.vm.network "private_network", ip: "192.168.33.10"

【修正後】
  config.vm.network "private_network", ip: "192.168.33.10"
~~~~

# 仮想マシンを起動する
vagrant up

# 仮想マシンの状態を確認する
vagrant status

[5] 仮想マシンにログインする

# 以下のコマンドにより、仮想マシンにログインする
~~~~~~
vagrant ssh

exit
~~~~~~

# 仮想環境を終了する場合は、以下のコマンドを実行
vagrant halt
※ 補足:認証によるログイン
 * デフォルトユーザ・sudo は、以下の通り。
  + ユーザ名   : vagrant
  + パスワード : vagrant

■ トラブル

エラー内容

$ 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' is up to date...
==> default: Setting the name of the VM: hello-vagrant_default_1532265784574_41140
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
    default: Adapter 1: nat
    default: Adapter 2: hostonly
==> 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
Timed out while waiting for the machine to boot. This means that
Vagrant was unable to communicate with the guest machine within
the configured ("config.vm.boot_timeout" value) time period.

If you look above, you should be able to see the error(s) that
Vagrant had when attempting to connect to the machine. These errors
are usually good hints as to what may be wrong.

If you're using a custom box, make sure that networking is properly
working and you're able to connect to the machine. It is a common
problem that networking isn't setup properly in these boxes.
Verify that authentication configurations are also setup properly,
as well.

If the box appears to be booting properly, you may want to increase
the timeout ("config.vm.boot_timeout") value.

参考文献

https://qiita.com/rrryutaro/items/260f0f4bd0b1e550b6da

関連記事

Vagrant ~ 初期構築 / Windows 編 ~

https://blogs.yahoo.co.jp/dk521123/37431398.html