当前位置: 首页>前端>正文

ubuntu新建ap热点并分享

测试环境ubuntu16,只有一台笔记本电脑,不插网线,无线网卡既连wifi,又作为热点

1.方法1 直接手动新建ap热点

参考https://jingyan.baidu.com/article/ea24bc39b03fc6da62b331f0.html
https://jingyan.baidu.com/article/363872ecd8f35d6e4ba16f97.html
亲测,发现电脑如果没有连有线,按照以上步骤并不能生成wifi热点,请各位指点

2.方法2 使用create_ap工具新建热点

2.1安装依赖hostapd和create_ap

sudo apt install build-essential hostapd
git clone https://github.com/oblique/create_ap
cd create_ap
sudo make install 

2.2获取 wlan/wireless 名字并用create_ap启动热点

然后

ifconfig

获取网卡的名称每台电脑的网卡名称不一定相同,我这里的有线网卡名称是 enp1s0 无线网卡名称是 wlp7s0,具体名称需要自己进行获取
wlan:enp4s0
wireless: wlp3s0
其中e开头代表有有线,w开头代表无线
测试create_ap 是否工作正常

create_ap 基本命令格式:

create_ap wlan0 eth0 MyAccessPoint MyPassPhrase

本机指令

create_ap wlp3s0 enp4s0 note-ubuntu 12345678

新建成功打印ap0:AP-ENABLED如下图,并且能在别的电脑或手机上搜到wifi名称为note-ubuntu。手机连接测试发现可以上网
ubuntu新建ap热点并分享,在这里插入图片描述,第1张

注意如果出现
ERROR: Your adapter can not be a station (i.e. be connected) and an AP at the same time
并且关闭WiFi后出现
WARN: Your adapter does not fully support AP virtual interface, enabling --no-virt
可能为网卡不支持新建热点

2.3create_ap其他命令

(1)查看创建的热点

create_ap --list-running

出现结果

8323 wlp3s0(ap0)

(2)查看连接在 ap 上的设备

create_ap --list-clients 查看创建的热点前面的编号 如8323

https://blog.csdn.net/freewebsys/article/details/126297257

(3)开启关闭热点

sudo systemctl start create_ap # 开启 wifi 热点
sudo systemctl stop create_ap # 关闭 wifi 热点

3.方法3用hostapd和dhcp建立wifi热点

3.1 hostapd和udhcpd工具简介

(1)hostapd简介
用过WIFI的应该对wpa_supplicant这个工具有一定的了解。wpa_supplicant用于wifi连接AP、验证密码等功能,实现的是wifi sta相关功能。而hostapd工具用于实现WIFI AP相关的功能。

hostapd包括IEEE 802.11接入点管理(认证/关联)、IEEE 802.1X/WPA/WPA2 Authenticator、EAP服务器和RADIUS认证服务器功能。它可以使用各种配置选项构建,例如,一个独立的AP管理解决方案或一个支持多种EAP方法的RADIUS认证服务器。
通俗来讲作用就是将WIFI模块模拟成热点,供其他设备连接。
参考 http://w1.fi/wpa_supplicant/devel/

(2)udhcpd简介
hostapd可以理解为用于搭建物理层。一个WIFI设备连接到热点,就像一个网线的设备,插入到了路由器的lan口。而我们Linux设备模拟成热点,就相当于实现了路由器的功能。Linux热点运行这hostapd程序,那么其他wifi终端设备就可以连接它。我们知道,将设备用网线连接到路由器是需要手动配置IP地址或者自动的获取IP地址的。之所以设备能从路由器获取IP地址,就是因为路由器中运行DHCP服务,可以动态的给连接的设备分配IP地址。同样我们用Linux模拟成热点,也需要为其他连接热点的设备提供IP地址和DNS等配置,这样其他WIFI终端设备才真正的和热点组成了局域网。为了实现DHCP服务,所以Linux上需要运行udhcpd这个程序。在嵌入式上udhcpd是busybox提供的一个应用程序。

3.2 安装hostapd 和dhcp工具

