sudo apt install hostapd
interface=p2p0 #wifi的网卡名称
driver=nl80211
ssid=TP-LINK_TEST #热点名称
channel=10
hw_mode=g
wpa=3
wpa_passphrase=12345678 #无线密码
wpa_key_mgmt=WPA-PSK
wpa_pairwise=TKIP
ignore_broadcast_ssid=0 #0:显示热点名称 1:隐藏热点名称
修改/etc/init.d/hostapd或者/etc/default/hostapd中的DAEMON_CONF,修改内容如下:
DAEMON_CONF="/etc/hostapd/hostapd.conf"
sudo systemctl start hostapd.service #启动hostapd服务,仅本次有效。
sudo systemctl restart hostapd.service #重启hostapd服务,仅本次有效。
sudo systemctl stop hostapd.service #停止hostapd服务,仅本次有效。
sudo systemctl status hostapd.service #查看hostapd状态、log
sudo systemctl enable hostapd.service #使能hostapd服务,下次重启后自动start
sudo systemctl disable hostapd.service #失能hostapd服务,下次重启后不在启动hostapd
hostapd /etc/hostapd/hostapd.conf
。如果有提示如下错误,请检查配置的wifi网卡释放已经被占用,下面就是wlan0已经被wpa_supplicant占用而报的错误信息。// 一般都会出现以下错误
Configuration file: hostapd.conf
nl80211: Could not configure driver mode
nl80211 driver initialization failed.
hostapd_free_hapd_data: Interface wlan0 wasn't started
成功搭建hostapd后,手机就可以搜索到wifi热点,但是连接后报错或者提示无法分配IP。下面利用isc-dhcp-server搭建一个dhcp的服务器,实现为自动为接入的设备动态分配ip地址。
sudo apt-get install isc-dhcp-server
INTERFACES="p2p0" #多个网卡名称之间用空格隔开。如:INTERFACES="eth0 eth1"
修改 /etc/dhcp/dhcpd.conf指定要分配的ip地址池信息。修改如下:
# option definitions common to all supported networks...
option domain-name "example.org";
#原文件中下面这一行注释掉。
# option domain-name-servers ns1.example.org, ns2.example.org;
/etc/dhcp/dhcpd.conf中增加如下内容,注意IP地址请根据自己的需要修改。
# DHCP server to understand the network topology.
subnet 192.168.100.0 netmask 255.255.255.0 {
range 192.168.100.100 192.168.100.200;
option routers 192.168.100.1;
option subnet-mask 255.255.255.0;
option broadcast-address 192.168.100.255;
option domain-name-servers 8.8.8.8;
option ntp-servers 192.168.100.1;
option netbios-name-servers 192.168.100.1;
option netbios-node-type 8;
}
多个网卡时需要添加多个subnet,并且每组信息不能在同一网段。另外在option domain-name-servers属性中配置多个dns时,用英文逗号隔开。
auto p2p0
iface p2p0 inet static
address 192.168.100.1
netmask 255.255.255.0
network 192.168.100.0
broadcast 192.168.100.255
配置完成后重启设备生效。
sudo vim /etc/netplan/01-network-manager-all.yaml
#修改时注意yaml文件的格式要求。
network:
wifis:
p2p0:
dhcp4: no #指定是否需要进行DHCP,4指的是ipv4
addresses: [192.168.100.1/24] #IP地址
optional: true
gateway4: 192.168.100.1 #默认网关,如果不需要网关地址,这一行可以不要
version: 2
renderer: NetworkManager #对于无NetworkManager可选择networkd
配置完成后可通过下面命令使配置生效。sudo netplan apply
sudo service isc-dhcp-server restart
,通过sudo netstat -uap
命令查看DHCP服务是否正常启动,如果出现有dhcpd的字样说明服务启动成功。ps -ef | grep dhcpd
查看有没有相关的进程已经启动。如果没有启动,请仔细排查相关配置,看是否有配置错误的地方。systemctl status isc-dhcp-server
查看log提示如下信息:dhcpd -f -4 -pf /run/dhcp-server/dhcpd.pid -cf $CONFIG_FILE $INTERFACES
(code=exited, status=1/FAILURE)
Main PID: 278 (code=exited, status=1/FAILURE)
May 15 15:56:32 localhost.localdomain sh[278]: Not configured to listen on any i
nterfaces!
May 15 15:56:32 localhost.localdomain sh[278]: If you think you have received th
is message due to a bug rather
这个错误是因为dhcpd在开机启动的时候没有检测网络接口导致的,这里可以将isc-dhcp-server的服务设置成守护进程,在dhcpd未启动的情况下会重复尝试重启,这样当网络接口正常后系统就可正常启动dhcpd程序。修改/lib/systemd/system/isc-dhcp-server.service,在"[Service]"下添加下面内容:
[Service]
Restart=on-failure #表示进程退出或崩溃需要重启
RestartSec=10s #表示重启的时间间隔
经过上面2步后,已经可以通过手机连接到热点上了,但是这时手机还不能利用这个热点进行上网。下面利用nat实现热点共享eth0的网络。注意:下面配置需要确定内核中已开启iptables和nat功能。
net.ipv4.ip_forward=1
的注释。然后执行sudo sysctl -p
使其生效。sudo iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
sudo iptables -t nat -L #查看创建的规则
sudo iptables-save > /etc/iptables.rules #可将上面创建的规则保存到/etc/iptables.rules中。
sudo iptables-restore < /etc/iptables.rules #加载/etc/iptables.rules中的规则。
pre-up iptables-restore < /etc/iptables.rules
# post-down iptables-save > /etc/iptables.rules #添加这句会在网卡down的时候自动保存规则。
对于ubuntu18及后续版本,因为没有/etc/network/interfaces文件所以可以通过在/etc/network/if-pre-up.d下添加脚本来实现,例如:
sudo vim /etc/network/if-pre-up.d/iptables #创建一个脚本
#添加下面内容。
#!/bin/bash
iptables-restore < /etc/network/iptables.up.rules
#为脚本添加执行权限
sudo chmod +x /etc/network/if-pre-up.d/iptables