如何更改Vagrant的“default”虚拟机名称?

264

在启动vagrant box时,“default”这个名称从何而来?

$ vagrant up
Bringing machine 'default' up with 'virtualbox' provider...

有没有一种方法来设置这个?


不要忘记检查虚拟化是否已激活,在 BIOS 中启用 VT-x。那就是我的问题。 - Renato Tavares
7个回答

391

我发现有多个选项令人困惑,因此我决定测试所有选项,以便准确了解它们的作用。

我正在使用VirtualBox 4.2.16-r86992和Vagrant 1.3.3。

我创建了一个名为nametest的目录,并运行了

vagrant init precise64 http://files.vagrantup.com/precise64.box

生成默认的Vagrantfile。然后我打开VirtualBox GUI,以便查看我创建的虚拟机名称。

  1. 默认的Vagrantfile

Vagrant.configure('2') do |config|
    config.vm.box = "precise64"
    config.vm.box_url = "http://files.vagrantup.com/precise64.box"
end

VirtualBox GUI 名称:"nametest_default_1386347922"

注释:名称默认为DIRECTORY_default_TIMESTAMP格式。

  • 定义虚拟机

  • Vagrant.configure('2') do |config|
        config.vm.box = "precise64"
        config.vm.box_url = "http://files.vagrantup.com/precise64.box"
        config.vm.define "foohost"
    end
    

    VirtualBox GUI名称: "nametest_foohost_1386347922"

    评论: 如果您明确定义了虚拟机, 使用的名称将替换标记“default”。这是vagrant在控制台上输出的名称。根据zook的输入进行简化。

  • 设置提供程序名称

  • Vagrant.configure('2') do |config|
        config.vm.box = "precise64"
        config.vm.box_url = "http://files.vagrantup.com/precise64.box"
        config.vm.provider :virtualbox do |vb|
            vb.name = "foohost"
        end
    end
    

    VirtualBox GUI名称:“foohost”

    注释:如果在提供程序配置块中设置name属性,则该名称将成为在VirtualBox GUI中显示的完整名称。

    组合示例:定义虚拟机和设置提供程序名称

    Vagrant.configure('2') do |config|
        config.vm.box = "precise64"
        config.vm.box_url = "http://files.vagrantup.com/precise64.box"
        config.vm.define "foohost"
        config.vm.provider :virtualbox do |vb|
            vb.name = "barhost"
        end
    end
    

    VirtualBox GUI名称:"barhost"

    评论:如果同时使用两种方法,则在提供者配置块中分配给name的值将获胜。根据评论者zook的意见进行简化

  • 设置hostname(奖励)

  • Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
      config.vm.hostname = "buzbar"
    end
    

    注释: 这将设置VM内部的主机名。这将是VM中hostname命令的输出,也是在提示符中可见的内容,例如vagrant@<hostname>,这里它会显示为vagrant@buzbar

    最终代码

        Vagrant.configure('2') do |config|
            config.vm.box = "precise64"
            config.vm.box_url = "http://files.vagrantup.com/precise64.box"
            config.vm.hostname = "buzbar"
            config.vm.define "foohost"
            config.vm.provider :virtualbox do |vb|
                vb.name = "barhost"
            end
        end
    
    所以这就是它。你现在知道了3个不同的选项和它们的影响。我想现在只是一个偏好的问题?(我对Vagrant还很新,所以我还不能谈论最佳实践。)

    3
    嘿,我对相关事项进行了更多的研究。(1)在命名中的目录名称是第一次生成的,重命名后不会改变。(2)在命名中的时间戳是第一次生成的,在重新启动后不会改变。(3)该项目通过UUID映射到VirtualBox虚拟机,UUID存储在PROJECT_ROOT/.vagrant/machines/default/virtualbox/id中。这个UUID在VirtualBox GUI中不可见(https://www.virtualbox.org/ticket/6989),但是你可以在命令行上使用 VBoxManage list vms - odigity
    我在之前的评论中使用的一个有用的参考链接是:https://dev59.com/GGox5IYBdhLWcg3wGwpo - odigity
    8
    值得注意的是,"define VM" 方法名称用于 Vagrant 的标准输出和日志,而 "set provider name" 方法名称用于使用提供程序管理虚拟机镜像。因此,两者都是相关的。 - Redsandro
    31
    如果 do...end 块是空的,实际上不需要它。 config.vm.define "foohost" 就可以正常工作。 - Zook
    8
    我喜欢这个答案的详细说明,但它并没有真正回答原来的问题。是的,这些配置将导致"名称"在Virtualbox GUI中正确显示,并且如果您使用vboxmanage列出它们,也会显示相同结果。然而,当你执行"vagrant up"时,仍会保留"使用'virtualbox'提供程序启动'default'虚拟机..."的提示。 - TomSchober
    显示剩余4条评论

    67

    以下是我为各个虚拟机分配名称的方法。将YOURNAMEHERE更改为您想要的名称。

    Vagrantfile的内容:

    Vagrant.configure("2") do |config|
    
      # Every Vagrant virtual environment requires a box to build off of.
      config.vm.box = "precise32"
    
      # The url from where the 'config.vm.box' box will be fetched if it
      # doesn't already exist on the user's system.
      config.vm.box_url = "http://files.vagrantup.com/precise32.box"
    
      config.vm.define :YOURNAMEHERE do |t|
      end
    
    end
    

    终端输出:
    $ vagrant status
    Current machine states:
    
    YOURNAMEHERE             not created (virtualbox)
    

    9
    你可以忽略定义结尾处的块,所以只需要写 config.vm.define :app_name 就可以了。 - Vidur
    1
    如果YOURNAMEHERE是一个变量,那么冒号“:”应该被移除:config.vm.define YOURNAMEHERE do |t| end - Radu

    23

    我通过在 VagrantFile 中定义来指定名称,同时指定了主机名,这样我就可以在执行Linux命令时独立于设备的操作系统,并享受看到项目名称的乐趣。✌️

    config.vm.define "abc"
    config.vm.hostname = "abc"
    

    简单明了,直截了当。 - Marinos An

    17

    如果你想要改变除了“default”之外的任何东西,那么只需将这些额外的行添加到你的Vagrantfile中:

    更改基础镜像名称,在使用“vagrant status”时

     config.vm.define "tendo" do |tendo|
      end
    

    "tendo"将代替默认名称显示


    1
    我认为你的答案更加精细,很明显你所设置的是一个字符串,而不像其他答案使用了 :OBJECT_VALUE - Andre Leon Rangel
    除了 do ... end 部分似乎完全不需要之外,@AndresLeonRangel。 - 0xC0000022L

    10

    您可以通过修改config.vm.define的值来更改vagrant默认虚拟机名称。

    以下是一个简单的Vagrantfile,它使用getopts并允许您动态更改名称:

    # -*- mode: ruby -*-
    require 'getoptlong'
    
    opts = GetoptLong.new(
      [ '--vm-name',        GetoptLong::OPTIONAL_ARGUMENT ],
    )
    vm_name        = ENV['VM_NAME'] || 'default'
    
    begin
      opts.each do |opt, arg|
        case opt
          when '--vm-name'
            vm_name = arg
        end
      end
      rescue
    end
    
    Vagrant.configure(2) do |config|
      config.vm.define vm_name
      config.vm.provider "virtualbox" do |vbox, override|
        override.vm.box = "ubuntu/wily64"
        # ...
      end
      # ...
    end
    

    所以如果想要使用不同的名称,您可以运行以下命令:

    vagrant --vm-name=my_name up --no-provision
    

    注意:在执行up命令之前需要指定--vm-name参数。

    或者:

    VM_NAME=my_name vagrant up --no-provision
    

    6

    是的,对于Virtualbox提供程序,可以像这样操作:

    Vagrant.configure("2") do |config|
        # ...other options...
        config.vm.provider "virtualbox" do |p|
            p.name = "something-else"
        end
    end
    

    2
    更改了我的Vagrantfile并通过vagrant destroy完全拆除了虚拟机,然后重新启动它,但它仍然称之为默认值。 - Kyle Kelley
    3
    我相信这将更改VirtualBox GUI中的名称。“您可以自定义在VirtualBox GUI中显示的名称。” 参考文献:链接 - nikhil
    2
    啊,我的错 - 我以为作者想要更改VirtualBox的名称,而内部Vagrant名称当然只能在多VM环境中更改。 - cmur2
    4
    不是要太挑剔,cmur2,但在单机环境中只需提供一个config.vm.define块即可设置内部Vagrant名称。 - Dave Birch

    4

    如果有很多人使用您的vagrant文件,您可能希望动态设置名称。以下是一个示例,说明如何使用来自主机机器的用户名作为盒子和主机名的名称:

    require 'etc'
    vagrant_name = "yourProjectName-" + Etc.getlogin
    Vagrant.configure("2") do |config|
      config.vm.box = "ubuntu/xenial64"
      config.vm.hostname = vagrant_name
      config.vm.provider "virtualbox" do |v|
        v.name = vagrant_name
      end
    end
    

    网页内容由stack overflow 提供, 点击上面的
    可以查看英文原文,
    原文链接