多个 NIC、多个子网和多个网关

多个 NIC、多个子网和多个网关

现在是时候向专业人士请教了,因为我已经阅读了所有已回答的问题,并在论坛上尝试了无数针对相关问题的建议,但似乎无法做到这一点。

问题:如何允许“wlan0”的用户通过“eth0”连接到互联网?

系统有三张网卡。两张无线“wlan0”和“wlan1”以及一张 LAN“eth0”。最终目标是让“wlan1”成为网状网络的一部分,该网络在某处有一个节点连接到互联网,“wlan0”用于托管本地用户,eth0 也可以作为连接到互联网的可选有线管道。每个接口都必须有自己的子网。为了简单起见,我们暂时将网状部分放在“wlan1”上,只关注让两个不同的子网“wlan0”和“eth0”相互通信。

目前的情况是这样的。ssid 信标按预期广播,用户被要求输入密钥,DHCP 分配预期范围内的 IP 地址,但没有互联网连接。

这是我接触过的所有配置文件。

在 /etc/网络/接口:

# The loopback network interface
auto lo
iface lo inet loopback

# Wired LAN
allow-hotplug eth0
iface eth0 inet dhcp

# Wireless Users
allow-hotplug wlan0
iface wlan0 inet static
    address 192.168.2.100
    netmask 255.255.255.0
    gateway 192.168.2.254

在 /etc/default/hostapd 中:

DAEMON_CONF="/etc/hostapd/hostapd.conf"

/etc/hostapd/hostapd.conf:

interface=wlan0
driver=nl80211
country_code=US
ssid=YYYYYYYY
hw_mode=g
channel=6
wpa=2
wpa_passphrase=XXXXXXXX

/etc/sysctl.conf:

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

/etc/default/isc-dhcp 服务器:

INTERFACES="wlan0"

/etc/dhcp/dhcpd.conf

ddns-update-style none;
option domain-name "unixmen.local";
option domain-name-servers server.unixmen.local;
default-lease-time 600;
max-lease-time 7200;
authoritative;
log-facility local7;

subnet 192.168.2.0 netmask 255.255.255.0 {
  range 192.168.2.2 192.168.2.51;
  option subnet-mask 255.255.255.0;
  option routers 192.168.2.254;
  option broadcast-address 192.168.2.255;
}

/etc/rc.local:

#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.

#!/bin/sh

PATH=/usr/sbin:/sbin:/bin:/usr/bin

#
# delete all existing rules.
#
iptables -F
iptables -t nat -F
iptables -t mangle -F
iptables -X

# Always accept loopback traffic
iptables -A INPUT -i lo -j ACCEPT

# Allow established connections, and those not coming from the outside
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
iptables -A INPUT -m state --state NEW ! -i wlan0 -j ACCEPT
iptables -A FORWARD -i wlan0 -o eth0 -m state --state ESTABLISHED,RELATED -j     ACCEPT
iptables -I FORWARD -i eth0 -o wlan0 -s 192.168.1.0/24 -d 192.168.2.0/24 -j ACCEPT
iptables -I FORWARD -i wlan0 -o eth0 -s 192.168.2.0/24 -d 192.168.1.0/24 -j ACCEPT

# Allow outgoing connections from the LAN side.
iptables -A FORWARD -i eth0 -o wlan0 -j ACCEPT

# Masquerade.
iptables -t nat -A POSTROUTING -o wlan0 -j MASQUERADE

# Don't forward from the outside to the inside.
iptables -A FORWARD -i wlan0 -o wlan0 -j REJECT

# Drop outside traffic except ssh
iptables -A INPUT -p tcp --dport ssh -j ACCEPT -i wlan0
iptables -A INPUT -j DROP -p tcp -i wlan0

# Enable routing.
echo 1 > /proc/sys/net/ipv4/ip_forward

exit 0

#ip 路由显示

default via 192.168.1.1 dev eth0
192.168.1.0/24 dev eth0  proto kernel  scope link  src 192.168.1.3
192.168.2.0/24 dev wlan0  proto kernel  scope link  src 192.168.2.100

#路线

Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use     Iface
0.0.0.0         192.168.1.1     0.0.0.0         UG    0      0        0 eth0
192.168.1.0     0.0.0.0         255.255.255.0   U     0      0        0 eth0
192.168.2.0     0.0.0.0         255.255.255.0   U     0      0        0 wlan0

#iptables -nvL

Chain INPUT (policy DROP 49 packets, 7973 bytes)
 pkts bytes target     prot opt in     out     source               destination
  803 69528 f2b-sshd   tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            multiport dports 22
   47  3384 ACCEPT     all  --  lo     *       0.0.0.0/0            0.0.0.0/0
  820 71137 ACCEPT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            state RELATED,ESTABLISHED
  122 12775 ACCEPT     all  --  !wlan0 *       0.0.0.0/0            0.0.0.0/0            state NEW
    0     0 ACCEPT     tcp  --  wlan0  *       0.0.0.0/0            0.0.0.0/0            tcp dpt:22
    0     0 DROP       tcp  --  wlan0  *       0.0.0.0/0            0.0.0.0/0

