我想将嵌入式设备配置为无线路由器,并且我希望最好使用网络配置文件来实现它。
我有两个 *.network 文件、一个有线 WAN 接口和一个无线 LAN 接口,并且我希望 LAN 流量能够访问 WAN。
# cat /etc/systemd/network/wired.network
[Match]
Name=eth0
[Network]
DHCP=ipv4
IPForward=yes
IPMasquerade=yes
与无线路由器一样,无线 LAN 网络也提供 DHCP。我使用了 systemd 集成的 DHCP 服务器。
# cat /etc/systemd/network/wireless.network
[Match]
Name=wlan0*
[Network]
Description = Wireless IP interface
DHCPServer= yes
[Address]
Address = 192.168.128.1/24
Broadcast = 192.168.128.255
[DHCPServer]
PoolOffset = 10
PoolSize = 40
EmitDNS = yes
DNS = 8.8.8.8, 8.8.4.4
DefaultLeaseTimeSec = 600
MaxLeaseTimeSec = 7200
[Route]
我认为两个网络之间应该有 NAT,这对于无线路由器来说很常见,但这不是明确的要求。
据我目前所知,有线接口正确连接到WAN,无线接入点也提供DHCP地址。但如果我连接到 WiFi AP,我无法访问 WAN(互联网)。
以下是一些网络接口状态:
# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default
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 00:26:32:f0:03:21 brd ff:ff:ff:ff:ff:ff
inet 192.168.178.107/24 brd 192.168.178.255 scope global dynamic eth0
valid_lft 4650sec preferred_lft 4650sec
inet6 fe80::226:32ff:fef0:321/64 scope link
valid_lft forever preferred_lft forever
3: sit0@NONE: <NOARP> mtu 1480 qdisc noop state DOWN group default
link/sit 0.0.0.0 brd 0.0.0.0
4: wlan0wext: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
link/ether 00:1d:43:40:0d:04 brd ff:ff:ff:ff:ff:ff
inet 192.168.128.1/24 brd 192.168.128.255 scope global wlan0wext
valid_lft forever preferred_lft forever
inet6 fe80::21d:43ff:fe40:d04/64 scope link
valid_lft forever preferred_lft forever
以及路线状态:
# ip route
default via 192.168.178.1 dev eth0 proto dhcp src 192.168.178.107 metric 1024
192.168.128.0/24 dev wlan0wext proto kernel scope link src 192.168.128.1
192.168.178.0/24 dev eth0 proto kernel scope link src 192.168.178.107
192.168.178.1 dev eth0 proto dhcp scope link src 192.168.178.107 metric 1024
# ip route show table local
broadcast 127.0.0.0 dev lo proto kernel scope link src 127.0.0.1
local 127.0.0.0/8 dev lo proto kernel scope host src 127.0.0.1
local 127.0.0.1 dev lo proto kernel scope host src 127.0.0.1
broadcast 127.255.255.255 dev lo proto kernel scope link src 127.0.0.1
broadcast 192.168.128.0 dev wlan0wext proto kernel scope link src 192.168.128.1
local 192.168.128.1 dev wlan0wext proto kernel scope host src 192.168.128.1
broadcast 192.168.128.255 dev wlan0wext proto kernel scope link src 192.168.128.1
broadcast 192.168.178.0 dev eth0 proto kernel scope link src 192.168.178.107
local 192.168.178.107 dev eth0 proto kernel scope host src 192.168.178.107
broadcast 192.168.178.255 dev eth0 proto kernel scope link src 192.168.178.107
我没有找到 [Route] 部分的很多示例,而且我对路由只有基本的了解。
问候, 伊兹托克·杰拉斯
答案1
我发现上面的 .network 文件缺少伪装。以下修复:
iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
看来我的 systemd 版本是在没有 iptables 支持的情况下构建的。使用选项再次构建它'libiptc'
修复了这个问题。我需要添加
IPForward=yes
IPMasquerade=yes
也到 wireless.network 文件。
答案2
考虑RHEL 5 & 6
的153.xx
是你的System IP
临时添加路线
ip route add 192.168.10.0/24 via 153.254.92.42 dev eth0
永久路由添加
echo "192.168.10.0/24 via 153.254.92.XX" >> /etc/sysconfig/network-scripts/route-eth1
为了RHEL 7
临时添加路线
ip route add 192.168.20.0/29 via 153.254.92.XX dev enp00s0f0
永久路由添加
echo "192.168.10.0/24 via 153.254.92.XX" >> /etc/sysconfig/network-scripts/ifcfg-enp00s0f0
答案3
问题不在于路由配置,而是缺少 iptables 配置。