具有一个接口和别名的 Linux 网关

具有一个接口和别名的 Linux 网关

您好,我正在尝试使用 iptables 使用 Linux 机器作为同一网络中其他机器的网关,这些是与两台机器相关的配置:

  • 机器A(这是我想用作网关的)
root@router-1:~# sysctl -p
net.ipv4.ip_forward = 1
net.ipv6.conf.all.forwarding = 1
root@router-1:~# ip a
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 f2:3c:91:58:5b:fb brd ff:ff:ff:ff:ff:ff
    inet 172.105.89.xxx/24 brd 172.105.89.255 scope global eth0
       valid_lft forever preferred_lft forever
    inet 192.168.159.203/17 brd 192.168.255.255 scope global eth0
       valid_lft forever preferred_lft forever
    inet6 2a01:7e01::f03c:91ff:fe58:5bfb/64 scope global dynamic mngtmpaddr noprefixroute 
       valid_lft 2591978sec preferred_lft 604778sec
    inet6 fe80::f03c:91ff:fe58:5bfb/64 scope link 
       valid_lft forever preferred_lft forever

我有以下 iptables 规则:

# Generated by iptables-save v1.6.1 on Thu Aug  1 06:42:26 2019
*mangle
:PREROUTING ACCEPT [729:54316]
:INPUT ACCEPT [729:54316]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [539:62761]
:POSTROUTING ACCEPT [539:62761]
COMMIT
# Completed on Thu Aug  1 06:42:26 2019
# Generated by iptables-save v1.6.1 on Thu Aug  1 06:42:26 2019
*filter
:INPUT ACCEPT [493:36634]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [373:47212]
-A FORWARD -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
COMMIT
# Completed on Thu Aug  1 06:42:26 2019
# Generated by iptables-save v1.6.1 on Thu Aug  1 06:42:26 2019
*nat
:PREROUTING ACCEPT [28:1501]
:INPUT ACCEPT [28:1501]
:OUTPUT ACCEPT [5:397]
:POSTROUTING ACCEPT [2:161]
-A POSTROUTING -s 192.168.128.0/17 ! -d 192.168.128.0/17 -j MASQUERADE
COMMIT
# Completed on Thu Aug  1 06:42:26 2019
  • 机器B:
root@client:~# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1
    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 pfifo_fast state UP group default qlen 1000
    link/ether f2:3c:91:58:5b:62 brd ff:ff:ff:ff:ff:ff
    inet 192.168.157.248/17 brd 192.168.255.255 scope global eth0
       valid_lft forever preferred_lft forever
    inet6 2a01:7e01::f03c:91ff:fe58:5b62/64 scope global mngtmpaddr dynamic 
       valid_lft 2591988sec preferred_lft 604788sec
    inet6 fe80::f03c:91ff:fe58:5b62/64 scope link 
       valid_lft forever preferred_lft forever
root@client:~# ip r
default via 192.168.159.203 dev eth0 onlink 
192.168.128.0/17 dev eth0 proto kernel scope link src 192.168.157.248

root@client:~# ping 192.168.159.203
PING 192.168.159.203 (192.168.159.203) 56(84) bytes of data.
64 bytes from 192.168.159.203: icmp_seq=1 ttl=64 time=0.773 ms

因此,客户端计算机可以 ping 网关计算机,因为它们位于同一子网中,但无法访问该子网之外的任何其他 IP。

答案1

您可能需要对您的内部网络进行 SNAT(源 NAT):

-A POSTROUTING -o eth0 -j eth0_masq
-A eth0_masq -s 192.168.158.203/17 -j SNAT --to-source 172.105.89.xxx/24

其中“.89.xxx”是您想要在出站连接的公共 IP 地址范围中使用的公共 IP 地址。

答案2

最后我发现我的配置没问题。

问题在于提供商阻止了流量以避免欺骗。

相关内容