当サイトは、アフィリエイト広告を利用しています
Vagrantで作成した仮想マシン(vagrantVM)にホスト以外の同じLAN内のPCから
接続させる方法を調べていて、Vagrant のネットワーク設定に色々わかったことを
まとめておく
Vagrantfileのネットワーク設定は
Vagrantfile内の「config.vm.network~」の部分で行う。
config.vm.networkには
があり、
private_networkを設定した場合は「内部ネットワーク」となり
public_networkを設定した場合は「ブリッジアダプター」が設定される。
ちなみにデフォルトでは二つともコメントアウトされており、
基本的にホストからしか接続できない。
デフォルトで作成した場合、ホストから
の2つのパターンがある。
詳しくは下記記事で紹介してまる
実際にvagrantVMを作って動作を確認してみる
下記のような構成で動作を確認する
上記の構成のため
となる
ちなみに先に結果だけ書いておくと下記のようになる
※ネットワーク上のマシンからもアクセスできるので
セキュリティには注意が必要
ホストとvagrantVMを入れた仮想の内部ネットワークが作成される。
別のvagrantVMを作成する時に同一の内部ネットワークを設定すれば
同じネットワーク内に属させることができるので
vagrantVMでの通信が可能になる。
ubuntuのvagrantVMをprivate_networkで作成する
ubuntuのboxがあることを確認
$ vagrant box listubuntu/trusty64 (virtualbox, 20190514.0.0)
適当にディレクトリを作る。
├── vagrant_machins│ ├── vagrantVM1│ ││ └── vagrantVM2│
vagrantVM1に移動してVagrantfileを作る
$ vagrant init ubuntu/trusty64A `Vagrantfile` has been placed in this directory. You are nowready to `vagrant up` your first virtual environment! Please readthe comments in the Vagrantfile as well as documentation on`vagrantup.com` for more information on using Vagrant.$ lsVagrantfile
vagrantVM1のVagrantfileを編集してprivate_networkを
設定する
最初はコメントアウトされている
# config.vm.network "private_network"
↓
config.vm.network "private_network", ip: "192.168.59.11"
有効化してipアドレスを設定する
※ipアドレスは192.168.56.1 ~ 192.168.63.254の間でないとエラーになる。
vagrantVM1を起動してネットワーク設定を確認してみる
Vagrantfileがあるディレクトリに移動して起動する
$ vagrant up
起動できたらログインする
$ vagrant statusCurrent machine states:default running (virtualbox)The VM is running. To stop this VM, you can run `vagrant halt` toshut it down forcefully, or you can run `vagrant suspend` to simplysuspend the virtual machine. In either case, to restart it again,simply run `vagrant up`.
runningになっていれば起動できている。
vagrant sshでログインする
※初期パスワードは「vagrant」
$ vagrant sshvagrant@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 2023System load: 0.29 Processes: 77Usage of /: 3.6% of 39.34GB Users logged in: 0Memory usage: 25% IP address for eth0: 10.0.2.15Swap usage: 0% IP address for eth1: 192.168.59.11Graph 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 statusNew 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.2vagrant@vagrant-ubuntu-trusty-64:~$
vagrant@vagrant-ubuntu-trusty-64:~$ hostname -I10.0.2.15 192.168.59.11vagrant@vagrant-ubuntu-trusty-64:~$ routeKernel IP routing tableDestination Gateway Genmask Flags Metric Ref Use Ifacedefault 10.0.2.2 0.0.0.0 UG 0 0 0 eth010.0.2.0 * 255.255.255.0 U 0 0 0 eth0192.168.59.0 * 255.255.255.0 U 0 0 0 eth1
問題なく設定されている。
同様の手順でvagrantVM2を作成する
※手順で変わるのはVagrantfileのIPアドレスだけ
vagrantVM2のVagrantfileを編集してprivate_networkを
設定する
config.vm.network "private_network", ip: "192.168.59.22"
vagrantVM1と同じネットワークに所属させるため
IPアドレスのネットワーク部は192.168.59にする
ちゃんと内部ネットワークになっているのか確認してみる
確認のため
でsshで通信できるか確認する
イメージとしては下記のようになってるため 「外部マシンからvagrantVM1」以外は通信できる
まずはホストでipを調べてみると
$ ip addr12: vboxnet1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000link/ether 0a:00:27:00:00:01 brd ff:ff:ff:ff:ff:ffinet 192.168.59.1/24 brd 192.168.59.255 scope global vboxnet1valid_lft forever preferred_lft foreverinet6 fe80::800:27ff:fe00:1/64 scope linkvalid_lft forever preferred_lft forever
vboxnet1として追加されている
ホストからvagrantVM2にsshする
# ホストからssh$ ssh vagrant@192.168.59.22The 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 namesAre you sure you want to continue connecting (yes/no/[fingerprint])? yesWarning: 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 2023System load: 0.52 Processes: 85Usage of /: 3.6% of 39.34GB Users logged in: 0Memory usage: 25% IP address for eth0: 10.0.2.15Swap usage: 0% IP address for eth1: 192.168.59.22Graph 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 statusNew 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にsshする
# hostからvagrantVM1にssh$ ssh vagrant@192.168.59.11vagrant@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 2023System load: 0.35 Processes: 76Usage of /: 3.6% of 39.34GB Users logged in: 0Memory usage: 24% IP address for eth0: 10.0.2.15Swap usage: 0% IP address for eth1: 192.168.59.11Graph 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 statusNew 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.22vagrant@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 2023System load: 0.08 Processes: 74Usage of /: 3.6% of 39.34GB Users logged in: 0Memory usage: 24% IP address for eth0: 10.0.2.15Swap usage: 0% IP address for eth1: 192.168.59.22Graph 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 statusNew 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.11vagrant@vagrant-ubuntu-trusty-64:~$
vagrantVM同士もsshができる!
外部マシンからvagrantVMに関してはネットワークが違うため
通信できない
結果としては下記のようになる
ホストが所属するLAN内にvagrantVMのIPを割り当てる。
LAN上でvagrantVMは通常マシンと同様に見えるため
そのLAN内PCであれば通信可能になる
※ネットワーク上のマシンからもアクセスできるので、セキュリティには注意が必要
イメージとしては下記のような感じになる
vagrantVM3のフォルダを作る。
├── vagrant_machins│ ├── vagrantVM1│ ││ ├── vagrantVM2│ ││ └── vagrantVM3│
vagrantVM3に移動してVagrantfileを作る
$ vagrant init ubuntu/trusty64A `Vagrantfile` has been placed in this directory. You are nowready to `vagrant up` your first virtual environment! Please readthe comments in the Vagrantfile as well as documentation on`vagrantup.com` for more information on using Vagrant.$ lsVagrantfile
vagrantVMが稼働している端末のbridgeするインターフェースを調べる
$ VBoxManage list bridgedifs |grep '^Name:'Name: wlp2s0~
インターフェースを調べて後述するVagrantfileに
記述する。
※記述しないとどのインターフェースを使うか、
起動時に聞かれるのでめんどくさい。。。
vagrantVM1のVagrantfileを編集してpublic_networkを
設定する
最初はコメントアウトされている
# config.vm.network "public_network"
↓
config.vm.network "public_network", ip: "192.168.0.33",bridge: "wlp2s0"
有効化してipアドレスを設定する
IPアドレスはホストが属してるネットワークにする
$ routeカーネルIP経路テーブル受信先サイト ゲートウェイ ネットマスク フラグ Metric Ref 使用数 インタフェースdefault _gateway 0.0.0.0 UG 600 0 0 wlp2s0192.168.0.0 0.0.0.0 255.255.255.0 U 600 0 0 wlp2s0192.168.59.0 0.0.0.0 255.255.255.0 U 0 0 0 vboxnet1
vagrantVM3を起動してネットワーク設定を確認してみる
Vagrantfileがあるディレクトリに移動して起動する
$ vagrant up
起動できたらログインしてIPアドレスとルートを確認する
# 起動確認$ vagrant statusCurrent machine states:default running (virtualbox)The VM is running. To stop this VM, you can run `vagrant halt` toshut it down forcefully, or you can run `vagrant suspend` to simplysuspend the virtual machine. In either case, to restart it again,simply run `vagrant up`.# ログインash92@as-ubuntu:~/vagrant_machins/vagrantVM3$ vagrant sshvagrant@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 2023System load: 0.79 Processes: 84Usage of /: 3.6% of 39.34GB Users logged in: 0Memory usage: 26% IP address for eth0: 10.0.2.15Swap 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 statusNew release '16.04.7 LTS' available.Run 'do-release-upgrade' to upgrade to it.# IPアドレス確認vagrant@vagrant-ubuntu-trusty-64:~$ hostname -I10.0.2.15 192.168.0.33 2001:ce8:161:4fb4:a00:27ff:fe6e:cc6e# ルート確認vagrant@vagrant-ubuntu-trusty-64:~$ routeKernel IP routing tableDestination Gateway Genmask Flags Metric Ref Use Ifacedefault 10.0.2.2 0.0.0.0 UG 0 0 0 eth010.0.2.0 * 255.255.255.0 U 0 0 0 eth0192.168.0.0 * 255.255.255.0 U 0 0 0 eth1vagrant@vagrant-ubuntu-trusty-64:~$
ホストのネットワークに追加されていることを確認できた
ちゃんとパブリックネットワークになっているのか確認してみる
確認のため
同じネットワーク(192.168.0.0)のため通信できる
$ ssh vagrant@192.168.0.33The 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 namesAre you sure you want to continue connecting (yes/no/[fingerprint])? yesWarning: 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 2023System load: 0.42 Processes: 78Usage of /: 3.6% of 39.34GB Users logged in: 0Memory usage: 25% IP address for eth0: 10.0.2.15Swap usage: 0% IP address for eth1: 192.168.0.33Graph 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 statusNew 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.2vagrant@vagrant-ubuntu-trusty-64:~$
問題なくsshできる
ネットワーク上でvagrantVM3は独自のIPアドレスを持つ通常のマシン
のようになっているので、通信可能。
ash92@as-ubuntu:~/vagrant_machins/vagrantVM3$ vagrant global-status -a/home/ash92/vagrant_machins/vagrantVM1default running (virtualbox) 2023-09-10 03:18:50 +0900/home/ash92/vagrant_machins/vagrantVM2default running (virtualbox) 2023-09-10 03:01:39 +0900/home/ash92/vagrant_machins/vagrantVM3default running (virtualbox) 2023-09-11 18:30:32 +0900
vagrantの仮想マシンを一覧で確認する方法については下記参照
# vatrantVM3にログイン$ vagrant sshvagrant@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 2023System load: 0.0 Processes: 77Usage of /: 3.6% of 39.34GB Users logged in: 1Memory usage: 26% IP address for eth0: 10.0.2.15Swap usage: 0% IP address for eth1: 192.168.0.33Graph 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 statusNew 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.11vagrant@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 2023System load: 0.0 Processes: 74Usage of /: 3.9% of 39.34GB Users logged in: 0Memory usage: 27% IP address for eth0: 10.0.2.15Swap usage: 0% IP address for eth1: 192.168.59.11Graph 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 --upgradableEnable UA Infrastructure ESM to receive 193 additional security updates.See https://ubuntu.com/advantage or run: sudo ua statusNew 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.2vagrant@vagrant-ubuntu-trusty-64:~$
仮想マシン同士もログインできる
Windowsからもログインできる
結果としては下記のようになる
ブリッジ設定した場合は基本的に全て通信可能になる
※その分、注意が必要
vagrantのネットワーク設定について
の設定方法とその動作について実際に動かして
まとめてみた。
個人的には基本的には内部ネットワークでいいかなと思った。
※あまり外部からアクセスさせる必要もないので