264

Where does the name 'default' come from when launching a vagrant box?

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

Is there a way to set this?

kenorb
  • 155,785
  • 88
  • 678
  • 743
Kyle Kelley
  • 13,804
  • 8
  • 49
  • 78

7 Answers7

391

I found the multiple options confusing, so I decided to test all of them to see exactly what they do.

I'm using VirtualBox 4.2.16-r86992 and Vagrant 1.3.3.

I created a directory called nametest and ran

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

to generate a default Vagrantfile. Then I opened the VirtualBox GUI so I could see what names the boxes I create would show up as.

  1. Default Vagrantfile

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

    VirtualBox GUI Name: "nametest_default_1386347922"

    Comments: The name defaults to the format DIRECTORY_default_TIMESTAMP.

  2. Define VM

    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 Name: "nametest_foohost_1386347922"

    Comments: If you explicitly define a VM, the name used replaces the token 'default'. This is the name vagrant outputs on the console. Simplifying based on zook's (commenter) input

  3. Set Provider Name

    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 Name: "foohost"

    Comments: If you set the name attribute in a provider configuration block, that name will become the entire name displayed in the VirtualBox GUI.

    Combined Example: Define VM -and- Set Provider Name

    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 Name: "barhost"

    Comments: If you use both methods at the same time, the value assigned to name in the provider configuration block wins. Simplifying based on zook's (commenter) input

  4. Set hostname (BONUS)

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

    Comments: This sets the hostname inside the VM. This would be the output of hostname command in the VM and also this is what's visible in the prompt like vagrant@<hostname>, here it will look like vagrant@buzbar

Final Code

    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

So there it is. You now know 3 different options you can set and the effects they have. I guess it's a matter of preference at this point? (I'm new to Vagrant, so I can't speak to best practices yet.)

Dheeraj Bhaskar
  • 18,633
  • 9
  • 63
  • 66
odigity
  • 7,568
  • 4
  • 37
  • 51
  • 3
    Hey, I did a little more research on related matters. (1) The directory in the name is generated the first time and doesn't change when you rename the directory. (2) The timestamp in the name is generated the first time and doesn't change when you restart. (3) The project is mapped to a VirtualBox VM via UUID, which is stored in PROJECT_ROOT/.vagrant/machines/default/virtualbox/id. This UUID is not visible in the VirtualBox GUI (https://www.virtualbox.org/ticket/6989), but you can use `VBoxManage list vms` on the command line. – odigity Feb 18 '14 at 17:00
  • One of the useful references I used for the previous comment: http://stackoverflow.com/questions/9434313/how-do-i-associate-a-vagrant-project-directory-with-an-existing-virtualbox-vm – odigity Feb 18 '14 at 17:00
  • 8
    It's worth to note that the `define VM` method name is used in Vagrant stdout and logs, while `set provider name` name is used for managing the box with the provider. So both are relevant. – Redsandro Apr 07 '14 at 19:19
  • @odigity may I suggest that you incorporate the content of your comments into your answer? – guntbert Oct 20 '14 at 18:40
  • 31
    You don't actually need the `do... end` block if it's empty. `config.vm.define "foohost"` works just fine. – Zook Dec 29 '14 at 22:01
  • @odigity if I use method 3 or 4 how it handles the name that appears on `vagrant global-status`? – Paulo Oliveira May 05 '15 at 08:59
  • 8
    I love this answer's detail, but it doesn't really address the original question. Yes, those configurations will result in a "name" showing up in the Virtualbox GUI properly, and if you list them with vboxmanage, the same. However, this still keeps `Bringing machine 'default' up with 'virtualbox' provider...` around when you execute `vagrant up` – TomSchober Jun 03 '16 at 15:21
  • When i try to vagrant up using option **4 Define VM -and- Set Provider Name**, get this message: `formal argument cannot be a constant config.vm.define "foohost" do |foohost|` – FarIDM May 18 '17 at 11:10
  • For the `libvirt` provider the option is `default_prefix`. – Michael Franzl Jan 11 '23 at 09:52
66

This is the way I've assigned names to individual VMs. Change YOURNAMEHERE to your desired name.

Contents of 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

Terminal output:

$ vagrant status
Current machine states:

YOURNAMEHERE             not created (virtualbox)
nikhil
  • 2,471
  • 2
  • 15
  • 12
  • 9
    You can omit the block at the end of your define, so simply `config.vm.define :app_name` works too. – Vidur Jul 30 '15 at 11:11
  • 1
    if YOURNAMEHERE is a variable then the colon ":" should be removed: ````config.vm.define YOURNAMEHERE do |t| end```` – Radu Oct 12 '15 at 06:24
23

I specify the name by defining inside the VagrantFile and also specify the hostname so i enjoy seeing the name of my project while executing Linux commands independently from my device's OS. ✌️

config.vm.define "abc"
config.vm.hostname = "abc"
muratgozel
  • 2,333
  • 27
  • 31
17

If you want to change anything else instead of 'default', then just add these additional lines to your Vagrantfile:

Change the basebox name, when using "vagrant status"

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

Where "tendo" will be the name that will appear instead of default

raam86
  • 6,785
  • 2
  • 31
  • 46
Arbab Nazar
  • 22,378
  • 10
  • 76
  • 82
10

You can change vagrant default machine name by changing value of config.vm.define.

Here is the simple Vagrantfile which uses getopts and allows you to change the name dynamically:

# -*- 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

So to use different name, you can run for example:

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

Note: The --vm-name parameter needs to be specified before up command.

or:

VM_NAME=my_name vagrant up --no-provision
kenorb
  • 155,785
  • 88
  • 678
  • 743
6

Yes, for Virtualbox provider do something like this:

Vagrant.configure("2") do |config|
    # ...other options...
    config.vm.provider "virtualbox" do |p|
        p.name = "something-else"
    end
end
cmur2
  • 2,614
  • 1
  • 20
  • 23
  • 2
    After changing my Vagrantfile and fully tearing the machine down via `vagrant destroy` then bringing it back up it's still calling it default. – Kyle Kelley Jul 25 '13 at 14:34
  • 3
    I believe this changes the name in the VirtualBox GUI. "You can customize the name that appears in the VirtualBox GUI" Reference: [link](http://docs.vagrantup.com/v2/virtualbox/configuration.html) – nikhil Jul 25 '13 at 17:08
  • 2
    Ah, jepp my fault - I thought the author want's to change the VirtualBox name, the internal Vagrant name is of course only changeable in a multi-VM environment. – cmur2 Jul 25 '13 at 18:33
  • 4
    Not meaning to be too picky cmur2, but it's possible to setup an internal Vagrant name in a single machine environment by simply providing only a single config.vm.define block. – Dave Birch May 09 '14 at 13:47
4

In case there are many people using your vagrant file - you might want to set name dynamically. Below is the example how to do it using username from your HOST machine as the name of the box and hostname:

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