Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

vagrant up/reload just keeps adding new entries to /etc/hosts #12

Closed
ottok opened this issue May 26, 2015 · 15 comments
Closed

vagrant up/reload just keeps adding new entries to /etc/hosts #12

ottok opened this issue May 26, 2015 · 15 comments
Labels

Comments

@ottok
Copy link
Contributor

ottok commented May 26, 2015

Hello!

Every time I run 'vagrant up' or 'vagrant reload' or similar commands, the IP address of the Vagrant box changes (in fact a new Virtualbox network interface is created every time). My /etc/hosts gets populated with ever new lines. As only the first line matches, it means that in effect my browser never finds the Vagrant server unless I manually keep cleaning up the /etc/hosts file over and over again.

This is extremely annoying an probably impossible to debug to those who don't know where to look.

@anttiviljami
Copy link
Contributor

Are you running vagrant destroy to automatically clean up your hostsfile? Works fine for me on both OS X and cygwin.

If you don't destroy your vagrant boxes properly, the auto-cleanup won't happen and you will leave entries in your hostsfile. In that case, just manually delete all the vagrant hostsfile entries and run vagrant up again.

@ottok ottok reopened this May 27, 2015
@ottok
Copy link
Contributor Author

ottok commented May 27, 2015

Keep open until resolved and 'vagrant reload' and 'vagrant suspend + vagrant up' works as expected in Vagrant normally

@ottok
Copy link
Contributor Author

ottok commented May 27, 2015

´´´
$ grep dev /etc/hosts
192.168.205.242 wordpress-wp-dev # VAGRANT: 8bbfed5e31d34876e5fe883137c521bf (default) / 1c3d76ce-4db7-4960-852c-7876ef0bb75f
192.168.205.242 wordpress.dev # VAGRANT: 8bbfed5e31d34876e5fe883137c521bf (default) / 1c3d76ce-4db7-4960-852c-7876ef0bb75f
192.168.205.242 www.wordpress.dev # VAGRANT: 8bbfed5e31d34876e5fe883137c521bf (default) / 1c3d76ce-4db7-4960-852c-7876ef0bb75f
192.168.205.242 webgrind.wordpress.dev # VAGRANT: 8bbfed5e31d34876e5fe883137c521bf (default) / 1c3d76ce-4db7-4960-852c-7876ef0bb75f
192.168.205.242 adminer.wordpress.dev # VAGRANT: 8bbfed5e31d34876e5fe883137c521bf (default) / 1c3d76ce-4db7-4960-852c-7876ef0bb75f
192.168.205.242 mailcatcher.wordpress.dev # VAGRANT: 8bbfed5e31d34876e5fe883137c521bf (default) / 1c3d76ce-4db7-4960-852c-7876ef0bb75f
192.168.205.242 browsersync.wordpress.dev # VAGRANT: 8bbfed5e31d34876e5fe883137c521bf (default) / 1c3d76ce-4db7-4960-852c-7876ef0bb75f
192.168.205.242 info.wordpress.dev # VAGRANT: 8bbfed5e31d34876e5fe883137c521bf (default) / 1c3d76ce-4db7-4960-852c-7876ef0bb75f
192.168.205.242 wordpress.seravo.dev # VAGRANT: 8bbfed5e31d34876e5fe883137c521bf (default) / 1c3d76ce-4db7-4960-852c-7876ef0bb75f

$ vagrant status
Current machine states:

default saved (virtualbox)

To resume this VM, simply run vagrant up.

$ vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Checking if box 'seravo/wordpress' is up to date...
==> default: Resuming suspended VM...
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
default: SSH address: 127.0.0.1:2222
default: SSH username: vagrant
default: SSH auth method: private key
==> default: Machine booted and ready!
==> default: Checking for host entries
==> default: adding to (/etc/hosts) : 192.168.193.24 wordpress-wp-dev # VAGRANT: 8bbfed5e31d34876e5fe883137c521bf (default) / 1c3d76ce-4db7-4960-852c-7876ef0bb75f
==> default: adding to (/etc/hosts) : 192.168.193.24 wordpress.dev # VAGRANT: 8bbfed5e31d34876e5fe883137c521bf (default) / 1c3d76ce-4db7-4960-852c-7876ef0bb75f
==> default: adding to (/etc/hosts) : 192.168.193.24 www.wordpress.dev # VAGRANT: 8bbfed5e31d34876e5fe883137c521bf (default) / 1c3d76ce-4db7-4960-852c-7876ef0bb75f
==> default: adding to (/etc/hosts) : 192.168.193.24 webgrind.wordpress.dev # VAGRANT: 8bbfed5e31d34876e5fe883137c521bf (default) / 1c3d76ce-4db7-4960-852c-7876ef0bb75f
==> default: adding to (/etc/hosts) : 192.168.193.24 adminer.wordpress.dev # VAGRANT: 8bbfed5e31d34876e5fe883137c521bf (default) / 1c3d76ce-4db7-4960-852c-7876ef0bb75f
==> default: adding to (/etc/hosts) : 192.168.193.24 mailcatcher.wordpress.dev # VAGRANT: 8bbfed5e31d34876e5fe883137c521bf (default) / 1c3d76ce-4db7-4960-852c-7876ef0bb75f
==> default: adding to (/etc/hosts) : 192.168.193.24 browsersync.wordpress.dev # VAGRANT: 8bbfed5e31d34876e5fe883137c521bf (default) / 1c3d76ce-4db7-4960-852c-7876ef0bb75f
==> default: adding to (/etc/hosts) : 192.168.193.24 info.wordpress.dev # VAGRANT: 8bbfed5e31d34876e5fe883137c521bf (default) / 1c3d76ce-4db7-4960-852c-7876ef0bb75f
==> default: adding to (/etc/hosts) : 192.168.193.24 wordpress.seravo.dev # VAGRANT: 8bbfed5e31d34876e5fe883137c521bf (default) / 1c3d76ce-4db7-4960-852c-7876ef0bb75f
[sudo] password for otto:
==> default: Running triggers after up...
checking for composer... yes
Warning: This development build of composer is over 30 days old. It is recommended to update it by running "/usr/lib/composer/composer.phar self-update" to get the latest version.
Loading composer repositories with package information
Installing dependencies (including require-dev) from lock file

  • Installing wpackagist-plugin/polylang (1.7.5)
    Loading from cache
  • Installing wpackagist-plugin/posts-to-posts (1.6.3)
    Loading from cache

Generating autoload files
==> default: Executing remote command "wp-vagrant-import-db"...
==> default: Success: Database was imported correctly!
==> default: Remote command execution finished.
==> default: Executing remote command "sudo service nginx restart"...
==> default: * Restarting nginx nginx
==> default: ...done.
==> default: Remote command execution finished.
==> trigger: Visit your site: http://wordpress.dev

$ grep dev /etc/hosts
192.168.205.242 wordpress-wp-dev # VAGRANT: 8bbfed5e31d34876e5fe883137c521bf (default) / 1c3d76ce-4db7-4960-852c-7876ef0bb75f
192.168.205.242 wordpress.dev # VAGRANT: 8bbfed5e31d34876e5fe883137c521bf (default) / 1c3d76ce-4db7-4960-852c-7876ef0bb75f
192.168.205.242 www.wordpress.dev # VAGRANT: 8bbfed5e31d34876e5fe883137c521bf (default) / 1c3d76ce-4db7-4960-852c-7876ef0bb75f
192.168.205.242 webgrind.wordpress.dev # VAGRANT: 8bbfed5e31d34876e5fe883137c521bf (default) / 1c3d76ce-4db7-4960-852c-7876ef0bb75f
192.168.205.242 adminer.wordpress.dev # VAGRANT: 8bbfed5e31d34876e5fe883137c521bf (default) / 1c3d76ce-4db7-4960-852c-7876ef0bb75f
192.168.205.242 mailcatcher.wordpress.dev # VAGRANT: 8bbfed5e31d34876e5fe883137c521bf (default) / 1c3d76ce-4db7-4960-852c-7876ef0bb75f
192.168.205.242 browsersync.wordpress.dev # VAGRANT: 8bbfed5e31d34876e5fe883137c521bf (default) / 1c3d76ce-4db7-4960-852c-7876ef0bb75f
192.168.205.242 info.wordpress.dev # VAGRANT: 8bbfed5e31d34876e5fe883137c521bf (default) / 1c3d76ce-4db7-4960-852c-7876ef0bb75f
192.168.205.242 wordpress.seravo.dev # VAGRANT: 8bbfed5e31d34876e5fe883137c521bf (default) / 1c3d76ce-4db7-4960-852c-7876ef0bb75f
192.168.193.24 wordpress-wp-dev # VAGRANT: 8bbfed5e31d34876e5fe883137c521bf (default) / 1c3d76ce-4db7-4960-852c-7876ef0bb75f
192.168.193.24 wordpress.dev # VAGRANT: 8bbfed5e31d34876e5fe883137c521bf (default) / 1c3d76ce-4db7-4960-852c-7876ef0bb75f
192.168.193.24 www.wordpress.dev # VAGRANT: 8bbfed5e31d34876e5fe883137c521bf (default) / 1c3d76ce-4db7-4960-852c-7876ef0bb75f
192.168.193.24 webgrind.wordpress.dev # VAGRANT: 8bbfed5e31d34876e5fe883137c521bf (default) / 1c3d76ce-4db7-4960-852c-7876ef0bb75f
192.168.193.24 adminer.wordpress.dev # VAGRANT: 8bbfed5e31d34876e5fe883137c521bf (default) / 1c3d76ce-4db7-4960-852c-7876ef0bb75f
192.168.193.24 mailcatcher.wordpress.dev # VAGRANT: 8bbfed5e31d34876e5fe883137c521bf (default) / 1c3d76ce-4db7-4960-852c-7876ef0bb75f
192.168.193.24 browsersync.wordpress.dev # VAGRANT: 8bbfed5e31d34876e5fe883137c521bf (default) / 1c3d76ce-4db7-4960-852c-7876ef0bb75f
192.168.193.24 info.wordpress.dev # VAGRANT: 8bbfed5e31d34876e5fe883137c521bf (default) / 1c3d76ce-4db7-4960-852c-7876ef0bb75f
192.168.193.24 wordpress.seravo.dev # VAGRANT: 8bbfed5e31d34876e5fe883137c521bf (default) / 1c3d76ce-4db7-4960-852c-7876ef0bb75f
´´´

