添加新的 iptables 后,SSH 重定向到不同的 IP 地址

添加新的 iptables 后,SSH 重定向到不同的 IP 地址

我在通过 SSH 访问在 Proxmox 虚拟机管理程序上运行的专用服务器时遇到了一些麻烦,该服务器的 IP 地址为 40.53.XX.XX。

这是我的配置: 在此处输入图片描述

我在其上运行了 3 个 VM,它们具有本地 IP 地址:

Vm1 的 IP 地址为 192.168.4.4,并运行一个网站(使用 Apache),其域名为“london.austria.com”

Vm2 的 IP 地址为 192.168.4.5,并运行一个网站(使用 Apache),其域名为“manchester.austria.com”

通过该配置,我可以毫无问题地通过 SSH 访问所有 VM 和虚拟机管理程序。

今天,我设置了一个 Nginx 反向代理,以便从互联网访问 VM1 和 VM2 上的网站。

我们将其称为 Nginx 反向代理 VM3,其本地 IP 地址为 192.168.4.2

设置运行正常。我可以访问在 VM1 和 VM2 上运行的网站。

但是现在,在我将 /etc/network/interfaces 中最后两行的新 iptables 规则添加到 40.53.XX.XX 上的虚拟机管理程序后,我收到一条消息,说服务器指纹已更改...您要连接吗?

如果我单击“是”,则我将连接到 Nginx 反向代理(192.168.4.2),而不是虚拟机管理程序(40.53.XX.XX.)。

我尝试了另一个 SSH 工具,但问题仍然相同。

虚拟机管理程序上的 /etc/network/interfaces 如下:

root@ns568745:~# cat /etc/network/interfaces
    # This file describes the network interfaces available on your system
    # and how to activate them. For more information, see interfaces(5).

    # The loopback network interface
    auto lo
    iface lo inet loopback


    # vmbr0: Bridging. Make sure to use only MAC adresses that were assigned to you.
    auto vmbr0
    iface vmbr0 inet static
        address 40.53.XX.XX /24
        gateway 40.53.XX.XX .254
        bridge_ports eno1
        bridge_stp off
        bridge_fd 0





    auto vmbr1
    iface vmbr1 inet static
        address  192.168.4.254/24
        broadcast 192.168.4.255
        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 '192.168.4.254/24' -o vmbr0 -j MASQUERADE
        post-down iptables -t nat -D POSTROUTING -s '192.168.4.254/24' -o vmbr0 -j MASQUERADE


post-up iptables -t nat -A PREROUTING -d 40.53.XX.XX -p tcp -m multiport --dports 80,443 -m comment --comment "nginx" -j DNAT --to-destination 192.168.4.2

post-up iptables -t nat -A POSTROUTING -s 192.168.4.0/24 -j SNAT --to-source 40.53.XX.XX

以及 /etc/nginx/sites-available 中的 Nginx 配置文件(IP 地址:192.168.4.2)

server {
    listen 80 default;
    listen [::]:80 default;
    server_name manchester.example.com;

    location / {
        proxy_pass http://192.168.4.5:80;
    }
}

server {
    listen 80;
    listen [::]:80;
    server_name london.example.com;

    location / {
        proxy_pass http://192.168.4.4:80;
    }
}

为什么当我想要连接到 40.53.XX.XX 时会被重定向到 192.168.4.2 以及如何通过 SSH 连接到 40.53.XX.XX?

编辑1:

iptables-save在 /etc/network/interface 的最后两行启用 2 个 iptables 规则后的输出

root@ns568745:~# iptables-save
# Generated by iptables-save v1.6.0 on Sun Feb  9 16:01:44 2020
*filter
:INPUT ACCEPT [640:213744]
:FORWARD ACCEPT [15:900]
:OUTPUT ACCEPT [495:208357]
COMMIT
# Completed on Sun Feb  9 16:01:44 2020
# Generated by iptables-save v1.6.0 on Sun Feb  9 16:01:44 2020
*nat
:PREROUTING ACCEPT [82:4115]
:INPUT ACCEPT [68:2699]
:OUTPUT ACCEPT [27:1694]
:POSTROUTING ACCEPT [34:2114]
-A PREROUTING -d 40.53.XX.XX/32 -p tcp -m multiport --dports 80,443,22 -m comment --comment nginx -j DNAT --to-destination 192.168.4.2
-A POSTROUTING -s 192.168.4.0/24 -o vmbr0 -j MASQUERADE
-A POSTROUTING -s 192.168.4.0/24 -j SNAT --to-source 40.53.XX.XX
COMMIT
# Completed on Sun Feb  9 16:01:44 2020

iptables-save禁用 /etc/network/interface 最后两行中的 2 个 iptables 规则时的输出

root@ns568745:~# iptables-save
# Generated by iptables-save v1.6.0 on Sun Feb  9 15:56:26 2020
*filter
:INPUT ACCEPT [75392:20174610]
:FORWARD ACCEPT [34704:41945853]
:OUTPUT ACCEPT [70877:23709671]
COMMIT
# Completed on Sun Feb  9 15:56:26 2020
# Generated by iptables-save v1.6.0 on Sun Feb  9 15:56:26 2020
*nat
:PREROUTING ACCEPT [15399:828377]
:INPUT ACCEPT [11207:472538]
:OUTPUT ACCEPT [428:27752]
:POSTROUTING ACCEPT [591:34272]
-A POSTROUTING -s 192.168.4.0/24 -o vmbr0 -j MASQUERADE
COMMIT
# Completed on Sun Feb  9 15:56:26 2020

答案1

问题出在这一行:

-A PREROUTING -d 40.53.XX.XX/32 -p tcp -m multiport --dports 80,443,22 -m comment --comment nginx -j DNAT --to-destination 192.168.4.2

此规则将 http (80)、https (443) 和 ssh (22) 转发到192.168.4.2主机。

由于您的原始interfaces命令没有包含端口 22,因此必须将端口 22 添加到DNAT规则的其他位置。

相关内容