当サイトは、アフィリエイト広告を利用しています

Vagrantのネットワーク設定~private_networkとpublic_network~

作成日:2023月09月11日
更新日:2023年12月04日

Vagrantで作成した仮想マシン(vagrantVM)にホスト以外の同じLAN内のPCから
接続させる方法を調べていて、Vagrant のネットワーク設定に色々わかったことを
まとめておく

Vagrantのネットワーク設定

Vagrantfileのネットワーク設定は
Vagrantfile内の「config.vm.network~」の部分で行う。

config.vm.networkには

  • private_network
  • public_network

があり、 private_networkを設定した場合は「内部ネットワーク」となり
public_networkを設定した場合は「ブリッジアダプター」が設定される。

ちなみにデフォルトでは二つともコメントアウトされており、
基本的にホストからしか接続できない。

デフォルトで作成した場合、ホストから

  • vagrant sshコマンドで通信
  • 通常のsshで通信

の2つのパターンがある。

詳しくは下記記事で紹介してまる

実際にvagrantVMを作って動作を確認してみる

構成

下記のような構成で動作を確認する

  • windows10からlinux(ubuntu)にssh接続する
  • linux(ubuntu)でvagrantを使ってvirtualboxでvagrantVMを二つ作る

上記の構成のため

  • ホスト = linux(ubuntu)
  • 外部マシン = Windows10

となる

環境

  • linux(ubuntu) : Ubuntu 22.04.3 LTS

ちなみに先に結果だけ書いておくと下記のようになる

private_networkを設定した時

  • ホストからvagrantVMにssh → 〇 
  • vagrantVM同士のssh(同じprivate_network内) → 〇
  • 外部マシンからvagrantVMにssh → ×

public_networkを設定した時

  • ホストからvagrantVMにssh → 〇 
  • vagrantVM同士のssh → 〇
  • 外部マシンからvagrantVMにssh → 〇

※ネットワーク上のマシンからもアクセスできるので
セキュリティには注意が必要

private_network(内部ネットワーク)を設定する

ホストとvagrantVMを入れた仮想の内部ネットワークが作成される。
別のvagrantVMを作成する時に同一の内部ネットワークを設定すれば
同じネットワーク内に属させることができるので
vagrantVMでの通信が可能になる。

private_networkのvagrantVMを作成する

ubuntuのvagrantVMをprivate_networkで作成する

boxの確認

ubuntuのboxがあることを確認

box確認
$ vagrant box list
ubuntu/trusty64 (virtualbox, 20190514.0.0)

vagrantVM1作成

適当にディレクトリを作る。

ディレクトリ
├── vagrant_machins
│   ├── vagrantVM1
│   │  
│   └── vagrantVM2

vagrantVM1に移動してVagrantfileを作る

init
$ vagrant init ubuntu/trusty64
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.
$ ls
Vagrantfile

Vagrantfileの編集

vagrantVM1のVagrantfileを編集してprivate_networkを
設定する

最初はコメントアウトされている

Vagrantfile
# config.vm.network "private_network"

Vagrantfile
config.vm.network "private_network", ip: "192.168.59.11"

有効化してipアドレスを設定する
※ipアドレスは192.168.56.1 ~ 192.168.63.254の間でないとエラーになる。

vagrantVM1を起動する

vagrantVM1を起動してネットワーク設定を確認してみる

起動

Vagrantfileがあるディレクトリに移動して起動する

vm起動
$ vagrant up

ログインする

起動できたらログインする

