13
13

More than 5 years have passed since last update.

vagrantでCentOSマシンを構築する

Posted at

準備

  • vagrantのインストール
  • virtualboxのインストール

コチラの記事にインストールやvagrantの基本的な使い方が書いてあります。
よかったら参考にして下さい。

CentOSのbox追加

今回はCentOS6.5を使用します。
box追加コマンドの書式は以下になります。

vagrant box add "boxの名称" "boxのURL"

boxの名称については、任意の名称をつけられます。
OS名称、バージョンなどが入った管理しやすいものをオススメします。

それでは実際に追加します。

$ vagrant box add centos6.5-64 https://github.com/2creatives/vagrant-centos/releases/download/v6.5.1/centos65-x86_64-20131205.box

結構時間かかるので筋トレでもして待って下さい。
完了したらboxが追加されたことを確認しておきます。

$ vagrant box list
centos6.5-64 (virtualbox)

と表示されればオッケーです。

vagrant init

テストなので/tmp下にディレクトリ切って進めます。

$ mkdir /tmp/vagrant-centos
$ cd /tmp/vagrant-centos

先ほど追加した"centos6.5-64"を指定してVagrantfileを初期化します。

$ vagrant init centos6.5-64
$ ls
Vagrantfile

Vagrantfileが作られていれば完了です。

Vagrantfileの編集

box名(centos6.5-64)を指定してinitするとどうなるかというと…

Vagrantfile
  # ↓が指定したboxになってます。
  config.vm.box = "centos6.5-64"

"vagrant box add"でローカルに落としてあるので、
config.vm.box_urlの設定は不要となります。

他の設定に関してはコチラの設定を使用します。
※config.vm.synced_folderだけ変更

Vagrantfile
# -*- mode: ruby -*-
# vi: set ft=ruby :

Vagrant.configure("2") do |config|

  config.vm.box = "centos6.5-64"

  config.vm.network :forwarded_port, guest: 22, host:2222, id: "ssh"

  # Create a private network, which allows host-only access to the machine
  # using a specific IP.
  config.vm.network :private_network, ip: "192.168.33.10"                     
  # Shared folders
  config.vm.synced_folder "../vagrant-centos", "/vagrant-centos"

  # VM option
  config.vm.provider "virtualbox" do |v|
    v.customize ["modifyvm", :id, "--memory","1028"]
    v.customize ["modifyvm", :id, "--cpus","1"]
    v.customize ["modifyvm", :id, "--natdnshostresolver1", "on"]
  end

end

起動

仮想環境を起動させます。
bash
$ vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
[default] Clearing any previously set forwarded ports...
[default] Creating shared folders metadata...
[default] Clearing any previously set network interfaces...
[default] Preparing network interfaces based on configuration...
[default] Forwarding ports...
[default] -- 22 => 2222 (adapter 1)
[default] Running 'pre-boot' VM customizations...
[default] Booting VM...
[default] Waiting for machine to boot. This may take a few minutes...
[default] Machine booted and ready!
[default] Configuring and enabling network interfaces...
[default] Mounting shared folders...
[default] -- /vagrant
[default] -- /vagrant-centos

無事起動できました。
"vagrant ssh"で接続できるか確認して完了です。

おわり

試したいOS・バージョンが簡単に入れられるので便利!

13
13
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
13
13