The problem is with the Vagrantfile that always adds a new virtual network interface without removing the old one. Running 'vagrant destroy' in between will clean up the hosts file but still leaves the virtualbox network interface behind.

Please check how this is solved in other Vagrantfiles so that creating and deleting the virtual network interface is reliable.

@onnimonni
Copy link
Contributor

@ottok Which OS are you using?

@onnimonni
Copy link
Contributor

We could change the Vagrantfile to give a static ip address for example in the config.yml so even though /etc/hosts would have multiple entries they would be the same entries.

I didn't want to hardcode the IP-address in case people are using multiple boxes at the same time.
See Vagrantfile:

  # Use random ip address for box
  # This is needed for updating the /etc/hosts file
  config.vm.network :private_network, ip: "192.168.#{rand(255)}.#{rand(255)}"

https://github.com/Seravo/wordpress/blob/master/Vagrantfile#L36-L38

@ottok
Copy link
Contributor Author

ottok commented May 28, 2015

Vagrant 1.7.2 on Ubuntu 12.04.5 LTS

@ottok
Copy link
Contributor Author

ottok commented May 28, 2015

Is it possible to write in the Vagrantfile the host line adding so that it is done only when a new box is bootstrapped and not otherwise? Or that before creating new virtual network interfaces the previous ones (and /etc/hosts entries) would be deleted? Is it possible to trigger the same cleanup code which is run in 'vagrant destroy' to avoid getting doulbe /etc/hosts entries?

Or can you force in the Vagrantfile that 'vagrant destroy' is always run before the rest of the Vagrantfile executes? As I understood this is what you are now doing, always running "vagrant destroy && vagrant up" so maybe the easiest (but a bit stupid) solution is to embed "vagrant destroy" inside the "vagrant up" (=Vagranfile stanzas) command

@anttiviljami
Copy link
Contributor

really what it should do, is during any vagrant up/halt/destroy/reload invocation, it should check whether the vagrant boxes in the hostsfile are actually running and remove / add entries accordingly.

I guess we need to fix this upstream https://github.com/cogitatio/vagrant-hostsupdater

@onnimonni
Copy link
Contributor

It's possible but this is a upstream error rather than in our config: agiledivider/vagrant-hostsupdater#67

Can you try following:

  1. Remove all vagrant related entries
  2. Change the ip address to static ip in Vagrantfile
  3. add this into your Vagrantfile:
    config.hostsupdater.remove_on_suspend = true
  4. run vagrant up && vagrant destroy -f && vagrant up to generate new lines

@anttiviljami
Copy link
Contributor

Seems to be done in this fork: weynhamz/vagrant-hostsupdater@69df40a

@onnimonni
Copy link
Contributor

@ottok @anttiviljami this fork seems to do it. I'm just not sure how to use that and I don't have linux machine where to try this out

@ottok
Copy link
Contributor Author

ottok commented May 28, 2015

Have you ever noticed that upstream is dead, last updated in 2013 and has since 12 open PRs? https://github.com/cogitatio/vagrant-hostsupdater/pulls

After browsing the 53 forks at https://github.com/cogitatio/vagrant-hostsupdater/network I found this which looks like it could be the solution weynhamz/vagrant-hostsupdater@69df40a

@cgsmith
Copy link

cgsmith commented Oct 28, 2015

@ottok working to remedy the upstream issue! I'll keep this issue updated if I hear something.

@cgsmith
Copy link

cgsmith commented Nov 3, 2015

@ottok I pulled this in and will be testing it shortly.

@ottok
Copy link
Contributor Author

ottok commented Mar 11, 2017

This has been fixed for a long while already.

@ottok ottok closed this as completed Mar 11, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants