尋常でないもふもふ

a software engineer blog

Vagrantの使い方

インストール

公式の Download Vagrant - Vagrant からダウンロードしてインストール。

つかう

Ubuntu 14.04 を構築

$ mkdir vagrant-sample
$ cd vagrant-sample
$ vagrant init -m ubuntu/trusty64

-mは生成される Vagrantfile を説明コメントが除かれたミニマルな状態にするオプション。
ubuntu/trusty64は box 名。GitHubリポジトリ名と同じ感じ。 box は公式の Discover Vagrant Boxes | Atlas by HashiCorp から探してくる。

起動

$ vagrant up

最初だけ構築に数分かかる。

ストップ

$ vagrant halt

完全削除

$ vagrant destroy

SSH 接続

$ vagrant ssh

これで OK。

vagrant コマンドを使わずに一般的な SSH 接続したい場合は vagrant ssh-configSSH 用の config ファイルを作成できる。以下の場合 --host で 'ubuntu' という名前をつけてる。未指定の場合は 'default' になる。

$ vagrant ssh-config --host ubuntu >> ~/.ssh/config
$ ssh ubuntu
Welcome to Ubuntu 14.04.1 LTS (GNU/Linux 3.13.0-39-generic x86_64)

SCP も同様。超便利。

$ echo hello > hello.txt
$ scp hello.txt ubuntu:~/hello.txt
hello.txt        100%    6    0.0KB/s    00:00