見出し画像

Virtual Box + Vagrant環境構築

CentOSに入門したくて下準備としてVirtual Box + Vagrantの環境構築を行った際に詰まった箇所などをメモります。

環境:Mac

% sw_vers
ProductName:	Mac OS X
ProductVersion:	10.15.5
BuildVersion:	19F101

Virtual BoxとVagrantはbrew caskでインストール済み

% VBoxManage -v
6.1.12r139181
% vagrant -v
Vagrant 2.2.9


下準備が終わったので次はVagrantでCentOS8のBoxをインストールする。

BoxとはVagrant公式から引用するとこういうものらしい。

Boxes are the package format for Vagrant environments. A box can be used by anyone on any platform that Vagrant supports to bring up an identical working environment.

BoxとはVagrantの環境構築の為のパッケージフォーマットのこと。BoxはVagrantがサポートするどんな環境のどの人にも、同じように動く環境を提供する。(みたいな意味ですかね。大体笑)

つまりはここ(VagrantCloud)から同じイメージを引っ張ってくれば誰もが同じ環境を構築できますよってことっぽいですね。

私はCentOSに入門したかったのでgeneric/centos8っていうイメージを取得することにしました。

イメージの取得方法としてはvagrant box addコマンドを実行する方法があります。

generic/centos8を指定してコマンドを実行したところ下記のようなエラーが発生。

% vagrant box add generic/centos8
==> box: Loading metadata for box 'generic/centos8'
   box: URL: https://vagrantcloud.com/generic/centos8
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) docker
2) hyperv
3) libvirt
4) parallels
5) virtualbox
6) vmware_desktop

Enter your choice: 5
==> box: Adding box 'generic/centos8' (v3.0.22) for provider: virtualbox
   box: Downloading: https://vagrantcloud.com/generic/boxes/centos8/versions/3.0.22/providers/virtualbox.box
Download redirected to host: vagrantcloud-files-production.s3.amazonaws.com
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: Connection reset by peer, errno 54

ググってみたところ~/.vagrant.d/tmp/配下に生成されたBox~という一時ファイルを削除して再実行するとうまくいくという記事もありましたが、私の場合は失敗しました。

SSL認証が失敗しているようで、原因としては調べてみましたがこれというものが最後までわかりませんでした。

最終手段として下記コマンドで成功しました。

% vagrant box add generic/centos8 -c --insecure

-c or --clean : 一時ファイルをcleanしてからインストール

--insecure : SSL認証を検証しないでインストール

% vagrant box add generic/centos8 -c --insecure
==> box: Loading metadata for box 'generic/centos8'
   box: URL: https://vagrantcloud.com/generic/centos8
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) docker
2) hyperv
3) libvirt
4) parallels
5) virtualbox
6) vmware_desktop

Enter your choice: 5
==> box: Adding box 'generic/centos8' (v3.0.22) for provider: virtualbox
   box: Downloading: https://vagrantcloud.com/generic/boxes/centos8/versions/3.0.22/providers/virtualbox.box
Download redirected to host: vagrantcloud-files-production.s3.amazonaws.com
   box: Calculating and comparing box checksum...
==> box: Successfully added box 'generic/centos8' (v3.0.22) for 'virtualbox'!

VagrantCloudのgeneric/centos8ページを覗くと、Vagrantfileを生成するコマンドがあったのでそれを実行。

% vagrant init generic/centos8
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.

Vagrantfile生成に成功したら下記コマンドで仮想マシンを起動。

% vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Importing base box 'generic/centos8'...
==> default: Matching MAC address for NAT networking...
==> default: Checking if box 'generic/centos8' version '3.0.22' is up to date...
==> default: Setting the name of the VM: centos8_default_1596907831979_94818
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
   default: Adapter 1: nat
==> default: Forwarding ports...
   default: 22 (guest) => 2222 (host) (adapter 1)
==> default: Running 'pre-boot' VM customizations...
==> 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:
   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: The guest additions on this VM do not match the installed version of
   default: VirtualBox! In most cases this is fine, but in rare cases it can
   default: prevent things such as shared folders from working properly. If you see
   default: shared folder errors, please make sure the guest additions within the
   default: virtual machine match the version of VirtualBox you have installed on
   default: your host and reload your VM.
   default:
   default: Guest Additions Version: 5.2.44 r139111
   default: VirtualBox Version: 6.1

仮想マシン起動に成功したら下記コマンドでSSH接続。

% vagrant ssh
[vagrant@centos8 ~]$

成功しました!

ちなみに仮想マシンから抜けてシャットダウンするのは以下のコマンドです。

[vagrant@centos8 ~]$ exit
logout
Connection to 127.0.0.1 closed.
% vagrant halt
==> default: Attempting graceful shutdown of VM...


一旦一通り終了です。細かい設定などは後々やっていこうと思います。

この記事が気に入ったらサポートをしてみませんか?