sudo apt install hostapd 
sudo apt install isc-dhcp-server

3.3配置和启动hostapd

(1)配置hostapd
先自己创建一个简单的配置文件hostapd-minimal.conf,对hostapd的功能进行验证,将这个hostapd-minimal.conf 文件拷贝到目录/etc/hostapd/下
并且编辑hostapd-minimal.conf文件如下

#wlan0为你的无线网卡名称,用ifconfig查,注意无线为w开头
interface=wlp3s0
#??driver怎么查?
driver=nl80211
#热点名字
ssid=note-ubuntu
hw_mode=g
channel=1
#使用热点密码验证
wpa=1
#热点密码
wpa_passphrase=12345678

(2)启动hostapd

sudo hostapd /etc/hostapd/hostapd-minimal.conf 

出现如下显示

Configuration file: /etc/hostapd/hostapd-minimal.conf
Using interface wlp3s0 with hwaddr 84:ef:18:cc:32:4c and ssid "note-ubuntu"
wlp3s0: interface state UNINITIALIZED->ENABLED
wlp3s0: AP-ENABLED 
wlp3s0: STA 3c:6a:48:88:c8:10 IEEE 802.11: disassociated
wlp3s0: interface state ENABLED->DISABLED
wlp3s0: AP-DISABLED 
nl80211: deinit ifname=wlp3s0 disabled_11b_rates=0

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

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

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

Using interface wlp3s0 with hwaddr 84:ef:18:cc:32:4c and ssid "note-ubuntu"
wlp3s0: interface state UNINITIALIZED->ENABLED
wlp3s0: AP-ENABLED

在手机上发现出现了热点,并且输入密码后能连接上这个wifi。
但是手机上发现不能上网,ubuntu上没有联网。想要在手机上连上这个热点并且能上网还需要继续进行配置。

3.4配置和启动dhcp

(1)配置dhcp
1.编辑文件/etc/default/isc-dhcp-server
将INTERFACES项改为

INTERFACES="wlp3s0"

wlp3s0对应无线网卡名字。
2.然后编辑文件/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;
}

3.编辑
(2)启动dhcp server

sudo dhcpd

出现

Config file: /etc/dhcp/dhcpd.conf
Database file: /var/lib/dhcp/dhcpd.leases
PID file: /var/run/dhcpd.pid
Wrote 0 leases to leases file.
Listening on LPF/wlp3s0/84:ef:18:cc:32:4c/10.5.5.0/24
Sending on   LPF/wlp3s0/84:ef:18:cc:32:4c/10.5.5.0/24

No subnet declaration for enp4s0 (no IPv4 addresses).
** Ignoring requests on enp4s0.  If this is not what
   you want, please write a subnet declaration
   in your dhcpd.conf file for the network segment
   to which interface enp4s0 is attached. **

Sending on   Socket/fallback/fallback-net

表示成功了。并且通过sudo netstat -uap命令查看DHCP服务是否正常启动,如果出现有dhcpd的字样说明服务启动成功。

其他出现

Can't open /var/lib/dhcp/dhcpd.leases for append.

修改权限后执行可以成功

sudo chmod 777  /var/lib/dhcp/dhcpd.leases

3.4 启用internet共享

(1)启动路由转发,

sudo iptables -A FORWARD -o wlp3s0 -i wlp3s0 -s 192.168.5.0/24 -m conntrack --ctstate NEW -j ACCEPT
sudo iptables -A FORWARD -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
sudo iptables -t nat -F POSTROUTING
sudo iptables -t nat -A POSTROUTING -o wlp3s0 -j MASQUERADE

删除/etc/sysctl.conf中下面一行中的’#',解除net.ipv4.ip_forward=1的注释。然后执行sudo sysctl -p使其生效。或者调用

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

再执行

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

实验后发现如果连上了wifi,就没有了热点。看来还是需要看create_ap 脚本是怎么实现的。

参考文献
https://www.cnblogs.com/liyam/p/6186527.html


https://www.xamrdz.com/web/29f1848640.html

相关文章: