MacBookにVagrantでCentOS7の開発環境を構築

  • メモ書き

Mac環境(インストール済み)

作業手順

$ mkdir centos74
$ cd centos74
  • vagrant boxからimageをダウンロードする(今回はvirtualboxを利用しているので3を選択)
  • (2018/02/18時点)centos7の最新をダウンロードするとv7.4
$ vagrant box add centos/7 https://app.vagrantup.com/centos/boxes/7
==> box: Loading metadata for box 'https://app.vagrantup.com/centos/boxes/7'
This box can work with multiple providers! The providers that it
can work with are listed below. Please review the list and choose
the provider you will be working with.

1) hyperv
2) libvirt
3) virtualbox
4) vmware_desktop

Enter your choice: 3
==> box: Adding box 'centos/7' (v1801.02) for provider: virtualbox
    box: Downloading: https://vagrantcloud.com/centos/boxes/7/versions/1801.02/providers/virtualbox.box
==> box: Successfully added box 'centos/7' (v1801.02) for 'virtualbox'!
  • ダウンロードされていることを確認
$ vagrant box list
centos/7               (virtualbox, 1801.02)
  • 先程ダウンロードしてきたboxを指定して、Vagrantfileを作成
$ vagrant init centos/7
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.
  • 作成されていることを確認
$ ls -l
total 4
-rw-r--r-- 1 ******* staff 3018  2 18 20:36 Vagrantfile
  • 複数Vagrant仮想マシンを立ち上げたときに競合しないようにVagrantfileの下記を編集して、IPを固定する(今回は192.168.33.90に設定)
 config.vm.network "private_network", ip: "192.168.33.90"
  • 起動
$ vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Importing base box 'centos/7'...
==> default: Matching MAC address for NAT networking...
==> default: Checking if box 'centos/7' is up to date...
==> default: Setting the name of the VM: centos74_default_1518953817603_29630
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
    default: Adapter 1: nat
    default: Adapter 2: hostonly
==> default: Forwarding ports...
    default: 22 => 2222 (adapter 1)
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
    default: SSH address: 127.0.0.1:2222
    default: SSH username: vagrant
    default: SSH auth method: private key
    default: Warning: Connection timeout. Retrying...
    default: 
    default: Vagrant insecure key detected. Vagrant will automatically replace
    default: this with a newly generated keypair for better security.
    default: 
    default: Inserting generated public key within guest...
    default: Removing insecure key from the guest if it's present...
    default: Key inserted! Disconnecting and reconnecting using new SSH key...
==> default: Machine booted and ready!
==> default: Checking for guest additions in VM...
    default: No guest additions were detected on the base box for this VM! Guest
    default: additions are required for forwarded ports, shared folders, host only
    default: networking, and more. If SSH fails on this machine, please install
    default: the guest additions and repackage the box to continue.
    default: 
    default: This is not an error message; everything may continue to work properly,
    default: in which case you may ignore this message.
==> default: Configuring and enabling network interfaces...
==> default: Rsyncing folder: /Users/*******/vagrant/centos74/ => /vagrant
  • 起動が終わったらsshログイン
$ vagrant ssh
[vagrant@10 ~]$ cat /etc/redhat-release 
CentOS Linux release 7.4.1708 (Core)