这里的环境是archlinux
首先是启用网卡转发功能
|
1
2
|
sudo vim /etc/sysctl.confnet.ipv4.ip_forward=1 |
重启使之生效
再之是安装hostapd这一建ap利器。
|
1
|
sudo pacman -S hosapd |
然后是配置/etc/hostapd.conf 在arch下这个配置文件有非常详细的注释,而且默认的配置已经能满足基本需求,所以修改起来不难也不多。我也忘了我具体改了哪些参数,最后的配置文件主体如下:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
interface=wlan0driver=nl80211logger_syslog=-1logger_syslog_level=2logger_stdout=-1logger_stdout_level=2dump_file=/tmp/hostapd.dumpctrl_interface=/var/run/hostapdctrl_interface_group=0ssid=chlinyhw_mode=gchannel=11beacon_int=100dtim_period=2max_num_sta=255rts_threshold=2347fragm_threshold=2346macaddr_acl=0auth_algs=3ignore_broadcast_ssid=0#wep_default_key=0#wpa=1#wpa_passphrase=secret passphrase#wpa_key_mgmt=WPA-PSK WPA-EAP#wpa_pairwise=TKIP CCMP#rsn_pairwise=CCMP |
这时候用 sudo systemctl start hostapd.service 能够开启hostapd,用ifconfig查看的话会发现多出一个mon.wlan0。但这时手机虽然能扫描到这个AP,连接却获取不到ip地址。所以 我们要先给wlan0赋个地址
|
1
|
ifconfig wlan0 10.5.5.1 netmask 255.255.255.0 |
再然后是开启一个dhcp server服务。我一开始用的是dhcpd,也是网上大多数教程用的,但发现这样能给手机分配到ip地址,但手机却获取不到dns。直到饭桌上跟一师兄谈及,师兄指点可以用dnsmasq才大悟。
关于dnsmasq的配置, arch wiki上已经讲得非常详细了,这里就不再叙述。
最后是在iptables 设置nat
|
1
|
sudo iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE |
至此完成 。
初次搭建ap会比较麻烦,但后面基本只要四句命令而已:
|
1
2
3
4
|
sudo systemctl restart hostapd.servicesudo ifconfig wlan0 10.5.5.1 netmask 255.255.255.0sudo systemctl restart dnsmasq.servicesudo iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE |
也可以通过安装脚本实现:
wget https://codeload.github.com/oblique/create_ap/zip/master
unzip create_ap-master.zip
make install
然后systemctl enable create_ap
注意修改/usr/lib/systemd/system/create_ap.service
[Unit]
Description=Create AP Service
After=network.target
[Service]
Type=simple
ExecStart=/usr/bin/create_ap wlp2s0 enp3s0 devwireless password
KillSignal=SIGINT
Restart=on-failure
RestartSec=5
[Install]
WantedBy=multi-user.target