![更新1](https://linux22.com/image/86545/%E6%9B%B4%E6%96%B01.png)
我为我的两个网络接口配置了以下内容。
eth1 10.93.90.1(生产网络)
eth0 192.160.0.1(私有网络/Labb网络)
我想使用 NAT 配置 eth0,以便可以通过 eth1 与生产网络进行通信。
这是我尝试过的:
[email protected] # echo 1 > /proc/sys/net/ipv4/ip_forward
[email protected] # iptables -t nat -A POSTROUTING -o eth1 -j MASQUERADE
[email protected] # iptables -A FORWARD -i eth0 -o eth1 -j ACCEPT
[email protected] # iptables -A FORWARD -i eth1 -o eth0 -m state --state ESTABLISHED,RELATED -j ACCEPT
但是连接到 eth0 接口且 IP 地址为 192.168.0.236 的节点无法与生产网络 (10.0.0.0) 上的其他节点通信。但它可以与主机通信:
[email protected] # ping 10.93.90.1
10.93.90.1 is alive!
但生产网络上的任何其他主机都没有:
[email protected] # ping 10.93.89.1
No response from 10.93.89.1
当我从主机本身 ping 时,这工作正常:
[email protected] # ping 10.93.89.1
PING 10.93.89.1 (10.93.89.1) 56(84) bytes of data.
我在这里错过了什么吗?或者仅使用 NAT 无法做到这一点?
更新1
主机 eth1 (10.93.90.1) 的路由表:
[email protected] # route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
default fafne-vlan93.se 0.0.0.0 UG 0 0 0 eth1
10.93.0.0 * 255.255.0.0 U 0 0 0 eth1
192.168.0.0 * 255.255.255.0 U 0 0 0 eth0
主机 eth0 的路由表 (192.168.0.236)
[email protected] # ip route list
default via 192.168.0.1 dev eth0
default dev eth0 scope link metric 99
169.254.0.0/16 dev eth0 proto kernel scope link src 169.254.221.167
169.254.0.0/16 dev eth0 scope link metric 99
192.168.0.0/24 dev eth0 proto kernel scope link src 192.168.0.236
更新2
我更改了以下规则:
iptables -A FORWARD -i eth1 -o eth0 -m state --state ESTABLISHED,RELATED -j ACCEPT
到:
iptables -A FORWARD -i eth1 -o eth0 -j ACCEPT
我还添加了以下规则来调试 ping:
iptables -I FORWARD -j LOG --log-prefix 'SNOOP_'
以下是我从 192.168.0.236 ping 到 10.93.89.1 时记录的内容:
Apr 1 09:14:44 lnxchrispet kernel: [2415274.711664] SNOOP_IN=eth0 OUT=eth1 MAC=90:e2:ba:3c:87:c4:ac:cc:8e:37:47:a6:08:00 SRC=192.168.0.236 DST=10.93.89.1 LEN=84 TOS=0x00 PREC=0x00 TTL=63 ID=9639 DF PROTO=ICMP TYPE=8 CODE=0 ID=0 SEQ=0
Apr 1 09:14:44 lnxchrispet kernel: [2415274.711935] SNOOP_IN=eth1 OUT=eth0 MAC=7c:05:07:0e:36:03:70:54:d2:7c:45:be:08:00 SRC=10.93.89.1 DST=192.168.0.236 LEN=84 TOS=0x00 PREC=0x00 TTL=63 ID=58826 PROTO=ICMP TYPE=0 CODE=0 ID=0 SEQ=0
更新3
这是 nat 加载的所有内核模块:
[email protected] # lsmod |grep nat
iptable_nat 12928 1
nf_nat 18231 2 iptable_nat,ipt_MASQUERADE
nf_conntrack_ipv4 14078 3 nf_nat,iptable_nat
nf_conntrack 52720 5 nf_conntrack_ipv4,nf_nat,iptable_nat,ipt_MASQUERADE,xt_state
ip_tables 22042 2 iptable_nat,iptable_filter
x_tables 19118 6 ip_tables,iptable_nat,ipt_MASQUERADE,iptable_filter,xt_state,ipt_LOG
该模块似乎缺失,是否相关?
nf_nat_ipv4
我尝试加载它,但似乎不起作用:
[email protected] # modprobe -v nf_nat_ipv4
[email protected] # lsmod |grep nat
iptable_nat 12928 1
nf_nat 18231 2 iptable_nat,ipt_MASQUERADE
nf_conntrack_ipv4 14078 3 nf_nat,iptable_nat
nf_conntrack 52720 5 nf_conntrack_ipv4,nf_nat,iptable_nat,ipt_MASQUERADE,xt_state
ip_tables 22042 2 iptable_nat,iptable_filter
x_tables 19118 6 ip_tables,iptable_nat,ipt_MASQUERADE,iptable_filter,xt_state,ipt_LOG
更新4
似乎有些不对劲,缺少有关 NAT 的模块:
[email protected] # ls -l /lib/modules/3.2.0-4-amd64/kernel/net/netfilter
total 1076
drwxr-xr-x 2 root root 4096 Mar 4 06:42 ipset
drwxr-xr-x 2 root root 4096 Mar 4 06:42 ipvs
-rw-r--r-- 1 root root 10416 Feb 29 18:10 nf_conntrack_amanda.ko
-rw-r--r-- 1 root root 4336 Feb 29 18:10 nf_conntrack_broadcast.ko
-rw-r--r-- 1 root root 14440 Feb 29 18:10 nf_conntrack_ftp.ko
-rw-r--r-- 1 root root 67360 Feb 29 18:10 nf_conntrack_h323.ko
-rw-r--r-- 1 root root 12944 Feb 29 18:10 nf_conntrack_irc.ko
-rw-r--r-- 1 root root 116096 Feb 29 18:10 nf_conntrack.ko
-rw-r--r-- 1 root root 5552 Feb 29 18:10 nf_conntrack_netbios_ns.ko
-rw-r--r-- 1 root root 32528 Feb 29 18:10 nf_conntrack_netlink.ko
-rw-r--r-- 1 root root 14416 Feb 29 18:10 nf_conntrack_pptp.ko
-rw-r--r-- 1 root root 16672 Feb 29 18:10 nf_conntrack_proto_dccp.ko
-rw-r--r-- 1 root root 11912 Feb 29 18:10 nf_conntrack_proto_gre.ko
-rw-r--r-- 1 root root 15192 Feb 29 18:10 nf_conntrack_proto_sctp.ko
-rw-r--r-- 1 root root 8992 Feb 29 18:10 nf_conntrack_proto_udplite.ko
-rw-r--r-- 1 root root 10664 Feb 29 18:10 nf_conntrack_sane.ko
-rw-r--r-- 1 root root 36480 Feb 29 18:10 nf_conntrack_sip.ko
-rw-r--r-- 1 root root 6352 Feb 29 18:10 nf_conntrack_snmp.ko
-rw-r--r-- 1 root root 10240 Feb 29 18:10 nf_conntrack_tftp.ko
-rw-r--r-- 1 root root 11400 Feb 29 18:10 nfnetlink.ko
-rw-r--r-- 1 root root 20120 Feb 29 18:10 nfnetlink_log.ko
-rw-r--r-- 1 root root 18920 Feb 29 18:10 nfnetlink_queue.ko
-rw-r--r-- 1 root root 4952 Feb 29 18:10 nf_tproxy_core.ko
-rw-r--r-- 1 root root 44016 Feb 29 18:10 x_tables.ko
-rw-r--r-- 1 root root 6904 Feb 29 18:10 xt_addrtype.ko
-rw-r--r-- 1 root root 7688 Feb 29 18:10 xt_AUDIT.ko
-rw-r--r-- 1 root root 4912 Feb 29 18:10 xt_CHECKSUM.ko
-rw-r--r-- 1 root root 4384 Feb 29 18:10 xt_CLASSIFY.ko
-rw-r--r-- 1 root root 5440 Feb 29 18:10 xt_cluster.ko
-rw-r--r-- 1 root root 4144 Feb 29 18:10 xt_comment.ko
-rw-r--r-- 1 root root 5480 Feb 29 18:10 xt_connbytes.ko
-rw-r--r-- 1 root root 8776 Feb 29 18:10 xt_connlimit.ko
-rw-r--r-- 1 root root 6560 Feb 29 18:10 xt_connmark.ko
-rw-r--r-- 1 root root 6272 Feb 29 18:10 xt_CONNSECMARK.ko
-rw-r--r-- 1 root root 7032 Feb 29 18:10 xt_conntrack.ko
-rw-r--r-- 1 root root 4400 Feb 29 18:10 xt_cpu.ko
-rw-r--r-- 1 root root 7232 Feb 29 18:10 xt_CT.ko
-rw-r--r-- 1 root root 6376 Feb 29 18:10 xt_dccp.ko
-rw-r--r-- 1 root root 4352 Feb 29 18:10 xt_devgroup.ko
-rw-r--r-- 1 root root 5448 Feb 29 18:10 xt_dscp.ko
-rw-r--r-- 1 root root 6176 Feb 29 18:10 xt_DSCP.ko
-rw-r--r-- 1 root root 4704 Feb 29 18:10 xt_esp.ko
-rw-r--r-- 1 root root 17592 Feb 29 18:10 xt_hashlimit.ko
-rw-r--r-- 1 root root 5280 Feb 29 18:10 xt_helper.ko
-rw-r--r-- 1 root root 4472 Feb 29 18:10 xt_hl.ko
-rw-r--r-- 1 root root 5896 Feb 29 18:10 xt_HL.ko
-rw-r--r-- 1 root root 10888 Feb 29 18:10 xt_IDLETIMER.ko
-rw-r--r-- 1 root root 4848 Feb 29 18:10 xt_iprange.ko
-rw-r--r-- 1 root root 5672 Feb 29 18:10 xt_ipvs.ko
-rw-r--r-- 1 root root 8168 Feb 29 18:10 xt_LED.ko
-rw-r--r-- 1 root root 4456 Feb 29 18:10 xt_length.ko
-rw-r--r-- 1 root root 6480 Feb 29 18:10 xt_limit.ko
-rw-r--r-- 1 root root 4200 Feb 29 18:10 xt_mac.ko
-rw-r--r-- 1 root root 4936 Feb 29 18:10 xt_mark.ko
-rw-r--r-- 1 root root 5144 Feb 29 18:10 xt_multiport.ko
-rw-r--r-- 1 root root 4536 Feb 29 18:10 xt_NFLOG.ko
-rw-r--r-- 1 root root 6080 Feb 29 18:10 xt_NFQUEUE.ko
-rw-r--r-- 1 root root 4792 Feb 29 18:10 xt_NOTRACK.ko
-rw-r--r-- 1 root root 9536 Feb 29 18:10 xt_osf.ko
-rw-r--r-- 1 root root 4272 Feb 29 18:10 xt_owner.ko
-rw-r--r-- 1 root root 5192 Feb 29 18:10 xt_physdev.ko
-rw-r--r-- 1 root root 4224 Feb 29 18:10 xt_pkttype.ko
-rw-r--r-- 1 root root 5896 Feb 29 18:10 xt_policy.ko
-rw-r--r-- 1 root root 5120 Feb 29 18:10 xt_quota.ko
-rw-r--r-- 1 root root 5960 Feb 29 18:10 xt_rateest.ko
-rw-r--r-- 1 root root 8968 Feb 29 18:10 xt_RATEEST.ko
-rw-r--r-- 1 root root 4072 Feb 29 18:10 xt_realm.ko
-rw-r--r-- 1 root root 20136 Feb 29 18:10 xt_recent.ko
-rw-r--r-- 1 root root 5432 Feb 29 18:10 xt_sctp.ko
-rw-r--r-- 1 root root 6856 Feb 29 18:10 xt_SECMARK.ko
-rw-r--r-- 1 root root 10432 Feb 29 18:10 xt_set.ko
-rw-r--r-- 1 root root 8704 Feb 29 18:10 xt_socket.ko
-rw-r--r-- 1 root root 5056 Feb 29 18:10 xt_state.ko
-rw-r--r-- 1 root root 5232 Feb 29 18:10 xt_statistic.ko
-rw-r--r-- 1 root root 5088 Feb 29 18:10 xt_string.ko
-rw-r--r-- 1 root root 5024 Feb 29 18:10 xt_tcpmss.ko
-rw-r--r-- 1 root root 8496 Feb 29 18:10 xt_TCPMSS.ko
-rw-r--r-- 1 root root 5616 Feb 29 18:10 xt_TCPOPTSTRIP.ko
-rw-r--r-- 1 root root 6376 Feb 29 18:10 xt_tcpudp.ko
-rw-r--r-- 1 root root 9216 Feb 29 18:10 xt_TEE.ko
-rw-r--r-- 1 root root 6024 Feb 29 18:10 xt_time.ko
-rw-r--r-- 1 root root 10848 Feb 29 18:10 xt_TPROXY.ko
-rw-r--r-- 1 root root 4184 Feb 29 18:10 xt_TRACE.ko
-rw-r--r-- 1 root root 5176 Feb 29 18:10 xt_u32.ko
Debian Wheezy 系统上当前内核为 3.2.0-4-amd64
[email protected] # uname -r
3.2.0-4-amd64
[email protected] # lsb_release -a
No LSB modules are available.
Distributor ID: Debian
Description: Debian GNU/Linux 7.9 (wheezy)
Release: 7.9
Codename: wheezy
我将尝试安装 wheezy-backports 内核 3.16.0-0.bpo.4-amd64。
更新5
我可以在启动 wheezy-backports 内核 3.16.0-0.bpo.4-amd64 后加载正确的模块
[email protected] # uname -r
3.16.0-0.bpo.4-amd64
[email protected] # modprobe -v iptable_nat
insmod /lib/modules/3.16.0-0.bpo.4-amd64/kernel/net/netfilter/x_tables.ko
insmod /lib/modules/3.16.0-0.bpo.4-amd64/kernel/net/netfilter/nf_conntrack.ko
insmod /lib/modules/3.16.0-0.bpo.4-amd64/kernel/net/netfilter/nf_nat.ko
insmod /lib/modules/3.16.0-0.bpo.4-amd64/kernel/net/ipv4/netfilter/nf_nat_ipv4.ko
insmod /lib/modules/3.16.0-0.bpo.4-amd64/kernel/net/ipv4/netfilter/ip_tables.ko
insmod /lib/modules/3.16.0-0.bpo.4-amd64/kernel/net/ipv4/netfilter/iptable_nat.ko
[email protected] # lsmod |grep nat
iptable_nat 12646 1
ip_tables 25967 2 iptable_filter,iptable_nat
nf_nat_ipv4 12912 1 iptable_nat
nf_nat 18159 3 ipt_MASQUERADE,nf_nat_ipv4,iptable_nat
nf_conntrack 91426 6 ipt_MASQUERADE,nf_nat,xt_state,nf_nat_ipv4,iptable_nat,nf_conntrack_ipv4
答案1
因此,Debian Wheezy 安装似乎有一个有缺陷的内核,或者不完全支持 NAT。它缺少一个特定的内核模块nf_nat_ipv4
.从这里可以看出:
[email protected] # lsmod |grep nat
iptable_nat 12928 1
nf_nat 18231 2 iptable_nat,ipt_MASQUERADE
nf_conntrack_ipv4 14078 3 nf_nat,iptable_nat
nf_conntrack 52720 5 nf_conntrack_ipv4,nf_nat,iptable_nat,ipt_MASQUERADE,xt_state
ip_tables 22042 2 iptable_nat,iptable_filter
x_tables 19118 6 ip_tables,iptable_nat,ipt_MASQUERADE,iptable_filter,xt_state,ipt_LOG
安装3.16.0-0.bpo.4-amd64内核
Debian Wheezy 的默认内核是3.2.0-4-amd64
.因此,我安装了 wheezy-backports 内核3.16.0-0.bpo.4-amd64
。
[email protected] # apt-get update
[email protected] # apt-get install linux-image-3.16.0-0.bpo.4-amd64 linux-headers-3.16.0-0.bpo.4-all-amd64
The following packages have unmet dependencies:
linux-image-3.16.0-0.bpo.4-amd64 : Depends: initramfs-tools (>= 0.110~) but it is not going to be installed
E: Unable to correct problems, you have held broken packages.
APT 抱怨无法将正确的 initramfs-tools 包安装为依赖项。所以我不得不从 wheezy-backports 拳头安装它:
[email protected] # apt-get -t wheezy-backports install initramfs-tools
然后我可以安装3.16.0-0.bpo.4-amd64
内核:
[email protected] # apt-get install linux-image-3.16.0-0.bpo.4-amd64 linux-headers-3.16.0-0.bpo.4-all-amd64
然后我重新启动系统并继续加载 NAT 内核模块。
加载 NAT 内核模块
我首先检查是否加载了正确的内核:
[email protected] # uname -r
3.16.0-0.bpo.4-amd64
系统当前正在运行向后移植内核。然后我继续加载 NAT 内核模块:
[email protected] # modprobe -v iptable_nat
insmod /lib/modules/3.16.0-0.bpo.4-amd64/kernel/net/netfilter/x_tables.ko
insmod /lib/modules/3.16.0-0.bpo.4-amd64/kernel/net/netfilter/nf_conntrack.ko
insmod /lib/modules/3.16.0-0.bpo.4-amd64/kernel/net/netfilter/nf_nat.ko
insmod /lib/modules/3.16.0-0.bpo.4-amd64/kernel/net/ipv4/netfilter/nf_nat_ipv4.ko
insmod /lib/modules/3.16.0-0.bpo.4-amd64/kernel/net/ipv4/netfilter/ip_tables.ko
insmod /lib/modules/3.16.0-0.bpo.4-amd64/kernel/net/ipv4/netfilter/iptable_nat.ko
[email protected] # lsmod |grep nat
iptable_nat 12646 1
ip_tables 25967 2 iptable_filter,iptable_nat
nf_nat_ipv4 12912 1 iptable_nat
nf_nat 18159 3 ipt_MASQUERADE,nf_nat_ipv4,iptable_nat
nf_conntrack 91426 6 ipt_MASQUERADE,nf_nat,xt_state,nf_nat_ipv4,iptable_nat,nf_conntrack_ipv4
测试NAT
然后我再次配置了 ip 转发和 NAT,因为它在启动后消失了:
[email protected] # echo 1 > /proc/sys/net/ipv4/ip_forward
[email protected] # iptables -t nat -A POSTROUTING -o eth1 -j MASQUERADE
然后我测试从 192.168.0.236 ping 通,结果成功:
[email protected] # ping 10.93.89.1
10.93.89.1 is alive!
结论
问题是无法加载正确的 NAT 内核模块,为了解决这个问题,我必须从 wheezy-backports 安装内核并加载 NAT 内核模块。