linux无线网卡模拟ap,ubuntu自启动无线网卡AP模式

自己手头有一台ubuntu主机,懒得专门接显示器和键盘使用,但是手上仅有的电脑是macbook和一个win平板,都没有网线接口,无法直接连接ubuntu主机,手头有几个TP的722N无线网卡,就有个想法:

ubuntu主机连接有线网络。

ubuntu主机使用722N无线网卡建立AP共享有线网络。

macbook连接ubuntu共享AP,既能访问ubuntu主机也能共享使用有线网络。

ubuntu主机仅需要第一次配置时连接显示器和键盘来配置。只要网络和硬件(主要是网卡)没有什么变动,基本不用再次配置。

还有一个方案是设置无线网卡自动连接家里的无线路由器,方法更简单。考虑可能出现我带着ubuntu主机和macbook出门使用,所以ubuntu主机自动建立AP的方式是更好的选择。

下面的操作需要ubuntu中已安装hostapd和isc-dhcp-server,可以通过apt-get安装,过程简单不多说了。

下面所有操作都是在ubuntu主机上操作。

自动化配置

主要是需要修改/etc/network/interfaces。

eth0为ubuntu机器的有线接口。

wlan9为ubuntu机器无线接口。

sudo vim /etc/network/interfaces

内容如下(请仔细阅读注释,详细含义请参看手册man interfaces):

# This file describes the network interfaces available on your system

# and how to activate them. For more information, see interfaces(5).

# The loopback network interface

auto lo

iface lo inet loopback

# The primary network interface

auto eth0

#有线接口静态配置,也可以使用dhcp方式,请参考手册,dhcp的话下面到默认路由都可以不要

iface eth0 inet static

address 10.0.0.204

geteway 10.0.0.1

netmask 255.255.255.0

#域名服务器,看需求配置,就配只114.114.114.114也行

dns-nameservers 8.8.8.8 10.0.0.1

#默认路由

up ip route add default via 10.0.0.1

#热插拔自动启动模式,即如果此接口存在的话系统会自动ifup此接口

allow-hotplug wlan9

#无线接口静态配置,此处使用静态IP,因为要配合dhcp server配置,见附录

iface wlan9 inet static

address 112.113.114.1

netmask 255.255.255.0

#启动接口前杀死hostapd进程清空iptables

up if pgrep hostapd ; then pkill hostapd ; fi

up iptables -t nat -F

#接口启动后执行脚本

#启动hostapd服务,制定配置文件,配置文件见附录

post-up hostapd /etc/hostapd/wpa2.conf -B

#重启hdcp服务,

post-up service isc-dhcp-server restart

#iptables配置NAT

post-up iptables -A FORWARD -i wlan9 -o eth0 -s 112.113.114.0/24 -m state --state NEW -j ACCEPT

post-up iptables -A FORWARD -m state --state ESTABLISHED,RELATED -j ACCEPT

post-up iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE

post-up echo "1" >/proc/sys/net/ipv4/ip_forward

#ifdown时自动执行脚本

down if pgrep hostapd ; then pkill hostapd ; fi

down iptables -t nat -F

手动启停

配置好/etc/network/interfaces后,可以通过手动方式验证配置是否可以正确运行。

可以通过ifup命令启动:

sudo ifup wlan9

如果/etc/network/interfaces配置有问题,可能导致ifup命令失败,报错类似(按照上面的配置不会出现下列错误,此处只是演示):

$ sudo ifup wlan9

Configuration file: /etc/hostapd/wpa2.conf

nl80211: Could not configure driver mode

nl80211 driver initialization failed.

hostapd_free_hapd_data: Interface wlan9 wasn't started

Failed to bring up wlan9.

此处问题原因是执行时这个版本的/etc/network/interfaces中没有检查hostapd是否已运行,如果hostapd已运行则启动失败,可以手动kill掉hostapd进程,配置上可以在接口up和down前后添加处理。

可以在/etc/network/interfaces中添加:

通过添加down脚本,在接口ifdown的时候关闭hostapd并清空iptables。

#ifdown时自动执行脚本

down if pgrep hostapd ; then pkill hostapd ; fi

down iptables -t nat -F

通过添加up脚本,在接口启动时执行:

#启动接口前杀死hostapd进程

up if pgrep hostapd ; then pkill hostapd ; fi

up iptables -t nat -F

如果出现上面的错误可能出现以下情况,下面列举解决方法。

最简单的方法就是修改/etc/network/interfaces,然后reboot。如果你reboot,下面就可以不用再看了

此时ifconfig查看可以看到wlan9接口是up状态:

wlan9 Link encap:Ethernet  HWaddr ec:08:6b:16:ce:53

inet addr:112.113.114.1  Bcast:112.113.114.255  Mask:255.255.255.0

UP BROADCAST MULTICAST  MTU:1500  Metric:1

RX packets:3333 errors:0 dropped:0 overruns:0 frame:0

TX packets:4244 errors:0 dropped:0 overruns:0 carrier:0

collisions:0 txqueuelen:1000

RX bytes:299633 (299.6 KB)  TX bytes:5202893 (5.2 MB)

但是/etc/network/interface中post-up的脚本执行失败了,此时使用ifdown关闭接口也会报错:

$ sudo ifdown wlan9

ifdown: interface wlan9 not configured

上面的报错说明/run/network/ifstate中没有wlan9的配置。

因为ifup执行失败,/run/network/ifstate中没有wlan9,ifdown也是根据此状态来执行的,也可以通过ifquery查看:

$ ifquery --state

eth0=eth0

lo=lo

此时需要使用修复选项并通过ifconfig关闭wlan9:

$ sudo ifdown --force wlan9

$ sudo ifconfig wlan9 down

此时再使用ifup启动可能遇到:

$ sudo ifup wlan9

RTNETLINK answers: File exists

Failed to bring up wlan9.

需要通过ip addr flush清楚接口配置,ifup才能运行:

$ sudo ip addr flush wlan9

附录:

/etc/hostapd/wpa2.conf配置:

$ cat /etc/hostapd/wpa2.conf

interface=wlan9

driver=nl80211

ssid=dy-ap

hw_mode=g

channel=11

macaddr_acl=0

auth_algs=3

wpa=2

wpa_passphrase=Fuc&P@5$w0rD

wpa_key_mgmt=WPA-PSK

rsn_pairwise=TKIP CCMP

简单说明:

ssid=dy-ap 热点名称为"dy-ap" ,随意更改

channel=11 工作在11频道,可以看需求更改

wpa=2 使用wpa2安全策略,不要更改

wpa_key_mgmt=WPA-PSK wpa预共享秘钥方式,不要更改

wpa_passphrase=Fuc&P@5$w0rD 接入密码为"Fuc&P@5$w0rD",随意更改,需要超过8位字符

在/etc/dhcp/dhcpd.conf中添加:

subnet 112.113.114.0 netmask 255.255.255.0

{

range 112.113.114.2 112.113.114.10;

option routers 112.113.114.1;

option domain-name-servers 10.0.0.1;

}

简单说明:手册参看“man dhcpd.conf”

range 112.113.114.2 112.113.114.10; 分配给客户端的地址池,当前配置支持9台设备的地址。

option domain-name-servers 10.0.0.1; 域名服务器地址,改为8.8.8.8或者114.114.114.114。

上述仅浅显使用,如有问题请参看相关手册。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值