0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

[Vagrant][VirtualBox][Mac] Mac環境でVagrantからVirtualBoxを起動する_2

Last updated at Posted at 2025-01-19

環境

MacBook Pro (2.3 GHz 8コアIntel Core i9)
macOS 14.0(23A344)
Homebrew 4.3.8
gh 2.52.0

目次

ディレクトリ構成

directory_structure
drive/ ←ルートディレクトリ
├── Applications/
├── Syetem/
└── Usr/
│   ├── shared/
│   └── personnel(ex. Jochun)/ ←ホームディレクトリ
│       ├── MyVagrant/
│       │   ├── MyTest001/
│       │   │   ├── Vagrantfile (viniciusfs/centos7) 
│       │   │   └── .vagrant (仮想マシン)
│       │   └── MyTest002/
│       │       ├── Vagrantfile (ubuntu/trusty64) ←今回インストールしたVagrantboxによって作成されたVadrantfile
│       │       └── .vagrant (仮想マシン)
│       ├── Desktop/
│       ├── Downloads/
│       ├── Documents/
│       ├── .config/
│       ├── .ssh/
│       ├── .zshrc/
│       └── .local/
│           ├── .bin
│           └── 

手順

terminal
// Vagrantで管理する仮想マシン用にホームディレクトリ直下にディレクトリを作成する
$ mkdir MyVagrant

// 作成したディレクトリに移動する
$ cd MyVagrant

// 仮想サーバ1台につき、1つのディレクトリが必要なため、ディレクトリを作成する
$ mkdir MyTest002

// 作成したディレクトリに移動する
$ cd MyTest002

// 使用したいVagrantbox※1をインストールする
// $ vagrant box add [box-name]
$ vagrant box add ubuntu/trusty64

    // 過去の方法
    // $ vagrant box add [box-name] [URL※2]
    $ vagrant box add ubuntu/trusty64 https://atlas.hashicorp.com/viniciusfs/boxes/centos7/()

// 仮想マシン設定用のVagrantfileを作成する
// $ vagrant init [box-name]
$ vagrant init ubuntu/trusty64

// 仮想マシンを起動する
$ vagrant up

// 仮想マシンの状態を確認する
// running※3となっていれば正常に動作している
$ vagrat status

VirtualBoxを開くと下記のように、仮想マシンが作成されていることが確認できる。
image.png

注意点

  1. ※1
    主要なVagrantBoxはDiscover Vagrant Boxesから確認できる
    Discover Vagrant Boxesに記載のBox nameを手順の[box-name]に入力する
    image.png

  2. ※2
    URLはここのURL列から取得できる
    image.png

  3. ※2
    最新のVagrantでは、URLの記載は不要。

    古いバージョンのVagrantではURLを指定していましたが、今はBoxの名前を指定できるようになったので名前を指定するのが一般的です。
    Vagrant の Box の基本操作(追加・更新・削除・一覧)を覚えよう

    This will download the box named hashicorp/bionic64 from HashiCorp's Vagrant Cloud box catalog, where you can find and host boxes.
    Install a box (optional)

  4. ※3
    下記の表示
    image.png

仮想マシンにSSH接続する

terminal
// 仮想マシンにログインする(SSH接続する)
$ vagrant ssh

// ログアウトする
vagrant@localhost$ exit

SSH接続情報を~/.ssh/configファイルに設定して、sshコマンドで仮想マシンへSSHログインできるようにする

terminal
// 仮想マシンへのSSH接続情報を確認する
$ vagrant ssh-config

SSH接続情報が表示される(Host名はdefaultであることが確認できる)
image.png

terminal
// SSH接続情報を~/.ssh/configファイルに追記する
$ vagrant ssh-config >> ~/.ssh/config

// 仮想マシンにログインする(SSH接続する)-sshコマンド
$ ssh default

// ログアウトする
vagrant@localhost$ exit

参考リンク

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?