確認
$ 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`.

runningになっていれば起動できている。

vagrant sshでログインする
※初期パスワードは「vagrant」

ログイン
$ vagrant ssh
vagrant@127.0.0.1's password:
Welcome to Ubuntu 14.04.6 LTS (GNU/Linux 3.13.0-170-generic x86_64)
* Documentation: https://help.ubuntu.com/
System information as of Sat Sep 9 17:06:03 UTC 2023
System load: 0.29 Processes: 77
Usage of /: 3.6% of 39.34GB Users logged in: 0
Memory usage: 25% IP address for eth0: 10.0.2.15
Swap usage: 0% IP address for eth1: 192.168.59.11
Graph this data and manage this system at:
https://landscape.canonical.com/
UA Infrastructure Extended Security Maintenance (ESM) is not enabled.
0 updates can be installed immediately.
0 of these updates are security updates.
Enable UA Infrastructure ESM to receive 64 additional security updates.
See https://ubuntu.com/advantage or run: sudo ua status
New release '16.04.7 LTS' available.
Run 'do-release-upgrade' to upgrade to it.
Last login: Sat Sep 9 17:06:03 2023 from 10.0.2.2
vagrant@vagrant-ubuntu-trusty-64:~$

IPアドレスとルートの確認

確認
vagrant@vagrant-ubuntu-trusty-64:~$ hostname -I
10.0.2.15 192.168.59.11
vagrant@vagrant-ubuntu-trusty-64:~$ route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
default 10.0.2.2 0.0.0.0 UG 0 0 0 eth0
10.0.2.0 * 255.255.255.0 U 0 0 0 eth0
192.168.59.0 * 255.255.255.0 U 0 0 0 eth1

問題なく設定されている。

vagrantVM2作成

同様の手順でvagrantVM2を作成する
※手順で変わるのはVagrantfileのIPアドレスだけ

Vagrantfileの編集

vagrantVM2のVagrantfileを編集してprivate_networkを
設定する

Vagrantfile
config.vm.network "private_network", ip: "192.168.59.22"

vagrantVM1と同じネットワークに所属させるため
IPアドレスのネットワーク部は192.168.59にする

内部ネットワークになっているのか?

ちゃんと内部ネットワークになっているのか確認してみる
確認のため

  • ホストからvagrantVM1
  • vagrantVM1からvagrantVM2
  • 外部マシンからvagrantVM1

でsshで通信できるか確認する

イメージとしては下記のようになってるため 「外部マシンからvagrantVM1」以外は通信できる

ホストからvagrantVM1にssh

まずはホストでipを調べてみると

hostIP
$ ip addr
12: vboxnet1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
link/ether 0a:00:27:00:00:01 brd ff:ff:ff:ff:ff:ff
inet 192.168.59.1/24 brd 192.168.59.255 scope global vboxnet1
valid_lft forever preferred_lft forever
inet6 fe80::800:27ff:fe00:1/64 scope link
valid_lft forever preferred_lft forever

vboxnet1として追加されている

ssh接続する

ホストからvagrantVM2にsshする

ssh
# ホストからssh
$ ssh vagrant@192.168.59.22
The authenticity of host '192.168.59.22 (192.168.59.22)' can't be established.
ED25519 key fingerprint is SHA256:KrS/khf4+kXLudFmlOwzwov6s1GQWljYlfwHXVswRWs.
This key is not known by any other names
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added '192.168.59.22' (ED25519) to the list of known hosts.
vagrant@192.168.59.22's password:
Welcome to Ubuntu 14.04.6 LTS (GNU/Linux 3.13.0-170-generic x86_64)
* Documentation: https://help.ubuntu.com/
System information as of Sat Sep 9 18:01:36 UTC 2023
System load: 0.52 Processes: 85
Usage of /: 3.6% of 39.34GB Users logged in: 0
Memory usage: 25% IP address for eth0: 10.0.2.15
Swap usage: 0% IP address for eth1: 192.168.59.22
Graph this data and manage this system at:
https://landscape.canonical.com/
UA Infrastructure Extended Security Maintenance (ESM) is not enabled.
0 updates can be installed immediately.
0 of these updates are security updates.
Enable UA Infrastructure ESM to receive 64 additional security updates.
See https://ubuntu.com/advantage or run: sudo ua status
New release '16.04.7 LTS' available.
Run 'do-release-upgrade' to upgrade to it.
Last login: Sat Sep 9 17:53:33 2023 from 10.0.2.2

ログインできた!
vagrantVM1も同様にできる!

vagrantVM1からvagrantVM2

vagrantVM1にログインしてからvagrantVM2にsshする

ssh
# hostからvagrantVM1にssh
$ ssh vagrant@192.168.59.11
vagrant@192.168.59.11's password:
Welcome to Ubuntu 14.04.6 LTS (GNU/Linux 3.13.0-170-generic x86_64)
* Documentation: https://help.ubuntu.com/
System information as of Sat Sep 9 18:19:59 UTC 2023
System load: 0.35 Processes: 76
Usage of /: 3.6% of 39.34GB Users logged in: 0
Memory usage: 24% IP address for eth0: 10.0.2.15
Swap usage: 0% IP address for eth1: 192.168.59.11
Graph this data and manage this system at:
https://landscape.canonical.com/
UA Infrastructure Extended Security Maintenance (ESM) is not enabled.
0 updates can be installed immediately.
0 of these updates are security updates.
Enable UA Infrastructure ESM to receive 64 additional security updates.
See https://ubuntu.com/advantage or run: sudo ua status
New release '16.04.7 LTS' available.
Run 'do-release-upgrade' to upgrade to it.
Last login: Sat Sep 9 18:19:59 2023 from 192.168.59.1
# vagrantVM1からvagrantVM2にssh
$ ssh vagrant@192.168.59.22
vagrant@192.168.59.22's password:
Welcome to Ubuntu 14.04.6 LTS (GNU/Linux 3.13.0-170-generic x86_64)
* Documentation: https://help.ubuntu.com/
System information as of Sat Sep 9 18:20:46 UTC 2023
System load: 0.08 Processes: 74
Usage of /: 3.6% of 39.34GB Users logged in: 0
Memory usage: 24% IP address for eth0: 10.0.2.15
Swap usage: 0% IP address for eth1: 192.168.59.22
Graph this data and manage this system at:
https://landscape.canonical.com/
UA Infrastructure Extended Security Maintenance (ESM) is not enabled.
0 updates can be installed immediately.
0 of these updates are security updates.
Enable UA Infrastructure ESM to receive 64 additional security updates.
See https://ubuntu.com/advantage or run: sudo ua status
New release '16.04.7 LTS' available.
Run 'do-release-upgrade' to upgrade to it.
Last login: Sat Sep 9 18:20:47 2023 from 192.168.59.11
vagrant@vagrant-ubuntu-trusty-64:~$

vagrantVM同士もsshができる!

外部マシンからvagrantVM1

外部マシンからvagrantVMに関してはネットワークが違うため
通信できない

結果としては下記のようになる

  • ホストからvagrantVM1 → 〇
  • vagrantVM1からvagrantVM2 → 〇
  • 外部マシンからvagrantVM1 → ×

public_network(ブリッジアダプター)を設定する

ホストが所属するLAN内にvagrantVMのIPを割り当てる。
LAN上でvagrantVMは通常マシンと同様に見えるため
そのLAN内PCであれば通信可能になる
※ネットワーク上のマシンからもアクセスできるので、セキュリティには注意が必要

イメージとしては下記のような感じになる

vagrantVM3作成

vagrantVM3のフォルダを作る。

フォルダ構成
├── vagrant_machins
│   ├── vagrantVM1
│   │  
│   ├── vagrantVM2
│   │  
│   └── vagrantVM3

vagrantVM3に移動してVagrantfileを作る

init
$ vagrant init ubuntu/trusty64
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.
$ ls
Vagrantfile

bridgeするインターフェースを調べる

vagrantVMが稼働している端末のbridgeするインターフェースを調べる

インターフェース
$ VBoxManage list bridgedifs |grep '^Name:'
Name: wlp2s0
~

インターフェースを調べて後述するVagrantfileに
記述する。
※記述しないとどのインターフェースを使うか、
起動時に聞かれるのでめんどくさい。。。

Vagrantfileの編集

vagrantVM1のVagrantfileを編集してpublic_networkを
設定する

最初はコメントアウトされている

Vagrantfile
# config.vm.network "public_network"

Vagrantfile
config.vm.network "public_network", ip: "192.168.0.33",bridge: "wlp2s0"

有効化してipアドレスを設定する
IPアドレスはホストが属してるネットワークにする

route
$ route
カーネルIP経路テーブル
受信先サイト ゲートウェイ ネットマスク フラグ Metric Ref 使用数 インタフェース
default _gateway 0.0.0.0 UG 600 0 0 wlp2s0
192.168.0.0 0.0.0.0 255.255.255.0 U 600 0 0 wlp2s0
192.168.59.0 0.0.0.0 255.255.255.0 U 0 0 0 vboxnet1

vagrantVM3を起動する

vagrantVM3を起動してネットワーク設定を確認してみる

起動

Vagrantfileがあるディレクトリに移動して起動する

up
$ vagrant up

IPアドレスとルートの確認

起動できたらログインしてIPアドレスとルートを確認する

確認
# 起動確認
$ 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`.
# ログイン
ash92@as-ubuntu:~/vagrant_machins/vagrantVM3$ vagrant ssh
vagrant@127.0.0.1's password:
Welcome to Ubuntu 14.04.6 LTS (GNU/Linux 3.13.0-170-generic x86_64)
* Documentation: https://help.ubuntu.com/
System information as of Mon Sep 11 09:30:31 UTC 2023
System load: 0.79 Processes: 84
Usage of /: 3.6% of 39.34GB Users logged in: 0
Memory usage: 26% IP address for eth0: 10.0.2.15
Swap usage: 0%
Graph this data and manage this system at:
https://landscape.canonical.com/
UA Infrastructure Extended Security Maintenance (ESM) is not enabled.
0 updates can be installed immediately.
0 of these updates are security updates.
Enable UA Infrastructure ESM to receive 64 additional security updates.
See https://ubuntu.com/advantage or run: sudo ua status
New release '16.04.7 LTS' available.
Run 'do-release-upgrade' to upgrade to it.
# IPアドレス確認
vagrant@vagrant-ubuntu-trusty-64:~$ hostname -I
10.0.2.15 192.168.0.33 2001:ce8:161:4fb4:a00:27ff:fe6e:cc6e
# ルート確認
vagrant@vagrant-ubuntu-trusty-64:~$ route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
default 10.0.2.2 0.0.0.0 UG 0 0 0 eth0
10.0.2.0 * 255.255.255.0 U 0 0 0 eth0
192.168.0.0 * 255.255.255.0 U 0 0 0 eth1
vagrant@vagrant-ubuntu-trusty-64:~$

