LoginSignup
4
10

More than 5 years have passed since last update.

今更VagrantとVirtualBoxでMacにCentOS7の環境を作る

Last updated at Posted at 2018-05-30

基本概念

VirtualBoxは仮想マシンで、それ単体で仮想環境を築くことができます。
ただし、各種設定やOSのインストールなどが面倒なので、
Vagrantというツールを導入し、VagrantからVirtualBoxを操作します。

VirtualBoxをインストール

今回入れたバージョンは5.1.8 r111374 (Qt5.5.1)でした。
ダウンロードはこちらからどうぞ。https://www.virtualbox.org/

Vagrantをインストール

今回入れたバージョンはVagrant 2.0.1でした。
ダウンロードはこちらからどうぞ。https://www.vagrantup.com/

Vagrantのコマンドでいろいろと実行する

1. 仮想ファイルを入れるディレクトリを作成する

今回はhome直下にMyVagrantというディレクトリを作り、そこにCentOS7というディレクトリを作りました。

$ cd
$ mkdir MyVagrant
$ cd MyVagrant
$ mkdir CentOS7
$ cd CentOS7

2. CentOS7のBoxをダウンロードする

BoxとはOSのディスクイメージファイル等が含まれたファイルのことで、このBoxを元に環境を構築していきます。今回はCentOS7のBoxをダウンロードします。
ダウンロードの仕方は下記になります

$ vagrant box add [好きなBOX名] [BoxのURL]

BoxのURLは下記サイトから対応するものを探してきます
http://www.vagrantbox.es/
今回はcentos7というBox名にしてインストールします(ちなみに結構時間かかります)

$ vagrant box add centos7 https://github.com/holms/vagrant-centos7-box/releases/download/7.1.1503.001/CentOS-7.1.1503-x86_64-netboot.box

なんかエラーが発生しました。載せておきます。

An error occurred while downloading the remote file. The error
message, if any, is reproduced below. Please fix this error and try
again.
OpenSSL SSL_read: SSL_ERROR_SYSCALL, errno 60

調べたところ、回線の問題でタイムアウトしてしまうことがあるとのことで、再度実行したら今度はうまくいきました。なんやねん。

==> box: Successfully added box 'centos7' (v0) for 'virtualbox'!

一応ちゃんとBoxが入ったか以下のコマンドで確認しておきます。
以前入れていたcentos6.8の下に新たにcentos7が作られているのでOKですね。

$ vagrant box list
bento/centos-6.8 (virtualbox, 2.3.0)
centos7          (virtualbox, 0)

3. 先ほどのBoxを指定して初期化する

$ vagrant init centos7

実行結果は以下。VagrantFileが新しくできてvagrant upする準備が整ったと書いてありますね。

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.

4. vagrantfileを変更する

192.168.33.10でアクセス可能になるようにvagrantfileを変更します。
ちなみにドットインストールでは以下のコマンドを実行しています。

sed -i '' -e 's/# config.vm.network "private_network", ip: "192.168.33.10"/config.vm.network "private_network", ip: "192.168.33.10"/' Vagrantfile

これは要するにprivate_networkのipアドレス指定しているところのコメントを外しているだけです。
vimで直接コメントを外しにいっても変わらないです。参考までにsedの使い方載せておきます。

sed s/置換前/置換後 ファイル名

5. Vagrantを立ち上げる(仮想マシンの起動)

準備は整ったので、仮想マシンを立ち上げます。

$ vagrant up

少し時間がかかりましたが、うまくいったようなのでアクセスしてみます

$ vagrant ssh
Welcome to your Vagrant-built virtual machine.
[vagrant@localhost ~]$ 

アクセスできました!
これでCentOS7の仮想環境を作成できました。
OSの設定は別の記事で書こうと思います。

4
10
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
4
10