Closed4

Vagrantを使って、同じネットワーク上に仮想のUbuntuを立ち上げる

mobmob

vagrant の install

  • MacOSでやる前提
$ brew install vagrant

VirtualBox がないと動かない(↓みたいになる)。

$ vagrant up
No usable default provider could be found for your system.

Vagrant relies on interactions with 3rd party systems, known as
"providers", to provide Vagrant with resources to run development
environments. Examples are VirtualBox, VMware, Hyper-V.

The easiest solution to this message is to install VirtualBox, which
is available for free on all major platforms.

If you believe you already have a provider available, make sure it
is properly installed and configured. You can see more details about
why a particular provider isn't working by forcing usage with
`vagrant up --provider=PROVIDER`, which should give you a more specific
error message for that particular provider.

Virtual Box もインストールする。

mobmob

Vagrant を立ち上げる

Vagrantfile はこんな感じにした。

Vagrant.configure("2") do |config|
  # 使用する Ubuntu のボックスを指定
  config.vm.box = "ubuntu/bionic64"
  
  # 仮想マシンの名前を指定
  config.vm.hostname = "ubuntu-vm"

  # ブリッジネットワークを使用してホストネットワークに接続
  config.vm.network "public_network"

  # 仮想マシンのメモリとCPUを設定
  config.vm.provider "virtualbox" do |vb|
    vb.memory = "2048"  # 仮想マシンに割り当てるメモリ(MB単位)
    vb.cpus = 2         # 仮想マシンに割り当てるCPUコア数
  end

  # 仮想マシンのプロビジョニング(必要に応じてスクリプトを追加)
  config.vm.provision "shell", inline: <<-SHELL
    apt-get update
    apt-get upgrade -y
    apt-get install -y apache2
  SHELL
end

下記の設定で、 MacOS と同じネットワークにぶら下がるはず...

config.vm.network "public_network"
$ vagrant up

...
==> default: Available bridged network interfaces:
1) en0: Wi-Fi
2) en5: USB Ethernet(?)
3) awdl0
4) llw0
5) en8: Thunderbolt 3
6) en7: Thunderbolt 4
7) en3: Thunderbolt 1
8) en4: Thunderbolt 2
9) bridge0
==> default: When choosing an interface, it is usually the one that is
==> default: being used to connect to the internet.
==> default: 
    default: Which interface should the network bridge to? 1 
...
mobmob

とりあえず vagrant ssh

$ 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 という名前で立ち上がっている。そのため、下記で ssh できる。

$ vagrant ssh default

中に入って、ipアドレスを見てみる。

vagrant@ubuntu-vm:~$ ip addr show
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    ...
3: enp0s8: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 08:00:27:ae:05:af brd ff:ff:ff:ff:ff:ff
    inet 192.168.97.131/24 brd 192.168.97.255 scope global dynamic enp0s8
       valid_lft 86033sec preferred_lft 86033sec
    inet6 fe80::a00:27ff:feae:5af/64 scope link 
       valid_lft forever preferred_lft forever

無事 MacOS と同一ネットワーク内に192.168.97.131 というプライベートアドレスが割り振られているもよう。

MacOS から vagrant ssh ではなくて ssh vagrant@192.168.97.131 で接続できるように、sshの設定をする。

MacOS側で 公開鍵 を確認するため、下記を MacOS で実行。 公開鍵は必要に応じて変えてください。

$ cat ~/.ssh/id_rsa.pub | pbcopy

Vagrant の 仮想環境内で 公開鍵を登録する。

$ vim .ssh/authorized_keys
// 末尾に コピーしたものを貼り付けて保存する

MacOS から ssh接続できるか確認

$ ssh vagrant@192.168.97.131
mobmob

今回は確認だけだったので vm は削除しておく

$ vagrant destroy default
このスクラップは2024/06/06にクローズされました