The project is in a healthy, maintained state
Enables Vagrant to update hosts file on the host machine with goodhosts
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

>= 2.2.10
>= 0

Runtime

>= 0
 Project Readme

Vagrant::Goodhosts

This vagrant plugin adds host file entries to the host pointing to the guest VM, using the GoodHosts cli tool. This plugin is based on vagrant-hostsupdater and aims to be compatible with the same config parameters.

On up, resume and reload commands, it tries to add the hosts if they do not already exist in your hosts file. If it needs to be added, you will be asked for the sudo password to make the necessary edits.

On halt, destroy, and suspend, those entries will be removed again. By setting the config.goodhosts.remove_on_suspend = false, suspend and halt will not remove them.

Installation

vagrant plugin install vagrant-goodhosts

To uninstall :

vagrant plugin uninstall vagrant-goodhosts

To update the plugin:

vagrant plugin update vagrant-goodhosts

Installation from Gem file

The gem file is released with every release and you can install it with vagrant as it is a plugin after all.
Affter downloading the file run this command to install manually:

vagrant plugin install vagrant-goodhosts-*.gem

Usage

You currently only need the hostname and a :private_network network with a fixed IP address.

config.vm.network :private_network, ip: "192.168.3.10"
config.vm.hostname = "www.testing.de" # This is not used by the plugin
config.goodhosts.aliases = ["alias.testing.de", "alias2.somedomain.com"]

This IP address and the hostname will be used for the entry in the /etc/hosts file.

vagrant-hostsupdater conflicts

The original plugin has the issue of be executed in any vagrant environment, also that is not using and will add a rule in the hosts file that is not generated by this one. To avoid issues is better to remove that plugin and in case update accordingly the Vagrantfile if use it. Require just a change of the reference of the plugin name as the example above.

Multiple private network adapters

If you have multiple network adapters i.e.:

config.vm.network :private_network, ip: "10.0.0.1"
config.vm.network :private_network, ip: "10.0.0.2"

You can specify which hostnames are bound to which IP by passing a hash mapping the IP of the network to an array of hostnames to create, e.g.:

config.goodhosts.aliases = {
    '10.0.0.1' => ['foo.com', 'bar.com'],
    '10.0.0.2' => ['baz.com', 'bat.com']
}

This will produce /etc/hosts entries like so:

10.0.0.1 foo.com
10.0.0.1 bar.com
10.0.0.2 baz.com
10.0.0.2 bat.com

Keeping Host Entries After Suspend/Halt

To keep your /etc/hosts file unchanged simply add the line below to your VagrantFile:

config.goodhosts.remove_on_suspend = false

This disables vagrant-goodhosts from running on suspend and halt.

Disable file hosts clean

If you want /etc/hosts file cleaned add in your VagrantFile:

config.goodhosts.disable_clean = false

This enable vagrant-goodhosts from running the clean command in every call.

Suppressing prompts for elevating privileges

These prompts exist to prevent anything that is being run by the user from inadvertently updating the hosts file.
The command path is printed in red when there are errors with Vagrant eg. if the sudo password is entered incorrectly for 3 times.
If you understand the risks that go with supressing them, here's how to do it.

Linux/OS X: Passwordless sudo

To allow vagrant to automatically update the hosts file without asking for a sudo password, add one of the following snippets to a new sudoers file include, i.e. sudo visudo -f /etc/sudoers.d/vagrant_goodhosts. The command path is printed when there are errors with Vagrant, check the output marked in red.

For Ubuntu and most Linux environments:

%sudo ALL=(root) NOPASSWD: [the-command-path]

An example complete:

%sudo ALL=(root) NOPASSWD: /home/user/sites/vvv/.vagrant/plugins/gems/3.1.2/gems/vagrant-goodhosts-1.1.6/lib/vagrant-goodhosts/bundle/cli_amd64_linux

For MacOS:

%admin ALL=(root) NOPASSWD: [the-command-path]

Replace in both %sudo/%admin with the username it if it is not working for you.

Windows: UAC Prompt

You can use cacls or icacls to grant your user account permanent write permission to the system's hosts file. You have to open an elevated command prompt; hold ❖ Win and press X, then choose "Command Prompt (Admin)"

cacls %SYSTEMROOT%\system32\drivers\etc\hosts /E /G %USERNAME%:W

Generate The Development Version

If you would like to install vagrant-goodhosts to make contributions or changes, run the following commands::

git clone https://github.com/goodhosts/vagrant vagrant-goodhosts
cd vagrant-goodhosts
./package.sh
vagrant plugin install vagrant-goodhosts-*.gem

Test the plugin

You need to run a Vagrant machine with the minimum settings specified in the Usage section, it is enough a turn on and off and in the meantime if the hosts file in your machine is written in the right way.