1. 如何正确配置基本 DHCP 服务器并正确运行它

1. 如何正确配置基本 DHCP 服务器并正确运行它

我使用 hostapd 创建了一个无线 AP,其配置保存在名为 hostapd-test.conf 的文件中。当我使用 sudo hostapd ~/hostapd-test.conf 运行该文件时,我可以通过从我的 Android 手机扫描 Wi-Fi 连接来查看 AP。问题是当我尝试连接到网络时,我没有收到 IP 地址。我怀疑 DHCP 服务器配置错误或我没有正确执行它。我已经按照有关如何设置无线网络的教程操作,但它们最终只会让我更加困惑。

总结一下我的问题:

  1. 如何正确配置基本 DHCP 服务器并正确运行它
  2. 当我设置 wifi 网络时,我应该记住哪些设置或更改,例如更改防火墙设置、设置接口。
  3. 是不是因为网络管理器干扰了我的配置?

答案1

通过 NetworkManager:与有线以太网端口共享无线连接

但是,如果您想手动配置所有内容,我可以告诉您如何操作。从您的问题来看,您似乎已经开始走这条路了。

1. 如何正确配置基本 DHCP 服务器并正确运行它

首先在 WiFi 接口上选择并设置一个静态 IP 地址。假设它是 wlan0,地址为 192.168.44.1,网络掩码为 255.255.255.0(即 /24)。您需要在 NetworkManager 中为 wlan0 设置它,或者在 /etc/network/interfaces 中设置它:

auto wlan0
iface wlan0 inet static
    address 192.168.44.1
    netmask 255.255.255.0
    # Use a smaller MTU if you use VPN or PPPoE on your WAN
    # mtu 1400

如果您在 /etc/network/interfaces 中设置它,则可以使用以下命令启动接口:

sudo ifup wlan0

或者把它放下来:

sudo ifdown wlan0

然后安装 DHCP 服务器:

sudo apt-get install isc-dhcp-server

编辑/etc/dhcp/dhcpd.conf:

default-lease-time 600;
max-lease-time 7200;

option subnet-mask 255.255.255.0;
option broadcast-address 192.168.44.255;

# This is the IP address of our LAN interface
option routers 192.168.44.1;

# Set the DNS server you offer to the hosts here, or you can leave Google's:
option domain-name-servers 8.8.8.8;

# If you want to use a domain name, put it here:
#option domain-name "example.com";

# This is the pool of addresses which will be offered to the clients:
subnet 192.168.44.0 netmask 255.255.255.0 {
    range 192.168.44.100 192.168.44.200;
    # Use a smaller MTU if you use VPN or PPPoE on the router
    # option interface-mtu 1400;
}

启动它:

service isc-dhcp-server restart

2. 启用路由

您有 2 个选择:

echo 1 > /proc/sys/net/ipv4/ip_forward

或者编辑 /etc/sysctl.conf 并设置 net.ipv4.ip_forward=1 然后运行sysctl -p

3.防火墙设置

您需要做的是:

  • 阻止来自 WAN 的您不想要的内容(可选但推荐)
  • 允许从路由器到 WAN 的流量(没什么特别的)
  • 允许 WAN 回复路由器发送的流量(没什么特别的)
  • 允许 DHCP 并从 LAN 接口(在本例中为 wlan0)ping 流量,以便主机可以连接到路由器
  • 允许来自 LAN 并通过 WAN 的流量通过您路由
  • 允许来自 WAN 并通过 WAN 的流量,并且是对您路由的流量的回复,该流量将通过您路由
  • 在机器上进行 NAT,以便 LAN 中的主机可以共享你的 IP 地址访问互联网

仅当您更改了 Ubuntu 中的默认设置时才需要允许 DHCP 和 ping 流量通过防火墙,否则它将正常工作。否则,基本上修复方法是:

sudo iptables -I INPUT -i wlan0 -p udp --dport 67:68 --sport 67:68 -j ACCEPT
sudo iptables -I INPUT -i wlan0 -p icmp --icmp-type 8 -j ACCEPT

(UDP 端口 67 和 68 用于 DHCP,第二个命令是 ICMP 类型 8,又名 ping)

我不知道你对防火墙有多少了解,所以这里做一个简短的介绍。

