ubuntu下使用hostapd建立wifi热点

hostapd是一个linux下的wifi管理程序

安装环境:ubuntu 16.04 LTS

 

第一部分 安装和配置hostapd

下载hostapd源码

hostapd可以直接使用命令安装,也可以从源码编译安装。这里我使用从hostapd的源码安装配置,有两种下载方式

 一是从官方的git仓库里获取hostapd最新的开发版

git clone git://w1.fi/srv/git/hostap.git
cd hostap/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

然后就可以对hostapd进行编译了

make

 再安装

sudo make install

 

在编译过程中,可能会遇到几个问题

问题1:

driver_nl80211.c:21:31: warning: netlink/genl/genl.h: No such file or directory
driver_nl80211.c:22:33: warning: netlink/genl/family.h: No such file or directory
driver_nl80211.c:23:31: warning: netlink/genl/ctrl.h: No such file or directory
driver_nl80211.c:24:25: warning: netlink/msg.h: No such file or directory
driver_nl80211.c:25:26: warning: netlink/attr.h: No such file or directory

解决办法:

安装libnl和libssl库

sudo apt-get install libssl-dev libnl-3-dev libnl-genl-3-dev

如果还是有问题,编辑.config文件,去掉以下语句的注释,然后再次make。

#CONFIG_LIBNL32=y

 

配置hostapd

在下载的hostapd目录中有默认的配置文件hostapd.conf,但配置太多,对于初学者来说不是太好理解,先自己创建一个简单的配置文件hostapd-minimal.conf,对hostapd的功能进行验证。

编辑hostapd-minimal.conf文件

#wlan0为你的无线网卡名称
interface=wlan0
driver=nl80211
ssid=test
hw_mode=g
channel=1

完成配置后,可以使用命令尝试开启hostapd

sudo hostapd hostapd-minimal.conf

可能会遇到如下错误

Configuration file: hostapd-minimal.conf
nl80211: Could not configure driver mode
nl80211: deinit ifname=wlp9s0b1 disabled_11b_rates=0
nl80211 driver initialization failed.
wlp9s0b1: interface state UNINITIALIZED->DISABLED
wlp9s0b1: AP-DISABLED 
hostapd_free_hapd_data: Interface wlp9s0b1 wasn't started

这是因为有其他的网络程序在占用了无线网卡接口,你必须先关闭系统本身的无线网络管理程序network manager.

sudo nmcli radio wifi off
sudo rfkill unblock wlan
sudo ifconfig wlan0 10.5.5.1/24 up

然后再打开hostapd。如下所示,表示你已经成功启动了hostapd

Configuration file: hostapd-minimal.conf
Using interface wlan0 with hwaddr 68:94:23:8b:88:a3 and ssid "test"
wlan0: interface state UNINITIALIZED->ENABLED
wlan0: AP-ENABLED 

接下来用手机连接wifi,这时候就可以搜到hostapd的wifi “test”。但是手机连不上,这是因为电脑没有安装dhcp服务,不能给连接的终端分配ip地址

 

第二部分 安装isc-dhcp-server

首先使用命令安装dhcp server服务

sudo apt-get install isc-dhcp-server
然后编辑文件/etc/default/isc-dhcp-server,其中设置INTERFACES为无线网卡名称,如下所示:
INTERFACES="wlan0"
然后编辑文件/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;
}

启动dhcp server

sudo dhcpd

 

第三部分 启用internet共享

对于Internet连接共享,我们需要IP转发和IP转换。启用IP转发,使用命令:

 

echo 1 | sudo tee /proc/sys/net/ipv4/ip_forward

 然后配置路由转发表,命令如下,ppp0为你的internet连接名称

sudo iptables -t nat -A POSTROUTING -o ppp0 -j MASQUERADE

 

现在手机可以连接wifi,能够上网,说明已经配置成功。但是重启电脑后,很多步骤都需要重新来一遍,很麻烦。我们可以写一个启动脚本

#!/bin/bash
sudo nmcli radio wifi off
sudo rfkill unblock wlan
sudo ifconfig wlan0 10.5.5.1/24 up
sleep 4

sudo service isc-dhcp-server restart
echo 1| sudo tee /proc/sys/net/ipv4/ip_forward
sudo iptables -t nat -A POSTROUTING -o ppp0 -j MASQUERADE
sudo hostapd hostapd目录/hostapd-minimal.conf

 

                                   

 

posted @ 2016-12-16 13:37  南乡子-  阅读(10199)  评论(0编辑  收藏  举报