将数据包从绑定的 LAN 接口转发到 WAN

将数据包从绑定的 LAN 接口转发到 WAN

我无法在 ubuntu 服务器的局域网端和广域网端之间路由数据包。我需要一些帮助。我的服务器上有 5 个以太网端口 - 1 个板载端口(eth0,用于广域网)和 4 个 PCI 卡端口(eth1-eth4,绑定在一起作为 bond0,用于局域网)。任何帮助都非常感谢。提前致谢。

从通过 DHCP 获取 IP 的直接连接的 Windows 计算机进行一些 ping 测试。

C:\Users\mike>ipconfig

Windows IP Configuration


Ethernet adapter Ethernet:

   Connection-specific DNS Suffix  . : servo.firewall
   Link-local IPv6 Address . . . . . : fe80::ec54:579a:a4b5:991f%9
   IPv4 Address. . . . . . . . . . . : 10.10.10.108
   Subnet Mask . . . . . . . . . . . : 255.255.255.0
   Default Gateway . . . . . . . . . : 10.10.10.1

Ethernet adapter Bluetooth Network Connection:

   Media State . . . . . . . . . . . : Media disconnected
   Connection-specific DNS Suffix  . :

Wireless LAN adapter Wi-Fi:

   Media State . . . . . . . . . . . : Media disconnected
   Connection-specific DNS Suffix  . : hsd1.pa.comcast.net

Tunnel adapter Teredo Tunneling Pseudo-Interface:

   Media State . . . . . . . . . . . : Media disconnected
   Connection-specific DNS Suffix  . :

Tunnel adapter isatap.servo.firewall:

   Media State . . . . . . . . . . . : Media disconnected
   Connection-specific DNS Suffix  . : servo.firewall

C:\Users\mike>ping 10.10.10.1 <- 服务器

Pinging 10.10.10.1 with 32 bytes of data:
Reply from 10.10.10.1: bytes=32 time=15ms TTL=64
Reply from 10.10.10.1: bytes=32 time<1ms TTL=64
Reply from 10.10.10.1: bytes=32 time=1ms TTL=64
Reply from 10.10.10.1: bytes=32 time<1ms TTL=64

Ping statistics for 10.10.10.1:
    Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
    Minimum = 0ms, Maximum = 15ms, Average = 4ms

C:\Users\mike>ping 10.0.0.1 <- WAN 上的当前网关地址

Pinging 10.0.0.1 with 32 bytes of data: 
Control-C
^C

C:\Users\mike>ping yahoo.com <- DNS 测试

Ping request could not find host yahoo.com. Please check the name and try again.

C:\Users\mike>ping 8.8.8.8 <- 公共 IP 测试

Pinging 8.8.8.8 with 32 bytes of data:
Request timed out.
Request timed out.
Request timed out. 
Request timed out.

Ping statistics for 8.8.8.8:
    Packets: Sent = 4, Received = 0, Lost = 4 (100% loss),

以下是我为尝试使其正常工作而编辑的信息。

/etc/默认/ufw

DEFAULT_FORWARD_POLICY="ACCEPT"

/etc/ufw/sysctl.conf

net.ipv4.ip_forward=1
net/ipv6/conf/default/forwarding=1
net/ipv6/conf/all/forwarding=1

/etc/ufw/before.rules

# NAT table rules
*nat
:POSTROUTING ACCEPT [0:0]

# Forward traffic through eth0 - Change to match you out-interface
-A POSTROUTING -s 10.10.10.0/24 -o eth0 -j MASQUERADE

# don't delete the 'COMMIT' line or these nat table rules won't
# be processed
COMMIT

/etc/网络/接口

auto lo
iface lo inet loopback

auto eth0
iface eth0 inet dhcp

auto eth1
iface eth1 inet manual
bond-master bond0

auto eth2
iface eth2 inet manual
bond-master bond0

auto eth3
iface eth3 inet manual
bond-master bond0

auto eth4
iface eth4 inet manual
bond-master bond0

auto bond0
iface bond0 inet static
address 10.10.10.1
netmask 255.255.255.0
bond-mode broadcast
bond-miimon 100
bond-slaves eth1 eth2 eth3 eth4
post-up iptables-restore < /etc/iptables.up.rules
post-up ip6tables-restore < /etc/ip6tables.up.rules

是否配置

bond0     Link encap:Ethernet  HWaddr 00:26:55:dc:d8:4f
          inet addr:10.10.10.1  Bcast:10.10.10.255  Mask:255.255.255.0
          inet6 addr: fe80::226:55ff:fedc:d84f/64 Scope:Link
          UP BROADCAST MASTER MULTICAST  MTU:1500  Metric:1
          RX packets:3209 errors:0 dropped:0 overruns:0 frame:0
          TX packets:1362 errors:0 dropped:2 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:439418 (439.4 KB)  TX bytes:148509 (148.5 KB)

docker0   Link encap:Ethernet  HWaddr 02:42:d7:46:11:85
          inet addr:172.17.0.1  Bcast:0.0.0.0  Mask:255.255.0.0
          inet6 addr: fe80::42:d7ff:fe46:1185/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:8 errors:0 dropped:0 overruns:0 frame:0
          TX packets:23734 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:536 (536.0 B)  TX bytes:1631564 (1.6 MB)

eth0      Link encap:Ethernet  HWaddr 44:8a:5b:cd:95:56
          inet addr:10.0.0.201  Bcast:10.0.0.255  Mask:255.255.255.0
          inet6 addr: fe80::468a:5bff:fecd:9556/64 Scope:Link
           UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:362525 errors:0 dropped:0 overruns:0 frame:0
          TX packets:134713 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:91216243 (91.2 MB)  TX bytes:48392528 (48.3 MB)

eth1      Link encap:Ethernet  HWaddr 00:26:55:dc:d8:4f
          UP BROADCAST SLAVE MULTICAST  MTU:1500  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)
          Interrupt:34 Memory:fe920000-fe940000

eth2      Link encap:Ethernet  HWaddr 00:26:55:dc:d8:4f
          UP BROADCAST SLAVE MULTICAST  MTU:1500  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)
          Interrupt:40 Memory:fe900000-fe920000

eth3      Link encap:Ethernet  HWaddr 00:26:55:dc:d8:4f
          UP BROADCAST SLAVE MULTICAST  MTU:1500  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)
          Interrupt:42 Memory:fe720000-fe740000

eth4      Link encap:Ethernet  HWaddr 00:26:55:dc:d8:4f
          UP BROADCAST SLAVE MULTICAST  MTU:1500  Metric:1
          RX packets:3209 errors:0 dropped:0 overruns:0 frame:0
          TX packets:1362 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:439418 (439.4 KB)  TX bytes:148509 (148.5 KB)
          Interrupt:44 Memory:fe700000-fe720000

lo        Link encap:Local Loopback
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:48953 errors:0 dropped:0 overruns:0 frame:0
          TX packets:48953 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1
          RX bytes:8562847 (8.5 MB)  TX bytes:8562847 (8.5 MB)

vethdcf53dc Link encap:Ethernet  HWaddr 42:f9:a6:0e:bc:7a
          inet6 addr: fe80::40f9:a6ff:fe0e:bc7a/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:8 errors:0 dropped:0 overruns:0 frame:0
          TX packets:23750 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:648 (648.0 B)  TX bytes:1634701 (1.6 MB)

sudo iptables -t nat -L -v

Chain PREROUTING (policy ACCEPT 2629 packets, 1186K bytes)
 pkts bytes target     prot opt in     out     source               destination

Chain INPUT (policy ACCEPT 125 packets, 32492 bytes)
 pkts bytes target     prot opt in     out     source               destination

Chain OUTPUT (policy ACCEPT 368 packets, 32186 bytes)
 pkts bytes target     prot opt in     out     source               destination

Chain POSTROUTING (policy ACCEPT 368 packets, 32186 bytes)
 pkts bytes target     prot opt in     out     source               destination
    0     0 MASQUERADE  all  --  any    eth0    10.10.10.0/24        anywhere

Chain DOCKER (0 references)
 pkts bytes target     prot opt in     out     source               destination

答案1

我能够使用以下命令使其工作。我猜 ufw 规则不够用。

sudo iptables -A FORWARD -i eth0 -o bond0 -j ACCEPT 
sudo iptables -A FORWARD -i bond0 -o eth0 -j ACCEPT 
sudo iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE

答案2

我有一个类似的设置,其中机器 A 通过以太网电缆连接到机器 B,由于机器 A 没有互联网连接,我不得不使用机器 B 作为中继。首先,我在两台机器上都为以太网接口设置了私有 IP。在我的情况下,我使用了范围192.168.40.0/24。然后,在机器 BI 中运行,

sudo sysctl net.ipv4.conf.all.forwarding=1
sudo iptables -P FORWARD ACCEPT
sudo iptables -t nat -I POSTROUTING 1 -s 192.168.40.0/24 -j MASQUERADE

在机器 B 中,我不需要明确添加以太网接口的 FORWARD 链规则,因为默认ip route规则是将所有流量转发到互联网接口。我还必须通过将 NAT 规则插入 POSTROUTING 链的顶部来优先处理它,因为该链中已经有一个与所有流量匹配的特殊规则。

相关内容