usb无线网卡在京东买的,链接为:https://item.jd.com/39864106930.html
一、通过ubuntu的可视化界面设置
二、安装hostapd设置热点
1、下载hostapd源码
官网下载一个稳定版本,http://w1.fi/hostapd/
wget http://w1.fi/releases/hostapd-2.6.tar.gz
tar xzvf hostapd-2.6.tar.gz
cd hostapd-2.6/hostapd
配置hostapd使其获得nl80211驱动的支持,复制hostapd目录下的deconfig文件为.config
cp defconfig .config
vi .config
找到下面这行,去掉#注释
#CONFIG_DRIVER_NL80211=y
安装libnl和libssl库
sudo apt-get install libssl-dev libnl-3-dev libnl-genl-3-dev
如果出现问题,比如下面的头文件问题:
../src/drivers/driver_nl80211.c:17:31: fatal error: netlink/genl/genl.h: 没有那个文件或目录
编辑.config文件,去掉以下注释,然后make
#CONFIG_LIBNL32=y
然后就可以对hostapd进行编译了
make
再安装
sudo make install
配置hostapd
使用源码里的hostapd.conf,修改里里面的interface,改为ubuntu的wlan接口
我的无线接口是这样的: wlx7cdd905274b4
mi@mi-OptiPlex-7040:~/hostapd-2.6/hostapd$ iwconfig
wlx7cdd905274b4 IEEE 802.11 Mode:Master Tx-Power=20 dBm
Retry short long limit:2 RTS thr:off Fragment thr:off
Power Management:on
配置完成后,尝试重启hostapd
sudo hostapd hostapd.conf
如果出现错误
Configuration file: hostapd.conf
Could not read interface wlan0 flags: No such device
nl80211: Driver does not support authentication/association or connect commands
nl80211: deinit ifname=wlan0 disabled_11b_rates=0
Could not read interface wlan0 flags: No such device
nl80211 driver initialization failed.
wlan0: interface state UNINITIALIZED->DISABLED
wlan0: AP-DISABLED
hostapd_free_hapd_data: Interface wlan0 wasn't started
这是因为有其他的网络程序在占用了无线网卡接口,你必须先关闭系统本身的无线网络管理程序network manager
sudo nmcli radio wifi off
sudo rfkill unblock wlan
sudo ifconfig wlan0 10.5.5.1/24 up
然后重新启动
2、安装isc-dhcp-server
命令安装dhcp server的服务
sudo apt-get install isc-dhcp-server
然后编辑文件/etc/default/isc-dhcp-server,其中设置INTERFACES为无线网卡名称,如下所示:
INTERFACES="wlx7cdd905274b4"
然后编辑文件/etc/dhcp/dhcpd.conf,在文本后面添加,注释掉文件里的其他设置
subnet 10.5.5.0 netmask 255.255.255.0 {
range 10.5.5.26 10.5.5.30;
option domain-name-servers 8.8.8.8;
option routers 10.5.5.1;
option broadcast-address 10.5.5.255;
default-lease-time 600;
max-lease-time 7200;
}
可能会出现/var/lib/dhcp/dhcpd.leases无法访问的问题
创建该文件并设置权限
sudo touch /var/lib/dhcp/dhcpd.leases
sudo chmod 777 /var/lib/dhcp/dhcpd.leases
然后启动dhcp server
sudo dhcpd
3、启用internet共享
对于Internet连接共享,我们需要IP转发和IP转换。启用IP转发,使用命令:
echo 1 | sudo tee /proc/sys/net/ipv4/ip_forward
然后配置路由转发表,命令如下,enp0s31f6 为你的internet连接名称,是连外网的网卡
sudo iptables -t nat -A POSTROUTING -o enp0s31f6 -j MASQUERADE
现在就可以上网了
3、编写开机脚本
将hostapd.conf复制到/etc/hostapd/下
sudo cp hostapd.conf /etc/hostapd/
在/etc/rc.local下启用自己编写的开机脚本
1 #!/bin/sh -e
2 #
3 # rc.local
4 #
5 # This script is executed at the end of each multiuser runlevel.
6 # Make sure that the script will "exit 0" on success or any other
7 # value on error.
8 #
9 # In order to enable or disable this script just change the execution
10 # bits.
11 #
12 # By default this script does nothing.
13
14 /etc/hostapd/CreateAp.sh
15
16 exit 0
开机脚本内容,CreateAp.sh
1 #!/bin/sh
2
3 iwconfig >> /tmp/wqy
4
5 sudo nmcli radio wifi off
6 sudo rfkill unblock wlan
7 sudo ifconfig wlx7cdd905274b4 10.5.5.1/24 up
8 sleep 3
9
10 sudo hostapd /etc/hostapd/hostapd.conf
11 sudo service isc-dhcp-server restart
12 echo 1| sudo tee /proc/sys/net/ipv4/ip_forward
13 sudo iptables -t nat -A POSTROUTING -o wlx7cdd905274b4 -j MASQUERADE