Vagrantのprivate_networkをCentOS7.0で設定

※ この問題はVagrantの1.6.5で解決しているようです。
[vagrant 1.6.4以下でcentos7を動かすとエラーが出る件対策#コメント](http://qiita.com/tukiyo3/items/5c56fa29110f996f381f#comment-8f83202f0cfb0d1fb0c1)

CentOS7.0ではVagrantfileで以下のようにprivate_networkを指定して起動するとエラーとなってしまう。

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

# Vagrantfile API/syntax version. Don't touch unless you know what you're doing!
VAGRANTFILE_API_VERSION = "2"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|

  config.vm.box = "hfm4/centos7"
  config.ssh.forward_agent = true

  config.vm.define :node1 do |node|
    node.vm.box = "hfm4/centos7"
    node.vm.network :forwarded_port, guest:22, host:2001, id:"ssh"
    node.vm.network :forwarded_port, guest:80, host:8081, id:"http"
    node.vm.network :private_network, ip:"192.168.33.11"
  end
end
===================================

$ vagrant up
===================================
The following SSH command responded with a non-zero exit status.
Vagrant assumes that this means the command failed!

ARPCHECK=no /sbin/ifup eth1 2> /dev/null

Stdout from the command:

ERROR    : [/etc/sysconfig/network-scripts/ifup-eth] Device eth1 does not seem to be present, delaying initialization.


Stderr from the command:
===================================
```

# デフォルトの設定で立ち上げてNIC名を確認
```
[ホスト側]
$ mkdir CentOS7.0
$ cd CentOS7.0
$ vagrant init hfm4/centos7
$ vagrant up
$ vagrant ssh

[VM内]
$ ifconfig
===================================
-bash: ifconfig: コマンドが見つかりません
===================================

$ ip a
===================================
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 08:00:27:be:2c:f4 brd ff:ff:ff:ff:ff:ff
    inet 10.0.2.15/24 brd 10.0.2.255 scope global dynamic enp0s3
       valid_lft 85414sec preferred_lft 85414sec
    inet6 fe80::a00:27ff:febe:2cf4/64 scope link
       valid_lft forever preferred_lft forever
===================================
```
10.0.2.15はCentOS6系までは「eth0」が割り当てられていたが、
CentOS7系では「enp0s3」になっている。

# auto_config:false で立ち上げてみる
```
[VM内]
$ exit

[ホスト側]
$ vi Vagrantfile
===================================
# -*- mode: ruby -*-
# vi: set ft=ruby :

# Vagrantfile API/syntax version. Don't touch unless you know what you're doing!
VAGRANTFILE_API_VERSION = "2"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|

  config.vm.box = "hfm4/centos7"
  config.ssh.forward_agent = true

  config.vm.define :node1 do |node|
    node.vm.box = "hfm4/centos7"
    node.vm.network :forwarded_port, guest:22, host:2001, id:"ssh"
    node.vm.network :forwarded_port, guest:80, host:8081, id:"http"
    node.vm.network :private_network, ip:"192.168.33.11", auto_config:false
  end
end
===================================
$ vagrant reload
$ vagrant ssh

[VM内]
$ ip a
===================================
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 08:00:27:be:2c:f4 brd ff:ff:ff:ff:ff:ff
    inet 10.0.2.15/24 brd 10.0.2.255 scope global dynamic enp0s3
       valid_lft 86384sec preferred_lft 86384sec
    inet6 fe80::a00:27ff:febe:2cf4/64 scope link
       valid_lft forever preferred_lft forever
3: enp0s8: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 08:00:27:c7:09:b0 brd ff:ff:ff:ff:ff:ff
    inet6 fe80::a00:27ff:fec7:9b0/64 scope link
       valid_lft forever preferred_lft forever
===================================
```

「enp0s8」が認識されている。

# NICにIPアドレス付与
nmcliコマンドでやってみる。

```
$ nmcli d show
===================================
GENERAL.デバイス:                       enp0s3
GENERAL.タイプ:                         ethernet
GENERAL.HWADDR:                         08:00:27:BE:2C:F4
GENERAL.MTU:                            1500
GENERAL.状態:                           100 (接続済み)
GENERAL.接続:                           enp0s3
GENERAL.CON パス:                       /org/freedesktop/NetworkManager/ActiveConnection/1
WIRED-PROPERTIES.キャリア:              オン
IP4.アドレス[1]:                        ip = 10.0.2.15/24, gw = 10.0.2.2
IP4.DNS[1]:                             10.0.2.3
IP4.ドメイン[1]:                        wi2.ne.jp
IP6.アドレス[1]:                        ip = fe80::a00:27ff:febe:2cf4/64, gw = ::

GENERAL.デバイス:                       enp0s8
GENERAL.タイプ:                         ethernet
GENERAL.HWADDR:                         08:00:27:3A:2A:FA
GENERAL.MTU:                            1500
GENERAL.状態:                           70 (接続中（IP 設定を取得中）)
GENERAL.接続:                           Wired connection 1
GENERAL.CON パス:                       /org/freedesktop/NetworkManager/ActiveConnection/0
WIRED-PROPERTIES.キャリア:              オン

GENERAL.デバイス:                       lo
GENERAL.タイプ:                         loopback
GENERAL.HWADDR:                         00:00:00:00:00:00
GENERAL.MTU:                            65536
GENERAL.状態:                           10 (管理無し)
GENERAL.接続:                           --
GENERAL.CON パス:                       --
IP4.アドレス[1]:                        ip = 127.0.0.1/8, gw = 0.0.0.0
IP6.アドレス[1]:                        ip = ::1/128, gw = ::
===================================

$ nmcli con show enp0s8
===================================
Error: enp0s8 - no such connection profile.
===================================
```
あれ。

```
$ nmcli con show
===================================
名前                UUID                                  タイプ          デバイス
Wired connection 1  02a8b317-5a4d-46ca-ac94-04238c762461  802-3-ethernet  enp0s8
enp0s3              3c36b8c2-334b-57c7-91b6-4401f3489c69  802-3-ethernet  enp0s3
===================================
```
ということは

```
$ nmcli con show "Wired connection 1"
===================================
connection.id:                          Wired connection 1
connection.uuid:                        02a8b317-5a4d-46ca-ac94-04238c762461
connection.interface-name:              --
connection.type:                        802-3-ethernet
connection.autoconnect:                 yes
connection.timestamp:                   1407581555
connection.read-only:                   no
connection.permissions:
connection.zone:                        --
connection.master:                      --
connection.slave-type:                  --
connection.secondaries:
connection.gateway-ping-timeout:        0
802-3-ethernet.port:                    --
802-3-ethernet.speed:                   0
802-3-ethernet.duplex:                  --
802-3-ethernet.auto-negotiate:          yes
802-3-ethernet.mac-address:             08:00:27:3A:2A:FA
802-3-ethernet.cloned-mac-address:      --
802-3-ethernet.mac-address-blacklist:
802-3-ethernet.mtu:                     自動
802-3-ethernet.s390-subchannels:
802-3-ethernet.s390-nettype:            --
802-3-ethernet.s390-options:
ipv4.method:                            auto
ipv4.dns:
ipv4.dns-search:
ipv4.addresses:                         { ip = 192.168.33.11/24, gw = 0.0.0.0 }
ipv4.routes:
ipv4.ignore-auto-routes:                no
ipv4.ignore-auto-dns:                   no
ipv4.dhcp-client-id:                    --
ipv4.dhcp-send-hostname:                yes
ipv4.dhcp-hostname:                     --
ipv4.never-default:                     no
ipv4.may-fail:                          yes
ipv6.method:                            auto
ipv6.dns:
ipv6.dns-search:
ipv6.addresses:
ipv6.routes:
ipv6.ignore-auto-routes:                no
ipv6.ignore-auto-dns:                   no
ipv6.never-default:                     no
ipv6.may-fail:                          yes
ipv6.ip6-privacy:                       -1 (不明)
ipv6.dhcp-hostname:                     --
GENERAL.名前:                           Wired connection 1
GENERAL.UUID:                           02a8b317-5a4d-46ca-ac94-04238c762461
GENERAL.デバイス:                       enp0s8
GENERAL.状態:                           アクティベート中
GENERAL.デフォルト:                     いいえ
GENERAL.デフォルト6:                    いいえ
GENERAL.VPN:                            いいえ
GENERAL.ゾーン:                         --
GENERAL.DBUS パス:                      /org/freedesktop/NetworkManager/ActiveConnection/0
GENERAL.CON パス:                       /org/freedesktop/NetworkManager/Settings/1
GENERAL.スペックオブジェクト:           --
GENERAL.マスターパス:                   --
===================================
```
やっぱり。

以下のように設定してみる。

| 変数 | 値 | 備考 |
|:-----|:---------|:---|
| connection.id: | enp0s8 | nmcliコマンドで指定するデバイス名 |
| connection.interface-name: | enp0s8 | インタフェース名 |
| ipv4.addresses: | 192.168.33.11/24 | IPアドレスとレンジ |
| ipv4.method: | manual | DHCPを利用しない |
| connection.autoconnect: | yes | OS立ち上げ時自動起動 |

```
$ sudo nmcli con mod "Wired connection 1" connection.id "enp0s8"
$ nmcli con show
===================================
名前    UUID                                  タイプ          デバイス
enp0s8  02a8b317-5a4d-46ca-ac94-04238c762461  802-3-ethernet  enp0s8
enp0s3  3c36b8c2-334b-57c7-91b6-4401f3489c69  802-3-ethernet  enp0s3
===================================
```
お。

```
$ sudo nmcli con mod enp0s8 connection.interface-name "enp0s8"
$ sudo nmcli con mod enp0s8 ipv4.addresses "192.168.33.11/24"
$ sudo nmcli con mod enp0s8 connection.autoconnect "yes"
$ sudo nmcli con mod enp0s8 ipv4.method "manual"
```

# インタフェース再起動
```
$ sudo nmcli device disconnect enp0s8
$ sudo nmcli device connect enp0s8
===================================
Device 'enp0s8' successfully activated with '02a8b317-5a4d-46ca-ac94-04238c762461'.
===================================
```
きたこれ

```
$ ifconfig
===================================
-bash: ifconfig: コマンドが見つかりません
===================================

$ ip a
===================================
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 08:00:27:be:2c:f4 brd ff:ff:ff:ff:ff:ff
    inet 10.0.2.15/24 brd 10.0.2.255 scope global dynamic enp0s3
       valid_lft 83635sec preferred_lft 83635sec
    inet6 fe80::a00:27ff:febe:2cf4/64 scope link
       valid_lft forever preferred_lft forever
3: enp0s8: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 08:00:27:3a:2a:fa brd ff:ff:ff:ff:ff:ff
    inet 192.168.33.11/24 brd 192.168.33.255 scope global enp0s8
       valid_lft forever preferred_lft forever
    inet6 fe80::a00:27ff:fe3a:2afa/64 scope link
       valid_lft forever preferred_lft forever
===================================
```
これでホスト側から `ssh 192.168.33.11 -l vagrant` で無事接続できました！


# 備考

## NICの命名規則について
[こちら](http://d.hatena.ne.jp/enakai00/20140728/1406504163)でNICの命名規則についての記述がありました。

| 文字 | 意味 |
|:-----:|:---------:|
| en | イーサネット |
| p | Firmware/BIOSがNICの（固定的な）論理番号を提供していない場合|
| 0 | Bus0|
| s8 |Slot8|

## おまじない

``` shell
  config.vm.define :node1 do |node|
    node.vm.box = "hfm4/centos7"
    node.vm.network "private_network", ip: "192.168.33.11", auto_config:false
    node.vm.network "forwarded_port", guest: 80, host: 8080, id:"http"
    node.vm.network "forwarded_port", guest: 22, host: 2001, id:"ssh"
    node.vm.provision "shell", inline: <<-SHELL
      sudo nmcli con delete `nmcli con  | grep enp0s8 | tail -1 | awk '{print $2}'`
      sudo nmcli con mod "`nmcli con  | grep enp0s8 | head -1 | awk '{print $3}'`" connection.id "enp0s8"
      sudo nmcli con mod enp0s8 ipv4.addresses "192.168.33.11/24"
      sudo nmcli con mod enp0s8 ipv4.method "manual"
      sudo nmcli con down enp0s8
      sudo nmcli con up enp0s8
    SHELL
  end
```

# リンク
* [RHEL7のNICのネーミングルール](http://d.hatena.ne.jp/enakai00/20140728/1406504163)
* [Vagrant の VM が使う NIC を自動設定しないようにする](http://momijiame.tumblr.com/post/76852553568/vagrant-vm-nic)
* nmcli関連
    * [NetworkManagerの設定変更、nmtuiとnmcliについてまとめたよ！](http://blog.yuryu.jp/2014/07/networkmanager-nmtui-nmcli.html)
    * [[Linux]RHEL7 RCでネットワーク周りで躓いたのでメモ](http://d.hatena.ne.jp/ryousanngata/20140518/1400365072)
    * [CentOS7が出たので、最小インストールからSSHが接続できるまで](http://qiita.com/tamamius/items/a27fa04a4c978479b395)