您可以使用以下方法检查防火墙规则:

sudo iptables -L -n -v --line-numbers

数据包最终会出现 3 种可能的情况,这被称为“链”:

  • 输入是指从机器外部进入到机器的数据(路由数据包除外)
  • OUTPUT 是从你的机器输出的内容(路由数据包除外)
  • FORWARD 是由于路由而从你的机器发出/发出的信息

每个链可以有两种模式:

  • 策略 ACCEPT 意味着,除非有规则与数据包匹配并调用 DROP,否则数据包将通过链
  • 策略 DROP 意味着,除非有规则与数据包匹配并调用 ACCEPT,否则数据包将被链丢弃

通常你想要:

  • OUTPUT 处于 ACCEPT 模式;
  • FORWARD 处于 DROP 模式,并添加规则以允许流量从 LAN 传出到 WAN,同时也允许回复流量从 WAN 传到 LAN;
  • INPUT 处于 DROP 模式,但允许 DHCP 和从 LAN ping,允许回复从路由器发送到 WAN 的流量,并可选择为服务器应用程序打开端口(例如,如果您想运行 Web 服务器)。

另外,您还需要添加一条执行 NAT 的规则。这非常简单,例如,如果 eth0 是您的 WAN 接口:

iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE

您可以使用以下命令列出 NAT 规则:

iptables -t nat -L -n -v --line-numbers

具体需要做什么取决于你的系统配置。通常我会创建一个脚本,清除所有现有规则,然后从头开始填充所有内容。

完整内容,大量评论:

# Set the correct names of the interfaces here:
wan=eth0
lan=wlan0

# Wipe out the current firewall config:
iptables -t filter -F
iptables -t filter -X
iptables -t nat -F
iptables -t nat -X
iptables -t mangle -F
iptables -t mangle -X
iptables -t raw -F
iptables -t raw -X

# Set default policies for the chains in the filter table:
iptables -P INPUT DROP
iptables -P FORWARD DROP
iptables -P OUTPUT ACCEPT

# INPUT (basic client)
# Allow loopback traffic (from us to us)
iptables -A INPUT -i lo -j ACCEPT
# Allow replies to traffic we have sent
iptables -A INPUT -m conntrack --ctstate related,established -j ACCEPT

# INPUT (router)
# Allow DHCP from LAN
iptables -I INPUT -i $lan -p udp --dport 67:68 --sport 67:68 -j ACCEPT
# Allow ping from LAN
iptables -I INPUT -i $lan -p icmp --icmp-type 8 -j ACCEPT


# FORWARD (router)
# Accept any traffic coming from LAN to route it
iptables -A FORWARD -i $lan -j ACCEPT
# Accept replies from WAN to traffic we routed from LAN
iptables -A FORWARD -i $wan -m state --state ESTABLISHED,RELATED -j ACCEPT

# NAT from LAN to WAN
iptables -t nat -A POSTROUTING -o $wan -j MASQUERADE

4. 有关 hostapd 的更多信息

您需要妥善保护您的网络。重要的是将 WPA 设置为 2 (WPA2),并且成对算法仅使用 CCMP (AES) 而不是 TKIP (不安全)。此外,使用复杂的密码。以下是示例配置/etc/hostapd/hostapd.conf

interface=wlan0
driver=nl80211
country_code=US
ssid=Home
hw_mode=g
channel=7
wpa=2
wpa_passphrase=complicated
wpa_key_mgmt=WPA-PSK
wpa_pairwise=CCMP
rsn_pairwise=CCMP
auth_algs=1
macaddr_acl=0

您可能使用的不同的东西是驱动程序、ssid、也许还有 hw_mode、通道,当然还有 wpa_passphrase。实际上不需要 wpa_pairwise。可能值得一看https://wiki.gentoo.org/wiki/Hostapd(是的,不同的发行版,但它们通常都有很好的例子)。

5. 一些额外内容

您可能需要考虑降低延迟和 QoS。通常,以下方法会有所帮助,在最坏的情况下,它不会造成任何损害,也不需要进行调整:

ifconfig wlan0 txqueuelen 50
tc qdisc add dev wlan0 root sfq perturb 10
tc qdisc add dev eth0 root sfq perturb 10

6. IPv6

没有线索 :)

相关内容