![从外部无法访问 Vagrant 机器](https://linux22.com/image/14350/%E4%BB%8E%E5%A4%96%E9%83%A8%E6%97%A0%E6%B3%95%E8%AE%BF%E9%97%AE%20Vagrant%20%E6%9C%BA%E5%99%A8%20.png)
当我的意思是“从外部”时,我的意思是来自互联网。
我的机器位于路由器后面。我正在运行 vagrant 来启动 3 台机器。这是 vagrant 文件(我删除了多余的内容):
Vagrant.configure("2") do |config|
config.vm.box = "bento/ubuntu-20.04"
config.vm.provider :virtualbox
mount_new_disk = <<-SCRIPT
sudo mkdir /mnt/da
sudo mkfs -t ext4 /dev/sdb
sudo mount /dev/sdb /mnt/da
SCRIPT
config.vm.define "da1" do |da1|
da1.vm.synced_folder ".", "/vagrant", disabled: true
da1.vm.hostname = "da1"
da1.vm.network "private_network", ip: "10.118.8.10"
config.ssh.forward_agent = true
end
config.vm.define "da2" do |da2|
da2.vm.synced_folder ".", "/vagrant", disabled: true
da2.vm.disk :disk, name: "backup", size: "10GB"
da2.vm.hostname = "da2"
da2.vm.network "private_network", ip: "10.118.8.11"
config.ssh.forward_agent = true
config.vm.provision :shell, :inline => mount_new_disk
end
config.vm.define "da3" do |da3|
da3.vm.synced_folder ".", "/vagrant", disabled: true
da3.vm.disk :disk, name: "backup", size: "10GB"
da3.vm.hostname = "da3"
da3.vm.network "private_network", ip: "10.118.8.12"
da3.vm.network "public_network", ip: "192.168.1.155", netmask: "255.255.255.0", bridge: "Intel(R) I211 Gigabit Network Connection"
da3.vm.network "forwarded_port", guest: 22, host: 5555
config.ssh.forward_agent = true
config.vm.provision :shell, :inline => mount_new_disk
end
end
虚拟机上的 sshd 配置:
testuser1@da3:~$ head -20 /etc/ssh/sshd_config|grep -v '#'
Include /etc/ssh/sshd_config.d/*.conf
Port 22
AddressFamily any
ListenAddress 0.0.0.0
ListenAddress ::
并且可以使用桥接 IP ( 192.168.1.155
) 从我的计算机上的任何终端连接到它:
D:\vag_rant>ssh [email protected]
[email protected]'s password:
Welcome to Ubuntu 20.04.1 LTS (GNU/Linux 5.4.0-58-generic x86_64)
防火墙已关闭:
testuser1@da3:~$ sudo ufw status
Status: inactive
ip addr
这是虚拟机上的输出:
testuser1@da3:~$ ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
link/ether 08:00:27:14:86:db brd ff:ff:ff:ff:ff:ff
inet 10.0.2.15/24 brd 10.0.2.255 scope global dynamic eth0
valid_lft 86205sec preferred_lft 86205sec
inet6 fe80::a00:27ff:fe14:86db/64 scope link
valid_lft forever preferred_lft forever
3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
link/ether 08:00:27:86:38:bf brd ff:ff:ff:ff:ff:ff
inet 10.118.8.12/24 brd 10.118.8.255 scope global eth1
valid_lft forever preferred_lft forever
inet6 fe80::a00:27ff:fe86:38bf/64 scope link
valid_lft forever preferred_lft forever
4: eth2: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
link/ether 08:00:27:5a:fa:4c brd ff:ff:ff:ff:ff:ff
inet 192.168.1.155/24 brd 192.168.1.255 scope global eth2
valid_lft forever preferred_lft forever
inet6 fe80::a00:27ff:fe5a:fa4c/64 scope link
valid_lft forever preferred_lft forever
我port 22
在路由器上打开 ip 192.168.1.155
。
到目前为止一切看起来都很好。我之前只使用一个只有桥接网络适配器的虚拟机执行此操作,并且它有效。它可以从外面进入。
但由于某种原因,这不起作用。没有人可以使用我的公共 IP 从外部访问我的机器...
经过一番努力,我取得了进展,现在它只是说no route to host
。
答案1
编辑:发现问题。相同的端口 22 被转发到路由器上的另一台机器。当我解决了这个问题之后,又出现了一个问题。 vagrant 机器上的默认网关是 virtualbox 适配器的网关。我必须将其修改为路由器的默认网关,然后它就可以了。
root@da3:/home/testuser1# ip route show
default via 10.0.2.2 dev eth0 proto dhcp src 10.0.2.15 metric 100
default via 192.168.1.254 dev eth2 proto dhcp src 192.168.1.104 metric 100
10.0.2.0/24 dev eth0 proto kernel scope link src 10.0.2.15
10.0.2.2 dev eth0 proto dhcp scope link src 10.0.2.15 metric 100
10.118.8.0/24 dev eth1 proto kernel scope link src 10.118.8.12
192.168.1.0/24 dev eth2 proto kernel scope link src 192.168.1.104
192.168.1.254 dev eth2 proto dhcp scope link src 192.168.1.104 metric 100
root@da3:/home/testuser1# ip route del default via 10.0.2.2
root@da3:/home/testuser1# ip route show
default via 192.168.1.254 dev eth2 proto dhcp src 192.168.1.104 metric 100
10.0.2.0/24 dev eth0 proto kernel scope link src 10.0.2.15
10.0.2.2 dev eth0 proto dhcp scope link src 10.0.2.15 metric 100
10.118.8.0/24 dev eth1 proto kernel scope link src 10.118.8.12
192.168.1.0/24 dev eth2 proto kernel scope link src 192.168.1.104
192.168.1.254 dev eth2 proto dhcp scope link src 192.168.1.104 metric 100