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.
vagrant plugin install vagrant-goodhosts
To uninstall :
vagrant plugin uninstall vagrant-goodhosts
To update the plugin:
vagrant plugin update vagrant-goodhosts
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.
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.
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
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.
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.
These prompts exist to prevent anything that is being run by the user from inadvertently updating the hosts file. If you understand the risks that go with supressing them, here's how to do it.
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 sudo.
For Ubuntu and most Linux environments:
%sudo ALL=(root) NOPASSWD: [the-path]
For MacOS:
%admin ALL=(root) NOPASSWD: [the-path]
Replace in both %sudo/%admin with the username it if it is not working for you.
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
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