2

Despite reading this

How to change Vagrant 'default' machine name?

my hostname is stuck to default

Here's my Vagrantfile:

Vagrant.configure(2) do |config|
  config.vm.box = "hashicorp/precise32"
  config.vm.hostname = "web"
  config.vm.network :private_network, ip: "10.0.0.10"
  config.vm.provider :virtualbox do |vb|
    vb.name = "vagrant-web"
  end
end

But

$ vagrant status
Current machine states:

default                   running (virtualbox)

and

$ vagrant ssh web
The machine with the name 'web' was not found configured for
this Vagrant environment.

but

$ vagrant ssh default

works fine.

I've done a vagrant halt and vagrant up. Any other suggestions?

Community
  • 1
  • 1
Snowcrash
  • 80,579
  • 89
  • 266
  • 376

1 Answers1

3

If you want to change the vagrant name (compared to the virtualbox name) you need to do the following

Vagrant.configure(2) do |config|
  config.vm.box = "hashicorp/precise32"

  config.vm.define "web" do |web|
    web.vm.hostname = "web"
    web.vm.network :private_network, ip: "10.0.0.10"
    web.vm.provider :virtualbox do |vb|
      vb.name = "vagrant-web"
    end
  end

end

This notation is mostly used in case of multi VM but can also be used in your case for a single VM if you really want to have the vagrant name different than default.

When you start the VM, it will say

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

and so if you review the status you'll see

$ vagrant status
Current machine states:

web                       running (virtualbox)

to ssh into the machine you can just to vagrant ssh as you have a single machine, (sure vagrant ssh web will also work)

One Note Of Caution as its a new vagrant machine it will really create a new machine with the new name, the default machine you have created remains so if you have stuff already there, it will not automatically be copied into the new machine. If you want to keep your existing VM you could rename the default directory from the .vagrant folder into web, it could theoritically work but I never tried myself

Frederic Henri
  • 51,761
  • 10
  • 113
  • 139
  • 1
    Just chiming in. Renaming the `.vagrant/machines/default` folder to `.vagrant/machines/your_custom_name` will work, as long as it corresponds with the name in `config.vm.define`. – user6616962 Jan 26 '18 at 01:31