程序员我用 Linux

ubuntu 自启动无线网卡AP模式

2016-08-23  本文已影响0人  dyang__

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

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

下面的操作需要ubuntu中已安装hostapdisc-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中添加:


#ifdown时自动执行脚本


down if pgrep hostapd ; then pkill hostapd ; fi

down iptables -t nat -F


#启动接口前杀死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。

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

上一篇 下一篇

猜你喜欢

热点阅读