Vagrant安装CentOS等

vagrant官网写的蛮清楚了,这里再总结下,相比homestead的安装感觉简单了许多。

1. 新建个目录

比如装CentOS就叫CentOS7。

2. 初始化

初次安装的话,进这个CentOS目录进入CMD输入一下命令

vagrant init centos/7

这个centos/7是从官方的镜像目录得来的。由于网络关系,命令行里面出现盒子下载地址后,复制下来,停掉CMD命令, 用迅雷等自己下载下来然后放到比如D盘,比如命名为CENTOS_7_1902.01.box。

这个vagrant init命令主要是为了在目录里面生成一个Vagrantfile.Vagrantfile的配置文件,后面的一切运行都要在里面配置的。

如果以前已经下载好了 CENTOS_7_1902.01.box 这个文件,只要运行vagrant init就行。

3. 配置

打开 Vagrantfile.Vagrantfile,做如下配置

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://vagrantcloud.com/search.
  
  config.vm.box = "centos/7"
  config.vm.box_url = "file:///d:CENTOS_7_1902.01.box"
  
  # 这里配置同步目录,参考https://www.vagrantup.com/docs/synced-folders/basic_usage.html
  config.vm.synced_folder "website/", "/home/website"


  # 下面给自己虚拟机取名,参考https://www.vagrantup.com/docs/virtualbox/configuration.html
  config.vm.provider "virtualbox" do |v|
	  v.name = "centos-7"
	end

说明下:

  • v.name = “centos-7″这句,是给自己的VirtualBox里面的虚拟机取名,这样好看点,不然默认后面有一长串的数字。
  • config.vm.synced_folder “website”, “/home/website”这句是给自己设置同步目录,第一个参数是本机根目录(CentOS7 )下的目录,第二个参数是虚拟机上的目录,没的话会自动递归创建。这个默认的是本机根目录”/”对应虚拟机上的”/vagrant”。
  • 注意,虚拟机vagrant ssh进去后默认的目录是/home/vagrant。而默认的同步文件夹是/vagrant。不一样的地方。

4. 启用

最后在CentOS 的根目录里面启动CMD输入vagrant up就OK了。

5.一些问题

  • 按上面的方法最终会出现下面的错误提示:
==> default: Mounting shared folders...
    default: /home/website => C:/Users/Administrator/CentOS7/website
Vagrant was unable to mount VirtualBox shared folders. This is usually
because the filesystem "vboxsf" is not available. This filesystem is
made available via the VirtualBox Guest Additions and kernel module.
Please verify that these guest additions are properly installed in the
guest. This is not a bug in Vagrant and is usually caused by a faulty
Vagrant box. For context, the command attempted was:

mount -t vboxsf -o uid=1000,gid=1000 home_website /home/website

The error output from the command was:

mount: unknown filesystem type 'vboxsf'

解决方法参照: https://zj-john.github.io/tips/cjhpvs7vl000r64f01suposfq.html 这写的,输入以下命令:

vagrant plugin install vagrant-vbguest
vagrant vbguest

然后vagrant reload就解决了。

  • 要SSH进vagrant建立的系统建议还是用自带的vagrant ssh。貌似只有这样他的同步文件夹功能才起作用。直接在virtual box进去操作不起作用。而且其余的用putty,secureCRT等连接实在是太烦太烦了。