相比较在vagrant中配置centos来讲,配置windows会比较麻烦而且特殊一些,下面记录一些自己的经历。
第一步:创建虚拟机
由于windows的vbox不多,能找到一个适合自己的也不太容易,倒不如自己在virtualbox中直接用iso文件安装一个,安装完成后直接: vagrant package –base the_virtualbox_vm_name –output my_windows.box
由于windows不比centos可以使用ssh登录,而是有自己的管理方式,叫做wrm,这个必须在安装windows的时候都设置好,参考资料:
- win10 vagrant box: http://huestones.co.uk/node/305
- 安装和配置windows远程管理: https://msdn.microsoft.com/en-us/library/aa384372(v=vs.85).aspx
- https://github.com/WinRb/vagrant-windows
- PowerShell启用winrm失败: http://www.jb51.net/article/68187.htm
下面是我遇到过的一些问题:
- 为了避免忘记密码,干脆没给用户设置密码;然而,wrm要求管理员用户必须设置密码,否则,无法配置wrm;就因为这个浪费了不少时间
- 网络连接类型不能是公共网络,否则wrm不允许使用
- 为了不必要的麻烦,干脆把防火墙给关掉了
重要配置:
1 2 3 4 5 6 |
winrm quickconfig -q winrm set winrm/config/winrs @{MaxMemoryPerShellMB="512"} winrm set winrm/config @{MaxTimeoutms="1800000"} winrm set winrm/config/service @{AllowUnencrypted="true"} winrm set winrm/config/service/auth @{Basic="true"} sc config WinRM start= auto |
曾经因为没有设置@{AllowUnencrypted=”true” } 和 @{Basic=”true”} 浪费了很多时间
关闭UAC: http://jingyan.baidu.com/article/09ea3ede241689c0afde3972.html
第二步: 配置vagrantfile
vagrant默认使用ssh,而windows要使用wrm,那么,总得有个方式说明一下吧?vagrant官网文档查看了一大片,没找到,最后还是在 http://huestones.co.uk/node/305 找到的,如下:
1 |
config.vm.communicator = "winrm" |
windows中很可能会出现未识别的网络或者是“公用网络”,在这种情况下,winrm是不能正常使用的,所以,保险起见,vagrantfile中添加如下设置:
1 2 |
# Ensure that all networks are set to private config.windows.set_work_network = true |
确保不会出现公用网络,曾经因为这个浪费了不少时间
附:一个windows的vagrantfile
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# -*- mode: ruby -*- # vi: set ft=ruby : # All Vagrant configuration is done below. The "2" in Vagrant.configure # configures the configuration version (we support older styles for # backwards compatibility). Please don't change it unless you know what # you're doing. Vagrant.configure(2) do |config| # The most common configuration options are documented and commented below. # For a complete reference, please see the online documentation at # https://docs.vagrantup.com. # Every Vagrant development environment requires a box. You can search for # boxes at https://atlas.hashicorp.com/search. config.vm.box = "win7" # Disable automatic box update checking. If you disable this, then # boxes will only be checked for updates when the user runs # `vagrant box outdated`. This is not recommended. # config.vm.box_check_update = false # Create a forwarded port mapping which allows access to a specific port # within the machine from a port on the host machine. In the example below, # accessing "localhost:8080" will access port 80 on the guest machine. # config.vm.network "forwarded_port", guest: 80, host: 8080 # Create a private network, which allows host-only access to the machine # using a specific IP. # config.vm.network "private_network", ip: "192.168.33.10" # Create a public network, which generally matched to bridged network. # Bridged networks make the machine appear as another physical device on # your network. # config.vm.network "public_network" config.vm.network "public_network", ip:"172.16.161.230", :bridge => "em2" # Share an additional folder to the guest VM. The first argument is # the path on the host to the actual folder. The second argument is # the path on the guest to mount the folder. And the optional third # argument is a set of non-required options. # config.vm.synced_folder "../data", "/vagrant_data" config.vm.guest = :windows config.vm.communicator = "winrm" config.windows.set_work_network = true config.winrm.username = "vagrant" config.winrm.password = "vagrant" # Provider-specific configuration so you can fine-tune various # backing providers for Vagrant. These expose provider-specific options. # Example for VirtualBox: # #config.vm.provider "virtualbox" do |vb| # # Display the VirtualBox GUI when booting the machine # vb.gui = true # # # Customize the amount of memory on the VM: # vb.memory = "4096" #end # # View the documentation for the provider you are using for more # information on available options. # Define a Vagrant Push strategy for pushing to Atlas. Other push strategies # such as FTP and Heroku are also available. See the documentation at # https://docs.vagrantup.com/v2/push/atlas.html for more information. # config.push.define "atlas" do |push| # push.app = "YOUR_ATLAS_USERNAME/YOUR_APPLICATION_NAME" # end # Enable provisioning with a shell script. Additional provisioners such as # Puppet, Chef, Ansible, Salt, and Docker are also available. Please see the # documentation for more information about their specific syntax and use. config.vm.provision "shell", inline: <<-SHELL route delete 0.0.0.0 route add 0.0.0.0 mask 0.0.0.0 172.16.161.1 SHELL config.vm.provider "virtualbox" do |v| v.customize ["modifyvm", :id, "--vrde", "on"] v.name = "phpor_win7" end end |
附:
如何给设置的ip指定掩码(默认24位)? 如下指定22位掩码:
1 |
config.vm.network "public_network", ip:"172.16.161.230", :bridge => "em2", :netmask => "255.255.252.0" |
windows的“未识别网络”是什么意思?
当本地连接设置为“自动获取IP地址”,同时网络中又没有dhcp服务器,则该本地连接会被标识为“未识别网络”;如果在所在网络提供一个dhcp服务器,就会避免此种问题的出现