ホストのネットワークに追加されていることを確認できた

パブリックネットワークになっているのか?

ちゃんとパブリックネットワークになっているのか確認してみる
確認のため

  • ホストからvagrantVM3
  • vagrantVM3からvagrantVM1
  • 外部マシンからvagrantVM3

ホストからvagrantVM3に通信

同じネットワーク(192.168.0.0)のため通信できる

ホストからvagrantVM3
$ ssh vagrant@192.168.0.33
The authenticity of host '192.168.0.33 (192.168.0.33)' can't be established.
ED25519 key fingerprint is SHA256:MYkmlVkzUa9tT3v8V1W1pU6pEtA1HeLYSVk3KoYG+A4.
This key is not known by any other names
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added '192.168.0.33' (ED25519) to the list of known hosts.
vagrant@192.168.0.33's password:
Welcome to Ubuntu 14.04.6 LTS (GNU/Linux 3.13.0-170-generic x86_64)
* Documentation: https://help.ubuntu.com/
System information as of Mon Sep 11 09:31:15 UTC 2023
System load: 0.42 Processes: 78
Usage of /: 3.6% of 39.34GB Users logged in: 0
Memory usage: 25% IP address for eth0: 10.0.2.15
Swap usage: 0% IP address for eth1: 192.168.0.33
Graph this data and manage this system at:
https://landscape.canonical.com/
UA Infrastructure Extended Security Maintenance (ESM) is not enabled.
0 updates can be installed immediately.
0 of these updates are security updates.
Enable UA Infrastructure ESM to receive 64 additional security updates.
See https://ubuntu.com/advantage or run: sudo ua status
New release '16.04.7 LTS' available.
Run 'do-release-upgrade' to upgrade to it.
Last login: Mon Sep 11 09:31:15 2023 from 10.0.2.2
vagrant@vagrant-ubuntu-trusty-64:~$

