首先分别去两个应用的官网去下载最新的桌面软件

Vagrant: https://www.vagrantup.com/downloads.html

VirtualBox: https://www.virtualbox.org/wiki/Downloads

安装之后使用vagrant box add命令安装系统镜像

不过以国内下载速度可能到我退休才能下好,所以我一般会从其他地方下载系统镜像

比如:https://cloud.centos.org/centos/7/vagrant/x86_64/images/

找个 .VirtualBox.box 的下载好

然后 vagrant box add centos/7 你下载镜像的路径 来添加box

可以通过 vagrant box list 可以查看你的镜像

随后在终端里进入你想创建vagrant的文件夹
输入vagrant init来新建一个Vagrantfile文件 这里给一个我的配置

if defined?(VagrantVbguest)
  class MyWorkaroundInstallerUntilPR373IsMerged < VagrantVbguest::Installers::CentOS
    protected
    
    def has_rel_repo?
      unless instance_variable_defined?(:@has_rel_repo)
        rel = release_version
        @has_rel_repo = communicate.test(centos_8? ? 'yum repolist' : "yum repolist --enablerepo=C#{rel}-base --enablerepo=C#{rel}-updates")
      end
      @has_rel_repo
    end

    def centos_8?
      release_version && release_version.to_s.start_with?('8')
    end

    def install_kernel_devel(opts=nil, &block)
      if centos_8?
        communicate.sudo('yum update -y kernel', opts, &block)
        communicate.sudo('yum install -y kernel-devel', opts, &block)
        communicate.sudo('shutdown -r now', opts, &block)

        begin
          sleep 10
        end until @vm.communicate.ready?
      else
        rel = has_rel_repo? ? release_version : '*'
        cmd = "yum install -y kernel-devel-`uname -r` --enablerepo=C#{rel}-base --enablerepo=C#{rel}-updates"
        communicate.sudo(cmd, opts, &block)
      end
    end
  end
end

Vagrant.configure("2") do |config|

  config.vagrant.plugins = ['vagrant-vbguest']
  config.vbguest.auto_update = true

  config.vm.box = "centos/7"

  if defined?(MyWorkaroundInstallerUntilPR373IsMerged)
    config.vbguest.installer = MyWorkaroundInstallerUntilPR373IsMerged
  end

  config.vm.network "forwarded_port", guest: 80, host: 80
  config.vm.network "forwarded_port", guest: 888, host: 888
  config.vm.network "forwarded_port", guest: 3306, host: 3306
  config.vm.network "forwarded_port", guest: 6001, host: 6001
  config.vm.network "forwarded_port", guest: 6379, host: 6379
  config.vm.network "forwarded_port", guest: 8888, host: 8888

  config.vm.network "private_network", ip: "192.168.56.10"

  config.vm.synced_folder "C:\\code\\php", "/www/wwwroot"

  config.vm.provider "virtualbox" do |vb|

    vb.name = "CentOS"

    vb.memory = "12288"

    vb.cpus = 4
  end

end

虽然语言是ruby,但是代码还是比较好读的。 可以看到我配置了一个插件“vagrant-vbguest”,这个插件是用来同步主宿文件夹的。

但是启动时应为这个插件更新软件包时会报错,因此我定义了一个类来替代这个插件原有的installer。 插件安装命令 vagrant plugin install vagrant-vbguest 至于其他配置看一下官方文档和原注释即可

 

再之后就是vagrant的管理命令了
vagrant up 开机

vagrant halt 关机

vagrant ssh 进入ssh

ssh的用户是 vagrant 密码也是 vagrant

可以用sudo passwd root修改root密码

su或者su -进入root用户

如果要用第三方软件连接ssh则需要修改/etc/ssh/sshd_config的PasswordAuthentication 为 yes


不知天在水,清梦压星河