vagrant: a better to way to “reset” my guest vagra

2019-03-12 03:46发布

问题:

I am relatively new to vagrant but certainly liking it so far.

One of the little problem I often face is that when my host machine's network goes down momentarily, it affects my connection to my vagrant guest vm.

When my host machine's network comes back up again, I have to - on my host machine - run vagrant halt and then vagrant up in order to "reset" my guest vagrant vm's network so that it can connect to the internet once again.

Is there a more "elegant" way of getting my vm's internet connection via my host machine's network to detect that the internet network connection is back up?

回答1:

Yes, the easiest solution is to restart the networking subsystem within the VM itself. SSH connections shouldn't be interrupted. On Ubuntu, do the following:

sudo /etc/init.d/networking restart


回答2:

Regarding what Mitchell answers, for my Vagrant version 1.5.2 the command is little different:

sudo /etc/init.d/network restart

Instead of using "networking". I know is not a big deal, but just wanted to say it.

Great work with Vagrant Mitchell!



回答3:

Here's what i did:

HOST=VM IP Address PRIVATE_KEY_PATH= Private key path

ssh vagrant@$HOST -i $PRIVATE_KEY_PATH to login to the VM

Then, sudo /etc/init.d/networking restart



回答4:

Updated version (year 2018) of the command listed in other answers*:

For Ubuntu 16.04.* LTS (Xenial Xerus):

sudo systemctl restart networking

# Check the status - there is no better way with systemd for now:
# https://github.com/systemd/systemd/issues/1287
sudo systemctl status networking

For CentOS 7.5 (network vs networking is quite unfortunate difference, but here we are):

sudo systemctl restart network

# Check the status - there is no better way with systemd for now:
# https://github.com/systemd/systemd/issues/1287
sudo systemctl status network

*In modern distributions scripts listed in the answers are just wrappers around systemd's systemctl.