Chain FORWARD (policy DROP 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination
    0     0 ACCEPT     all  --  wlan0  eth0    192.168.2.0/24       192.168.1.0/24
    0     0 ACCEPT     all  --  eth0   wlan0   192.168.1.0/24       192.168.2.0/24
    0     0 ACCEPT     all  --  wlan0  eth0    0.0.0.0/0            0.0.0.0/0            state RELATED,ESTABLISHED
    0     0 ACCEPT     all  --  eth0   wlan0   0.0.0.0/0            0.0.0.0/0
    0     0 REJECT     all  --  wlan0  wlan0   0.0.0.0/0            0.0.0.0/0            reject-with icmp-port-unreachable

Chain OUTPUT (policy ACCEPT 721 packets, 115K bytes)
 pkts bytes target     prot opt in     out     source               destination

Chain f2b-sshd (1 references)
 pkts bytes target     prot opt in     out     source               destination
    0     0 REJECT     all  --  *      *       212.83.191.97        0.0.0.0/0            reject-with icmp-port-unreachable
    0     0 REJECT     all  --  *      *       212.129.6.17         0.0.0.0/0            reject-with icmp-port-unreachable
    0     0 REJECT     all  --  *      *       198.11.246.172       0.0.0.0/0            reject-with icmp-port-unreachable
    0     0 REJECT     all  --  *      *       193.104.41.54        0.0.0.0/0            reject-with icmp-port-unreachable
  803 69528 RETURN     all  --  *      *       0.0.0.0/0            0.0.0.0/0

#ifconfig

eth0      Link encap:Ethernet  HWaddr XXXXXXXXXXXXXXXXXXXX
          inet addr:192.168.1.3  Bcast:192.168.1.255  Mask:255.255.255.0
          inet6 addr: XXXXXXXXXXXXXXXXXXXXXXX/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:1271 errors:0 dropped:0 overruns:0 frame:0
          TX packets:877 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:131595 (131.5 KB)  TX bytes:150077 (150.0 KB)
          Interrupt:20 Memory:f7100000-f7120000

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:229 errors:0 dropped:0 overruns:0 frame:0
          TX packets:229 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:16828 (16.8 KB)  TX bytes:16828 (16.8 KB)

wlan0     Link encap:Ethernet  HWaddr XXXXXXXXXXXXXXXXX
          inet addr:192.168.2.100  Bcast:192.168.2.255  Mask:255.255.255.0
          inet6 addr: XXXXXXXXXXXXXXXXXXXXX/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:222 errors:0 dropped:0 overruns:0 frame:0
          TX packets:266 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:12115 (12.1 KB)  TX bytes:26066 (26.0 KB)

#lspci

00:19.0 Ethernet controller [0200]: Intel Corporation Ethernet Connection (3) I218-V [8086:15a3] (rev 03)
        Subsystem: Intel Corporation Device [8086:2057]
        Kernel driver in use: e1000e

02:00.0 Network controller [0280]: Intel Corporation Wireless 7265 [8086:095a] (rev 59)
        Subsystem: Intel Corporation Dual Band Wireless-AC 7265 [8086:9010]
        Kernel driver in use: iwlwifi

这应该是我能想到的调试所需的一切。非常感谢你抽出时间。

答案1

iptables 妨碍了。我不太清楚在哪里。好消息是,您不需要 iptables 来进行此设置。UFW 可以直接完成所有这些操作。

apt-get remove --purge iptables

注意:这个 - PURGE 命令还将完全删除 UFW 以及对 IPTABLES 的任何其他引用包,包括所有 *.CONF 文件,请将您想要保留的所有内容保存在新文件中 - 这是大红色重置按钮 - 使用时请极其小心

apt-get install ufw
ufw allow ssh

编辑以下配置文件:

/etc/ufw/sysctl.conf:

uncomment net/ipv4/ip_forward=1 on line 8

/etc/ufw/before.rules:将以下内容放在文件中最后一个 COMMIT 之后,否则您将收到 *filter 错误。请记住在下面显示的伪装块之后添加另一个 COMMIT,否则什么都不会发生!

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

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

COMMIT

UFW 规则:

ufw route allow in on wlan0 out on eth0 from 192.168.2.0/24

/etc/dhcp/dhcpd.conf:

ddns-update-style none;
option domain-name-servers 192.168.1.1;
default-lease-time 600;
max-lease-time 7200;
authoritative;
log-facility local7;

# wlan0
subnet 192.168.2.0 netmask 255.255.255.0 {
  range 192.168.2.2 192.168.2.51;
  option routers 192.168.2.1;
}

在 /etc/网络/接口:

# The loopback network interface
auto lo
iface lo inet loopback

# Wired LAN
allow-hotplug eth0
iface eth0 inet dhcp

# Wireless Users
allow-hotplug wlan0
iface wlan0 inet static
    address 192.168.2.100
    netmask 255.255.255.0
    broadcast 192.168.2.255

删除或注释掉 /etc/rc.local 中除 exit 0 之外与此主题相关的所有内容。

/etc/default/isc-dhcp 服务器:

INTERFACES="wlan0"

/etc/sysctl.conf:您可以在此处注释掉所有内容。许多说明都要求您取消注释“net.ipv4.ip_forward=1”。我们已经在 /etc/ufw/sysctl.conf 中启用了此功能,因此此处不再需要它。

在 /etc/default/hostapd 中:

DAEMON_CONF="/etc/hostapd/hostapd.conf"

/etc/hostapd/hostapd.conf:

interface=wlan0
driver=nl80211
country_code=US
ssid=YYYYYYYY
hw_mode=g
channel=6
wpa=2
wpa_passphrase=XXXXXXXX

启用 UFW

ufw enable

重新启动服务器以使更改生效

reboot

检查一切是否正常:wlan0 应在指定的 ssid 上广播,通过指定的密码允许用户,在指定的范围内发出 dhcp 地址,并允许这些用户通过 eth0 访问互联网。此外,如果您运行“ufw status”,它将如下所示:

Status: active

To                         Action      From
--                         ------      ----
22                         ALLOW       Anywhere
22 (v6)                    ALLOW       Anywhere (v6)

Anywhere on eth0           ALLOW FWD   192.168.2.0/24 on wlan0

我希望这对你们中的一些人有所帮助,因为关于如何完成这一步有很多不同的说明,并且我在尝试遵循它们时遇到了很多问题。

相关内容