Vagrant:重置客户机Vagrant虚拟机网络的更好方法?

23

虽然我对Vagrant还比较陌生,但迄今为止确实很喜欢它。

我经常遇到一个小问题,就是当我的主机网络短暂中断时,会影响我的Vagrant虚拟机连接。

当我的主机网络重新连接上后,我必须在我的主机上运行 vagrant halt 然后再运行 vagrant up 以“重置”我的虚拟机的网络,这样它才能再次连接到互联网。

有没有更加优雅的方法可以让我的虚拟机通过主机机器的网络检测到互联网连接已经恢复呢?

4个回答

26

是的,在VM内部重新启动网络子系统是最简单的解决方法。 SSH连接不应中断。 在Ubuntu上,按照以下步骤操作:

sudo /etc/init.d/networking restart

对于我使用Debian和在Vagrantfile中使用config.vm.network :public_network, bridge: 'en0: Wi-Fi (AirPort)'的情况,我需要执行以下操作:sudo ifdown eth2 && sudo ifup eth2 - zigomir

3

关于Mitchell的回答,针对我的Vagrant版本1.5.2,命令略有不同:

sudo /etc/init.d/network restart

而不是使用“networking”。我知道这没什么大不了的,但只是想说一下。

Mitchell在Vagrant方面做得很棒!


这可能更适合作为注释。 - timgeb
1
可能你正在使用的是 CentOS/Red Hat/Fedora 虚拟机,而不是 Ubuntu/Debian。 - John C

2

以下是我所做的:

主机=虚拟机IP地址 私钥路径=私钥路径

ssh vagrant@$HOST -i $PRIVATE_KEY_PATH 用于登录到虚拟机

然后,sudo /etc/init.d/networking restart


0

其他回答中列出的命令的更新版本(2018年):

适用于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

对于CentOS 7.5(networknetworking之间的区别相当不幸,但我们在这里):

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

*在现代发行版中,答案中列出的脚本只是systemdsystemctl的包装器。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接