LoginSignup
2
5

More than 5 years have passed since last update.

Windows版VirtualBox+VagrantをBash on Ubuntu on Windowsで操作して、ubuntu17.04環境を構築

Posted at

開発用PCを買ったので、早速VirtualBox+Vagrantで環境構築。
Bash on Ubuntu on Windows(BoWと略すらしい)でWindowsアプリが実行できるようになったと聞いたので、BoWで構築してみました。

VirtualBoxとVagrantをインストール

Windows版をインストールします
https://www.virtualbox.org/wiki/Downloads
https://www.vagrantup.com/downloads.html

Vagrant用の作業フォルダを作成

$ cd /mnt/c/
$ mkdir vagrant
$ cd vagrant

作業ディレクトリはどこに作成しても良いというわけではなく、例えば~/vagrant/vagrant initしようとすると、このようなエラーが出てうまくいきません。

現在の作業ディレクトリを変換できません。C:\Windows\system32 を使用します
The user that is running Vagrant doesn't have the proper permissions
to write a Vagrantfile to the specified location. Please ensure that
you call `vagrant init` in a location where the proper permissions
are in place to create a Vagrantfile.

これは、windowsアプリケーションがBoW制御下のディレクトリを操作してはいけないからみたいです。
https://blogs.msdn.microsoft.com/commandline/2016/11/17/do-not-change-linux-files-using-windows-apps-and-tools/
英語読めないので詳しくは分かりませんが、とにかく作業ディレクトリは/mnt/c配下に作成しましょう。
ちなみに/mnt/cとは、C:\のことです。

Vagrantfileの作成

今回は2017/8/15現在でubuntuの最新版である17.04環境を作ります。boxは一番有名(?)なお弁当box使います。
https://app.vagrantup.com/bento/boxes/ubuntu-17.04

vagrant initを実行

$ sudo vagrant init bento/ubuntu-17.04
sudo: vagrant: コマンドが見つかりません

おっと、.exeを付けないとWindowsコマンドではなく、linuxコマンドとして認識してしまいます。

$ sudo vagrant.exe init bento/ubuntu-17.04
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 -a
 ./ ../ Vagrantfile

上手く作成できました。

仮想マシンの起動

vagrant upを.exeつきで実行するだけ。
毎回exeつけ忘れちゃうので、aliasに登録したほうがいいですね。

$ sudo vagrant.exe up
Bringing machine 'default' up with 'virtualbox' provider...
以下略

ログイン

vagrant.exe sshだと無応答?状態になってしまうので、sshでログインします。

vagrant.exe ssh-configで情報を取得してからssh

$ vagrant.exe ssh-config
Host default
  HostName 127.0.0.1
  User vagrant
  Port 2222
  UserKnownHostsFile /dev/null
  StrictHostKeyChecking no
  PasswordAuthentication no
  IdentityFile C:/vagrant/.vagrant/machines/default/virtualbox/private_key
  IdentitiesOnly yes
  LogLevel FATAL

$ sudo ssh -p 2222 vagrant@127.0.0.1
The authenticity of host '[127.0.0.1]:2222 ([127.0.0.1]:2222)' can't be established.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '[127.0.0.1]:2222' (ECDSA) to the list of known hosts.
vagrant@127.0.0.1's password:
Welcome to Ubuntu 17.04 (GNU/Linux 4.10.0-28-generic x86_64)

 * Documentation:  https://help.ubuntu.com
 * Management:     https://landscape.canonical.com
 * Support:        https://ubuntu.com/advantage

 * What are your preferred Linux desktop apps?  Help us set the default
   desktop apps in Ubuntu 18.04 LTS:
   - https://ubu.one/apps1804

0 packages can be updated.
0 updates are security updates.


Last login: Tue Aug 15 03:43:26 2017 from 10.0.2.2
vagrant@vagrant:~$

デフォルトパスワードはvagrantです。

まとめ

Bash on Ubuntu on WindowsでVagrantの操作をすることができました。
ただ、vagrant.exe sshが使えなかったり、sudo ssh時に無応答状態に陥る(もう1つBoW開いて、そこで叩くと普通に動く。これで2時間くらい無駄にした・・・)など、不安定な面もありました。
BoWによってcygwinいらなくなるかなと期待していましたが、安定するまではお世話になりそうです。

2
5
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
2
5