問題なくsshできる

vagrantVM3からvagrantVM1に通信

ネットワーク上でvagrantVM3は独自のIPアドレスを持つ通常のマシン
のようになっているので、通信可能。

vagrantVM1が稼働していることを確認

VM稼働確認
ash92@as-ubuntu:~/vagrant_machins/vagrantVM3$ vagrant global-status -a
/home/ash92/vagrant_machins/vagrantVM1
default running (virtualbox) 2023-09-10 03:18:50 +0900
/home/ash92/vagrant_machins/vagrantVM2
default running (virtualbox) 2023-09-10 03:01:39 +0900
/home/ash92/vagrant_machins/vagrantVM3
default running (virtualbox) 2023-09-11 18:30:32 +0900

vagrantの仮想マシンを一覧で確認する方法については下記参照

vagrantVM3にログイン後、vagrantVM1にsshしてみる

VM3→VM1
# vatrantVM3にログイン
$ vagrant ssh
vagrant@127.0.0.1's password:
Welcome to Ubuntu 14.04.6 LTS (GNU/Linux 3.13.0-170-generic x86_64)
* Documentation: https://help.ubuntu.com/
System information as of Mon Sep 11 09:49:20 UTC 2023
System load: 0.0 Processes: 77
Usage of /: 3.6% of 39.34GB Users logged in: 1
Memory usage: 26% IP address for eth0: 10.0.2.15
Swap usage: 0% IP address for eth1: 192.168.0.33
Graph this data and manage this system at:
https://landscape.canonical.com/
UA Infrastructure Extended Security Maintenance (ESM) is not enabled.
0 updates can be installed immediately.
0 of these updates are security updates.
Enable UA Infrastructure ESM to receive 64 additional security updates.
See https://ubuntu.com/advantage or run: sudo ua status
New release '16.04.7 LTS' available.
Run 'do-release-upgrade' to upgrade to it.
Last login: Mon Sep 11 09:49:20 2023 from 192.168.0.10
# vagrantVM3からvagrantVM1にログイン
$ ssh vagrant@192.168.59.11
vagrant@192.168.59.11's password:
Welcome to Ubuntu 14.04.6 LTS (GNU/Linux 3.13.0-170-generic x86_64)
* Documentation: https://help.ubuntu.com/
System information as of Mon Sep 11 09:47:51 UTC 2023
System load: 0.0 Processes: 74
Usage of /: 3.9% of 39.34GB Users logged in: 0
Memory usage: 27% IP address for eth0: 10.0.2.15
Swap usage: 0% IP address for eth1: 192.168.59.11
Graph this data and manage this system at:
https://landscape.canonical.com/
UA Infrastructure Extended Security Maintenance (ESM) is not enabled.
1 update can be installed immediately.
0 of these updates are security updates.
To see these additional updates run: apt list --upgradable
Enable UA Infrastructure ESM to receive 193 additional security updates.
See https://ubuntu.com/advantage or run: sudo ua status
New release '16.04.7 LTS' available.
Run 'do-release-upgrade' to upgrade to it.
Last login: Mon Sep 11 09:47:51 2023 from 10.0.2.2
vagrant@vagrant-ubuntu-trusty-64:~$

仮想マシン同士もログインできる

外部マシンからvagrantVM3

Windowsからもログインできる

結果としては下記のようになる

  • ホストからvagrantVM3 → 〇
  • vagrantVM3からvagrantVM1 → 〇
  • 外部マシンからvagrantVM3 → 〇

ブリッジ設定した場合は基本的に全て通信可能になる
※その分、注意が必要

まとめ

vagrantのネットワーク設定について

  • private_network
  • public_network

の設定方法とその動作について実際に動かして
まとめてみた。

個人的には基本的には内部ネットワークでいいかなと思った。
※あまり外部からアクセスさせる必要もないので

参考

新着記事

top