Vargrant で CentOS7.3 の環境を構築 ～ default: Warning: Authentication failure. Retrying... の対処と固定IPアドレスの有効化 ～

# はじめに

Vagrant で CentOS7 の環境を構築しようとしたところ、`vagrant up`のときに、
`default: Warning: Authentication failure. Retrying...`
が表示された。
また、固定IPアドレスの設定が有効にならなかった。
この対処方法を含め、環境構築方法をまとめる。

以下の記事を参考にしました。
[vagrant upで失敗する原因が鍵不一致の場合の対処法 - Qiita](http://qiita.com/d2cdot-mmori/items/1c340f175ae510e4456a)
[CentOS7.2 64bit ネットワークの設定 | kakiro-web カキローウェブ](http://www.kakiro-web.com/memo/centos-network-setting.html)

追記
Vagrant をアンインストール＆インストールすることで、
`default: Warning: Remote connection disconnect. Retrying...`
が続くことはなくなりました。
Vagrant を再インストールしたことで、仮想マシンの初回起動時に、仮想マシンごとに秘密鍵と公開鍵を生成するようになったためです。
詳しくはコメント欄をご覧ください。



# 実行環境

* Windows 10 Home
* Cygwin 2.5.0
* VirtualBox 5.1.10
* Vagrant 1.9.1

# Vagrantfile の設定

MyCentOS フォルダを作成し、Vagrantfile を作成する。

```shell-session
$ mkdir MyCentOS
$ cd MyCentOS
$ vagrant init bento/centos-7.3
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.

$
```

[Vagrant box bento/centos-7.3 | Atlas by HashiCorp](https://atlas.hashicorp.com/bento/boxes/centos-7.3)

# Vagrantfile の編集

固定 IP アドレスを設定するために、Vagrantfile の

```text:Vagrantfile
# config.vm.network "private_network", ip: "192.168.33.10"
```

を有効にする。

```text:Vagrantfile
 config.vm.network "private_network", ip: "192.168.33.10"
```

Vagrantfile 全体では以下のようになる。

```ruby:Vagrantfile
# -*- mode: ruby -*-
# vi: set ft=ruby :

# All Vagrant configuration is done below. The "2" in Vagrant.configure
# configures the configuration version (we support older styles for
# backwards compatibility). Please don't change it unless you know what
# you're doing.
Vagrant.configure("2") do |config|
  # The most common configuration options are documented and commented below.
  # For a complete reference, please see the online documentation at
  # https://docs.vagrantup.com.

  # Every Vagrant development environment requires a box. You can search for
  # boxes at https://atlas.hashicorp.com/search.
  config.vm.box = "bento/centos-7.3"

  # Disable automatic box update checking. If you disable this, then
  # boxes will only be checked for updates when the user runs
  # `vagrant box outdated`. This is not recommended.
  # config.vm.box_check_update = false

  # Create a forwarded port mapping which allows access to a specific port
  # within the machine from a port on the host machine. In the example below,
  # accessing "localhost:8080" will access port 80 on the guest machine.
  # config.vm.network "forwarded_port", guest: 80, host: 8080

  # Create a private network, which allows host-only access to the machine
  # using a specific IP.
  config.vm.network "private_network", ip: "192.168.33.10"

  # Create a public network, which generally matched to bridged network.
  # Bridged networks make the machine appear as another physical device on
  # your network.
  # config.vm.network "public_network"

  # Share an additional folder to the guest VM. The first argument is
  # the path on the host to the actual folder. The second argument is
  # the path on the guest to mount the folder. And the optional third
  # argument is a set of non-required options.
  # config.vm.synced_folder "../data", "/vagrant_data"

  # Provider-specific configuration so you can fine-tune various
  # backing providers for Vagrant. These expose provider-specific options.
  # Example for VirtualBox:
  #
  # config.vm.provider "virtualbox" do |vb|
  #   # Display the VirtualBox GUI when booting the machine
  #   vb.gui = true
  #
  #   # Customize the amount of memory on the VM:
  #   vb.memory = "1024"
  # end
  #
  # View the documentation for the provider you are using for more
  # information on available options.

  # Define a Vagrant Push strategy for pushing to Atlas. Other push strategies
  # such as FTP and Heroku are also available. See the documentation at
  # https://docs.vagrantup.com/v2/push/atlas.html for more information.
  # config.push.define "atlas" do |push|
  #   push.app = "YOUR_ATLAS_USERNAME/YOUR_APPLICATION_NAME"
  # end

  # Enable provisioning with a shell script. Additional provisioners such as
  # Puppet, Chef, Ansible, Salt, and Docker are also available. Please see the
  # documentation for more information about their specific syntax and use.
  # config.vm.provision "shell", inline: <<-SHELL
  #   apt-get update
  #   apt-get install -y apache2
  # SHELL
end
```

# 仮想マシンの起動

## 初回

```shell-session
$ vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Box 'bento/centos-7.3' could not be found. Attempting to find and install...
    default: Box Provider: virtualbox
    default: Box Version: >= 0
==> default: Loading metadata for box 'bento/centos-7.3'
    default: URL: https://atlas.hashicorp.com/bento/centos-7.3
==> default: Adding box 'bento/centos-7.3' (v2.3.2) for provider: virtualbox
    default: Downloading: https://atlas.hashicorp.com/bento/boxes/centos-7.3/versions/2.3.2/providers/virtualbox.box
    default:
==> default: Successfully added box 'bento/centos-7.3' (v2.3.2) for 'virtualbox'!
==> default: Importing base box 'bento/centos-7.3'...
==> default: Matching MAC address for NAT networking...
==> default: Checking if box 'bento/centos-7.3' is up to date...
==> default: Setting the name of the VM: MyCentOS_default_1482296484528_8004
==> 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
    default: Warning: Remote connection disconnect. Retrying...
    default: Warning: Remote connection disconnect. Retrying...
    default: Warning: Remote connection disconnect. Retrying...
    default: Warning: Authentication failure. Retrying...
    default: Warning: Authentication failure. Retrying...
    default: Warning: Authentication failure. Retrying...
    default: Warning: Authentication failure. Retrying...
    default: Warning: Authentication failure. Retrying...
    default: Warning: Authentication failure. Retrying...
    default: Warning: Authentication failure. Retrying...
    default: Warning: Authentication failure. Retrying...
    default: Warning: Authentication failure. Retrying...
    default: Warning: Authentication failure. Retrying...
    default: Warning: Authentication failure. Retrying...
    default: Warning: Authentication failure. Retrying...
    default: Warning: Authentication failure. Retrying...
    default: Warning: Authentication failure. Retrying...
    default: Warning: Authentication failure. Retrying...
    default: Warning: Authentication failure. Retrying...
    default: Warning: Authentication failure. Retrying...
    default: Warning: Authentication failure. Retrying...
    default: Warning: Authentication failure. Retrying...
    default: Warning: Authentication failure. Retrying...
    default: Warning: Authentication failure. Retrying...
    default: Warning: Authentication failure. Retrying...
    default: Warning: Authentication failure. Retrying...
    default: Warning: Authentication failure. Retrying...
    default: Warning: Authentication failure. Retrying...
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.

$
```

この状態で、仮想マシンは起動している。

```shell-session
$ 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`.
```

追記
初回起動時に`default: Warning: Authentication failure. Retrying...`が続いていなければ、「Authentication failure. の対処」の項はスキップしてください。

# Authentication failure. の対処

## 秘密鍵の場所の確認

以下のコマンドで、秘密鍵の場所を確認する。

```shell-session
$ vagrant ssh-config
Host default
  HostName 127.0.0.1
  User vagrant
  Port 2222
  UserKnownHostsFile /dev/null
  StrictHostKeyChecking no
  PasswordAuthentication no
  IdentityFile C:/Users/XXXXX/.vagrant.d/insecure_private_key
  IdentitiesOnly yes
  LogLevel FATAL
```

上記の  IdentityFile が秘密鍵である。

## 鍵の生成

以下のコマンドで、秘密鍵に対応する公開鍵を生成する。

```shell-session
$ ssh-keygen -yf C:/Users/XXXXX/.vagrant.d/insecure_private_key > public_key

```

これで、public_key という公開鍵が生成される。

## 仮想マシンの公開鍵の書き換え

仮想マシンに接続する。
パスワードは、"vagrant"

```shell-session
$ vagrant ssh
vagrant@127.0.0.1's password:
[vagrant@localhost ~]$
```


仮想マシンに設定されている公開鍵は、`~/.ssh/authorized_keys`なので、先ほどの`public_key`の内容をこのファイルに上書きする。

```shell-session
[vagrant@localhost ~]$ vi ~/.ssh/authorized_keys
```

仮想マシンをログアウトする。

```shell-session
[vagrant@localhost ~]$ exit
logout
Connection to 127.0.0.1 closed.

$
```

# 仮想マシンの再起動

```shell-session
$ vagrant reload
==> default: Attempting graceful shutdown of VM...
==> default: Checking if box 'bento/centos-7.3' is up to date...
==> default: Clearing any previously set forwarded ports...
==> 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
    default: Warning: Remote connection disconnect. Retrying...
==> default: Machine booted and ready!
[default] GuestAdditions 5.1.10 running --- OK.
==> default: Checking for guest additions in VM...
==> default: Configuring and enabling network interfaces...
==> default: Mounting shared folders...
    default: /vagrant => D:/vagrant/MyCentOS

$
```

## 仮想マシンに接続

```shell-session
$ vagrant ssh
Last login: Wed Dec 21 05:14:42 2016 from 10.0.2.2
[vagrant@localhost ~]$

```

パスワードを入力しなくても接続できるようになる。


# 固定 IP アドレスの有効化

この時点では、Vagrantfile で設定した`192.168.33.10`は有効になっていない。
（仮想マシン上で`ip addr`や`ifconfig`コマンドで確認できる）

## 仮想マシンの停止

```shell-session
$ vagrant halt
==> default: Attempting graceful shutdown of VM...

$
```

## Vagrantfile の編集

Vagrantfile で、

```text:Vagrantfile
  # config.vm.provision "shell", inline: <<-SHELL
  #   apt-get update
  #   apt-get install -y apache2
  # SHELL
```

を

```text:Vagrantfile
  config.vm.provision "shell", inline: <<-SHELL
    sudo systemctl restart network.service 
  SHELL
```

に書き換える。

Vagrantfile 全体では以下のようになる。

```ruby:Vagrantfile
# -*- mode: ruby -*-
# vi: set ft=ruby :

# All Vagrant configuration is done below. The "2" in Vagrant.configure
# configures the configuration version (we support older styles for
# backwards compatibility). Please don't change it unless you know what
# you're doing.
Vagrant.configure("2") do |config|
  # The most common configuration options are documented and commented below.
  # For a complete reference, please see the online documentation at
  # https://docs.vagrantup.com.

  # Every Vagrant development environment requires a box. You can search for
  # boxes at https://atlas.hashicorp.com/search.
  config.vm.box = "bento/centos-7.3"

  # Disable automatic box update checking. If you disable this, then
  # boxes will only be checked for updates when the user runs
  # `vagrant box outdated`. This is not recommended.
  # config.vm.box_check_update = false

  # Create a forwarded port mapping which allows access to a specific port
  # within the machine from a port on the host machine. In the example below,
  # accessing "localhost:8080" will access port 80 on the guest machine.
  # config.vm.network "forwarded_port", guest: 80, host: 8080

  # Create a private network, which allows host-only access to the machine
  # using a specific IP.
  config.vm.network "private_network", ip: "192.168.33.10"

  # Create a public network, which generally matched to bridged network.
  # Bridged networks make the machine appear as another physical device on
  # your network.
  # config.vm.network "public_network"

  # Share an additional folder to the guest VM. The first argument is
  # the path on the host to the actual folder. The second argument is
  # the path on the guest to mount the folder. And the optional third
  # argument is a set of non-required options.
  # config.vm.synced_folder "../data", "/vagrant_data"

  # Provider-specific configuration so you can fine-tune various
  # backing providers for Vagrant. These expose provider-specific options.
  # Example for VirtualBox:
  #
  # config.vm.provider "virtualbox" do |vb|
  #   # Display the VirtualBox GUI when booting the machine
  #   vb.gui = true
  #
  #   # Customize the amount of memory on the VM:
  #   vb.memory = "1024"
  # end
  #
  # View the documentation for the provider you are using for more
  # information on available options.

  # Define a Vagrant Push strategy for pushing to Atlas. Other push strategies
  # such as FTP and Heroku are also available. See the documentation at
  # https://docs.vagrantup.com/v2/push/atlas.html for more information.
  # config.push.define "atlas" do |push|
  #   push.app = "YOUR_ATLAS_USERNAME/YOUR_APPLICATION_NAME"
  # end

  # Enable provisioning with a shell script. Additional provisioners such as
  # Puppet, Chef, Ansible, Salt, and Docker are also available. Please see the
  # documentation for more information about their specific syntax and use.
  config.vm.provision "shell", inline: <<-SHELL
    sudo systemctl restart network.service
  SHELL
end
```

# 仮想マシンの起動

仮想マシンを起動する。
このとき、`--provision`オプションをつける。

```shell-session
$ vagrant up --provision
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Checking if box 'bento/centos-7.3' is up to date...
==> default: Clearing any previously set forwarded ports...
==> 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
    default: Warning: Remote connection disconnect. Retrying...
==> default: Machine booted and ready!
[default] GuestAdditions 5.1.10 running --- OK.
==> default: Checking for guest additions in VM...
==> default: Configuring and enabling network interfaces...
==> default: Mounting shared folders...
    default: /vagrant => D:/vagrant/MyCentOS
==> default: Running provisioner: shell...
    default: Running: inline script

$
```

これで、固定 IP アドレスが有効になっている。

今後、仮想マシンを起動するときには、`--provision`オプションを指定する。