agiledivider / vagrant-hostsupdater

MIT License
1.15k stars 129 forks source link

NOT MAINTAINED

USE AN ALTERNATIVE

March 3, 2021

Hi Everyone. @cgsmith here. I took maintenance over from @agiledivider (Falk Kühnel) in 2015. Myself and the company I worked for relied on Vagrant for our development machines and I relied on vagrant-hostsupdater for changing hosts files. When I found out he was no longer maintaining the project I reached out to him on Twitter and he was willing to let me maintain it and apply some much needed pull requests.

Maintaining a large project is extremely rewarding. I enjoyed doing so but have stopped using Vagrant for development. There are other plugins to help you manage your hosts file or you can feel free to fork this one into your own.

Don't worry if you don't know Ruby... I didn't. I'm a PHP developer. I figured it out though and asked the right questions to get the problem solved.

So long... may we meet elsewhere on this vast planet.

Vagrant::Hostsupdater

Gem Version Gem Gem

Gitter Twitter

This plugin adds an entry to your /etc/hosts file on the host system.

On up, resume and reload commands, it tries to add the information, if it does not already exist in your hosts file. If it needs to be added, you will be asked for an administrator password, since it uses sudo to edit the file.

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

Installation

$ vagrant plugin install vagrant-hostsupdater

Uninstall it with:

$ vagrant plugin uninstall vagrant-hostsupdater

Update the plugin with:

$ vagrant plugin update vagrant-hostsupdater

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"
config.hostsupdater.aliases = ["alias.testing.de", "alias2.somedomain.com"]

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

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.hostsupdater.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

Skipping hostupdater

To skip adding some entries to the /etc/hosts file add hostsupdater: "skip" option to network configuration:

config.vm.network "private_network", ip: "172.21.9.9", hostsupdater: "skip"

Example:

config.vm.network :private_network, ip: "192.168.50.4"
config.vm.network :private_network,
    ip: "172.21.9.9",
    netmask: "255.255.240.0",
    hostsupdater: "skip"

Keeping Host Entries After Suspend/Halt

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

config.hostsupdater.remove_on_suspend = false

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

Suppressing prompts for elevating privileges

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.

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_hostsupdater.

For Ubuntu and most Linux environments:

# Allow passwordless startup of Vagrant with vagrant-hostsupdater.
Cmnd_Alias VAGRANT_HOSTS_ADD = /bin/sh -c echo "*" >> /etc/hosts
Cmnd_Alias VAGRANT_HOSTS_REMOVE = /bin/sed -i -e /*/ d /etc/hosts
%sudo ALL=(root) NOPASSWD: VAGRANT_HOSTS_ADD, VAGRANT_HOSTS_REMOVE

For MacOS:

# Allow passwordless startup of Vagrant with vagrant-hostsupdater.
Cmnd_Alias VAGRANT_HOSTS_ADD = /bin/sh -c echo "*" >> /etc/hosts
Cmnd_Alias VAGRANT_HOSTS_REMOVE = /usr/bin/sed -i -e /*/ d /etc/hosts
%admin ALL=(root) NOPASSWD: VAGRANT_HOSTS_ADD, VAGRANT_HOSTS_REMOVE

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 

Using AWS as a Provider

If you'd like AWS as a provider using vagrant-aws or other plugin, this plugin will detect the instance public IP by the tag infomations.
For example, vagrant-aws configures a tag infomations like the following.

config.vm.provider :aws do |aws, override|
  aws.tags = {
    "Name" => "vagrant",
    ...
  }
  aws.elastic_ip = true
  ...
end

Using Google as a provider

If you'd like a Google provider using vagrant-google, this plugin will detect the public IP from the name of the instance. vagrant-google provides a default name, but you can specify your own as follows:

config.vm.provider :google do |google, override|
  google.name = "somename"
  ...
end

Installing development version

If you would like to install vagrant-hostsupdater on the development version perform the following:

git clone https://github.com/cogitatio/vagrant-hostsupdater
cd vagrant-hostsupdater
git checkout develop
gem build vagrant-hostsupdater.gemspec
vagrant plugin install vagrant-hostsupdater-*.gem

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request on the develop branch

Versions

1.2.0

1.1.1

1.1.0

1.0.2

1.0.1

1.0.0

0.0.11

0.0.10

0.0.9

0.0.8

0.0.7

0.0.6

0.0.5

0.0.4

0.0.3