我正在尝试使用 ssh 连接两个 ubuntu vagrant 盒子。我的目标是使用 Python Fabric 库在本地机器上模拟 vm-box1 和 vm-box2 之间的通信,为此我必须使用静态 ip 地址创建私有网络。vm-box1 是我想要使用 Fabric 库访问 vm-box2 的主盒子。为了使我的 Fabric 连接正常工作,我需要设置一个可以使用 ssh 的网络。我尝试了以下方法在私有网络中设置静态 ip 地址。
我在 box2 Vagrantfile 中添加了以下行
config.vm.network "private_network", :ip => "192.168.1.111", :netmask => "255.255.255.0"
然后 vagrant up 了。成功启动机器后,稍后执行 ifconfig 可以看到 IPv4 已分配静态 IP 地址:
enp0s3: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 10.0.2.15 netmask 255.255.255.0 broadcast 10.0.2.255
inet6 fe80::45:2ff:fe5a:c0a3 prefixlen 64 scopeid 0x20<link>
ether 02:45:02:5a:c0:a3 txqueuelen 1000 (Ethernet)
RX packets 3166 bytes 527516 (527.5 KB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 1945 bytes 267522 (267.5 KB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
enp0s8: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.1.111 netmask 255.255.255.0 broadcast 192.168.1.255
inet6 fe80::a00:27ff:feee:c657 prefixlen 64 scopeid 0x20<link>
ether 08:00:27:ee:c6:57 txqueuelen 1000 (Ethernet)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 17 bytes 1326 (1.3 KB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 1000 (Local Loopback)
RX packets 26 bytes 2312 (2.3 KB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 26 bytes 2312 (2.3 KB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
从我的 Windows ipconfig 输出来看:
Wireless LAN adapter WiFi:
Connection-specific DNS Suffix . :
Link-local IPv6 Address . . . . . : fe80::5f0:a3f4:e833:9aec%25
IPv4 Address. . . . . . . . . . . : 192.168.0.203
Subnet Mask . . . . . . . . . . . : 255.255.255.0
Default Gateway . . . . . . . . . : 192.168.0.1
这是我的 Vagrantfile 配置:
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
config.vm.box = "ubuntu/focal64"
config.vm.network "private_network", :ip => "192.168.1.111", :netmask => "255.255.255.0"
config.vm.network "forwarded_port", guest: 80, host: 7072
config.vm.synced_folder "", "/home/", :owner=>"vagrant", :group=>"vagrant", :mount_options=>["dmode=777","fmode=666"]
config.vm.provider "virtualbox" do |v|
v.memory = 2048
v.cpus = 2
v.customize ["modifyvm", :id, "--natdnshostresolver1", "on"]
v.customize ["modifyvm", :id, "--natdnsproxy1", "on"]
end
end
现在,当我尝试 ping 192.168.1.111 时,我在终端上得到以下输出:
192.168.1.111
使用 32 字节数据进行Ping 操作:
Reply from 192.168.1.1: Destination host unreachable.
Request timed out.
Request timed out.
Request timed out.
在网上研究之后,我发现将这些gateway4
和nameservers
地址添加到 /etc/netplan/50-vagrant.yml 文件中将使我的主机网络可见。
gateway4: 198.168.1.1
nameservers:
addresses: [198.168.1.1,8.8.8.8]
应用 netplan 后,我与 ssh 会话断开连接,无法再次连接到我的 vm-box2 机器。我使用 Windows 10 作为主机,Ubuntu 20.04.4 LTS
我是多节点虚拟机网络的新手。任何有关如何在网络中正确设置虚拟机的指导都非常好。