テスト環境
- Windows 7 Ultimate ユーザー:administrator
- VirtualBox 4.3.16 ダウンロード
- Vagrant 1.6.5 ダウンロード
- CentOS 7 : boxファイル ダウンロード
- IPアドレス 例:192.168.1.80 / ブリッジ接続
手順
1) VirtualBox インストール
2) Vagrant インストール
3) ユーザディレクトリに移動 ( C:¥Users¥Administrator¥ )
4) Vagrant用にディレクトリを作成 ( 例 C:¥Users¥Administrator¥VagrantBox¥boxes¥centos7_80_app )
5) centos7_80_appに移動してCentOS 7のBoxファイルをダウンロード ※ファイル名をcentos7.boxに設定
6) boxファイルが用意できたら以下のコマンドを実行
コマンドプロンプト
C:¥Users¥Administrator¥VagrantBox¥boxes¥centos7_80_app> vagrant box add centos7_80_app centos7.box
C:¥Users¥Administrator¥VagrantBox¥boxes¥centos7_80_app> vagrant init centos7_80_app
7) Vagrantファイルが存在しない場合initコマンド実行後、カレントディレクトリにVagrantファイルが作成される。
8) Vagrantファイルを編集
Vagrant
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
# box名
config.vm.box = "centos7_80_app"
# ダウンロード用
config.vm.box_url = "https://f0fff3908f081cb6461b407be80daf97f07ac418.googledrive.com/host/0BwtuV7VyVTSkUG1PM3pCeDJ4dVE/centos7.box"
# ブリッジ接続
config.vm.network "public_network", ip: "192.168.1.80"
config.ssh.forward_agent = true
# 起動時にシェルスクリプトを実行する場合コメントアウト
#config.vm.provision :shell, :path => "provision.sh"
config.vm.provider "virtualbox" do |vb|
vb.customize ["modifyvm", :id, "--memory", "1028", "--cpus", "1"]
end
9) 仮想環境の起動とssh接続
コマンドプロンプト
C:¥Users¥Administrator¥VagrantBox¥boxes¥centos7_80_app> vagrant up
C:¥Users¥Administrator¥VagrantBox¥boxes¥centos7_80_app> vagrant ssh
[vagrant@localhost ~]$ _
10) vagrant ssh
を実行するとvagrantユーザでログインされる。※ vagrantユーザとrootのパスワードは"vagrant"
ssh秘密鍵はC:¥Users¥Administrator¥.vagrant.d¥insecure_private_keyに作成される(puttyから接続する場合はPuttygenでこの鍵を変換)
vagrant up
で立ち上げた直後はputty等でSSHからはログインできないのでとりあえず以下の設定をしておく
- firefalldの停止
- SELinuxの無効化
- 公開鍵の設定
- DNSの設定
コマンドプロンプト
[vagrant@localhost ~]$ su -
Password: vagrant
[root@localhost ~]♯ systemctl stop firewalld
[root@localhost ~]♯ systemctl disable firewalld
[root@localhost ~]♯ sed -i -e s/SELINUX=enforcing/SELINUX=disabled/ /etc/selinux/config
[root@localhost ~]♯ setenforce 0
[root@localhost ~]♯ cp -pR /home/vagrant/.ssh ~/
[root@localhost ~]♯ chown -R root:root ~/.ssh
[root@localhost ~]♯ echo "DNS1=192.168.1.1" >> /etc/sysconfig/network-scripts/ifcfg-enp0s8
[root@localhost ~]♯ echo "GATEWAY=192.168.1.1" >> /etc/sysconfig/network-scripts/ifcfg-enp0s8
[root@localhost ~]♯ systemctl restart network
これでputty等から秘密鍵を使って192.168.1.80に接続できるかと思います。
メモ
VirtualBox
- C:¥Users¥Administrator¥.vagrant.d¥
- C:¥Users¥Administrator¥.VirtualBox¥
- C:¥Users¥Administrator¥VirtualBox VMs¥
- C:¥Users¥Administrator¥VagrantBox¥
例:コマンドライン
[root@localhost ~]♯ vagrant box add centos7_80 centos7.box
[root@localhost ~]♯ vagrant init
[root@localhost ~]♯ vagrant up
[root@localhost ~]♯ vagrant ssh
[root@localhost ~]♯ vagrant destroy