LoginSignup
3
0

More than 3 years have passed since last update.

VagrantでVirtual Box上に作ったゲストマシン(CentOS)の共有フォルダが同期しない

Posted at

記事概要

CentOS 7の公式のVagrant boxから作ったVirtual Box上の仮想マシンにおいて、共有フォルダが同期されませんでした。原因と解決策について記します。

記事詳細

実行環境

Windows 10 Home
Virtual Box 6.0.14
Vagrant 2.2.6

原因

上記の環境において、共有フォルダが同期されない原因は、CentOS 7の公式のVagrant boxguest additionsがインストールされていないことです。

guest additionsとは、Virtual Boxのホストマシンとゲストマシンの連携を良くするためのツールです。

vagrant upでゲストマシンを起動すると、以下のようなメッセージが表示されます。

==> 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.

共有フォルダが動作するにはguest additionsが必要で、CentOS 7の公式のVagrant boxには、guest additionsがインストールされてないと書かれています。

ちなみに、Ubuntu 18.04の公式のVagrant boxは、guest additionsが最初からインストールされています。

解決策

以下の2つの作業を行うことで、共有フォルダが同期されるようになりました。

【1】vagrant-vbguestプラグインのインストール

vagrant-vbguestプラグインをインストールします。
vagrant-vbguestは、ホストマシンのguest additionsをゲストマシンに自動的にインストールしてくれるVagrantのプラグインです。

インストールは、以下のコマンドを入力するだけです。

vagrant plugin install vagrant-vbguest

これで、ゲストマシンの起動時にguest additionsが自動的にインストールされるようになりました。

【2】共有フォルダのtypeの指定

共有フォルダのtypeをvirtualboxに指定します。
Vagrantfileに以下のような設定を記述することで、共有フォルダのtypeを指定できます(ホストマシンとゲストマシンのディレクトリの設定は自分の環境に合わせて下さい)。

config.vm.synced_folder ".", "/vagrant", type: "virtualbox"

通常、vagrantは最適なtypeを自動的に設定してくれます。
従って、通常はtypeを設定する必要はありません。

しかし、今回の場合はtypeを明示的に設定する必要がありました。

これで、ゲストマシンの共有フォルダが同期されるようになりました。

3
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
3
0