hostapd - cllu/.rc GitHub Wiki

设置Hostapd

set up network device name

% cat /etc/udev/rules.d/10-network.rules
SUBSYSTEM=="net", ACTION=="add", ATTR{address}=="f0:de:f1:78:ab:78", NAME="net0"
SUBSYSTEM=="net", ACTION=="add", ATTR{address}=="a0:88:b4:b5:24:78", NAME="wifi0"

SUBSYSTEM=="net", ACTION=="add", ATTR{address}=="00:e0:4c:53:44:58", NAME="net1"
SUBSYSTEM=="net", ACTION=="add", ATTR{address}=="e8:4e:06:15:52:4c", NAME="wifi1"

Set up iptables

# iptables -t nat -A POSTROUTING -o internet0 -j MASQUERADE
# iptables -A FORWARD -i net0 -o internet0 -j ACCEPT
# iptables -A FORWARD -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
# iptables-save > /etc/iptables/iptables.rules

And enable the iptables service sudo systemctl enable iptables.

Set up hostapd

$ cat /etc/hostapd/hostapd.conf
ssid=Eeyore
interface=wifi1
driver=nl80211
logger_stdout=-1
logger_stdout_level=2
hw_mode=g
channel=6
auth_algs=3
max_num_sta=10
wpa=2
wpa_passphrase=he110w0r1d
wpa_pairwise=TKIP CCMP
rsn_pairwise=CCMP

And enable the hostapd service sudo systemctl enable hostapd

Set up DHCP

$ cat /etc/dhcpd.conf
option domain-name-servers 8.8.8.8;
option subnet-mask 255.255.255.0;
option routers 172.27.1.1;
subnet 172.27.1.0 netmask 255.255.255.0 {
  range 172.27.1.150 172.27.1.250;
}

Add a custom systemd service so we can start the DHCP service on given network interface.

[Unit]
Description=IPv4 DHCP server on %I
Wants=network.target
After=network.target

[Service]
Type=forking
PIDFile=/run/dhcpd4.pid
ExecStart=/usr/bin/dhcpd -4 -q -pf /run/dhcpd4.pid %I
KillSignal=SIGINT

[Install]
WantedBy=multi-user.target

systemctl start [email protected]

Usage

iptables开机启动,dhcpdhostapd在需要的时候启动即可。

% cat /etc/netctl/wifi1-Eeyore                                                                                                                                                                                
Description='Static Wifi for soft ap'                                
Interface=wifi1
Connection=wireless                                                             
#AutoWired=yes
IP=static                                                                       
Address=('172.27.1.1/24')

因为某些原因,wifi0端口启动的时候也会顺带启动wifi0端口的wpa_supplicant,导致hostapd无法启动。 这个时候需要手动干掉wpa_supplicant的进程,然后再开启hostapd进程。

$ sudo ip link set wifi1 up
$ sudo ip addr add 172.27.1.1/24 dev wifi1
$ sudo systemctl start [email protected]
$ sudo systemctl start hostapd