Proxmox Nat 网络问题

efe*_*aid 5 nat iptables proxmox dnat masquerade

定义

我已经安装了 Proxmox 3.2,我正在尝试将虚拟机配置为处理所有流量并将它们转发到具有私有 ip 的节点的通信服务器。

我使用两个配置完全相同的 CentOS 虚拟机为 NAT 网络配置了服务器。

我所做的

Proxmox wiki 有一个非常有限和基本的 nat 网络文档。
我在这里和 proxmox 论坛上发现了类似的问题(这个这个)。我试图了解linux nat 网络的基础知识,所以我从头到尾完成了这个非常容易理解的教程。我阅读了这篇文章以了解iptables nat 规则

问题

安装和配置后,当我从主机 ping 到 VM 或从 VM 到主机时,输出是:

root@testPrx:~# ping 10.0.4.2
PING 10.0.4.2 (10.0.4.2) 56(84) bytes of data.
From 10.0.4.1 icmp_seq=2 Destination Host Unreachable
Run Code Online (Sandbox Code Playgroud)

当我尝试从具有 Internet 连接和 (192.168.0.3) 的同一网络中的服务器通过 telnet 连接到通信服务器的公共 IP 时

bash-4.1# telnet 192.168.0.2 2701
Trying 192.168.0.2...
telnet: connect to address 192.168.0.2: No route to host
Run Code Online (Sandbox Code Playgroud)

当我尝试 telnet 连接到 localhost 时,192.168.0.2 结果相同:

root@testPrx:~# telnet localhost 2701
Trying 127.0.0.1...
telnet: Unable to connect to remote host: Connection refused
Run Code Online (Sandbox Code Playgroud)

我的错误是什么?

配置

所有主机和虚拟机配置为:

Proxmox 服务器

Proxmox 版本

root@testPrx:~# pveversion
pve-manager/3.2-4/e24a91c1 (running kernel: 2.6.32-29-pve)
Run Code Online (Sandbox Code Playgroud)

网络接口

网络接口添加的网络接口: 在此处输入图片说明

  • net0 -> vmbr0
  • 网络 1 -> vmbr1

    root@testPrx:~# cat /etc/network/interfaces

    auto lo
    iface lo inet loopback
    
    auto vmbr0
    iface vmbr0 inet static
            address 192.168.0.2
            netmask 255.255.255.0
            gateway 192.168.0.1
            bridge_ports eth0
            bridge_stp off
            bridge_fd 0
    auto vmbr1
    iface vmbr1 inet static
            address 10.0.4.1
            netmask 255.255.255.0
            bridge_ports none
            bridge_stp off
            bridge_fd 0
    
    post-up echo 1 > /proc/sys/net/ipv4/ip_forward
    post-up iptables -t nat -A POSTROUTING -s '10.0.4.0/24' -o vmbr0 -j MASQUERADE
    post-down iptables -t nat -D POSTROUTING -s '10.0.4.0/24' -o vmbr0 -j MASQUERADE
    
    #these rules forward traffic on port 2701 to port 22 on the VM at IP 10.0.4.2
    
    post-up iptables -t nat -A PREROUTING -i vmbr0 -p tcp --dport 2701 -j DNAT --to 10.0.4.2:22
    post-down iptables -t nat -D PREROUTING -i vmbr0 -p tcp --dport 2702 -j DNAT --to 10.0.4.2:22
    
    Run Code Online (Sandbox Code Playgroud)

nat 的防火墙规则(没有任何过滤器)

root@testPrx:~# iptables -L -t nat
Chain PREROUTING (policy ACCEPT)
target     prot opt source               destination
DNAT       tcp  --  anywhere             anywhere             tcp dpt:2701 to:10.0.4.2:22

Chain POSTROUTING (policy ACCEPT)
target     prot opt source               destination
MASQUERADE  all  --  10.0.4.0/24          anywhere

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination
Run Code Online (Sandbox Code Playgroud)

路由表

root@testPrx:~# route
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
10.0.4.0        *               255.255.255.0   U     0      0        0 vmbr1
192.168.0.0     *               255.255.255.0   U     0      0        0 vmbr0
default         192.168.0.1     0.0.0.0         UG    0      0        0 vmbr0
Run Code Online (Sandbox Code Playgroud)

ip转发

root@testPrx:~# cat /proc/sys/net/ipv4/ip_forward
1
Run Code Online (Sandbox Code Playgroud)

虚拟机

操作系统版本

-bash-4.1# cat /etc/redhat-release
CentOS release 6.4 (Final)
Run Code Online (Sandbox Code Playgroud)

接口

eth0

-bash-4.1# cat /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE=eth0
TYPE=Ethernet
ONBOOT=yes
NM_CONTROLLED=no
BOOTPROTO=none
IPADDR=192.168.0.3
GATEWAY=192.168.0.1
NETMASK=255.255.255.0
Run Code Online (Sandbox Code Playgroud)

eth1

-bash-4.1# cat /etc/sysconfig/network-scripts/ifcfg-eth1
DEVICE=eth1
TYPE=Ethernet
ONBOOT=yes
NM_CONTROLLED=no
BOOTPROTO=none
IPADDR=10.0.4.2
GATEWAY=10.0.4.1
NETMASK=255.255.255.0
Run Code Online (Sandbox Code Playgroud)

SSH 守护进程成功运行并监听端口(22)

-bash-4.1# netstat -puntl
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address               Foreign Address             State       PID/Program name
tcp        0      0 0.0.0.0:85                  0.0.0.0:*                   LISTEN      1100/sshd
Run Code Online (Sandbox Code Playgroud)

路由表

-bash-4.1# route
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
10.0.4.0        *               255.255.255.0   U     0      0        0 eth1
192.168.0.0     *               255.255.255.0   U     0      0        0 eth0
link-local      *               255.255.0.0     U     1002   0        0 eth0
link-local      *               255.255.0.0     U     1003   0        0 eth1
default         10.0.4.1        0.0.0.0         UG    0      0        0 eth1
Run Code Online (Sandbox Code Playgroud)

efe*_*aid 4

我在睡觉时发现了我的错误,所以我立即醒来并修复了这个问题。进行 nat 网络的接口是 vmbr1,但是当我设置虚拟机时,我为网络设备分配了错误的桥接模式 (vmbr0)。

为了解决这个问题,我停止了虚拟机并将桥接模式从 vmbr0 编辑到 vmbr1。现在一切正常。

在此输入图像描述