2

CentOS下做无线AP热点

 1 year ago
source link: https://bajie.dev/posts/20240123-centos_wifiap/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

CentOS下做无线AP热点

2024-01-23 3 分钟阅读

自己的笔记本是装了个CentOS的系统,然后上面跑了个Vmware,又装了Win10,然后接两个屏幕,小的是linux跑gnome,大的是Win10屏幕,这样干活的。

路由更是混乱无比,网卡有tun0 / wlan0 / eth0 ,没有弄br0,这是大前提。如果弄br0的话,得改一堆东西。

这样的话wlan0无线网卡不联网就空闲了,于是想把无线网卡做AP共享出来,给手机用,做法真是比较复杂,弄了2天才弄好,网上的教程都是莫名啊,不太适合CentOS。

其实就是安装hostpapd,把做法记录一下:

首先不想改动已有的网络,那么桥接这条路就彻底断了,只能在无线网卡上做DHCP+NAT一条路了。

先检查网络:

lspci -k | grep -A 3 -i "network"  
03:00.0 Network controller: Qualcomm Atheros QCA9565 / AR9565 Wireless Network Adapter (rev 01)  
    Subsystem: Lite-On Communications Inc Device 0662
    Kernel driver in use: ath9k
    Kernel modules: ath9k

看到自己的网卡驱动是ath9k,再看看模块

modinfo ath9k | grep 'depend'  
depends:        mac80211,ath9k_hw,ath9k_common,cfg80211,ath  

mac80211, cfg80211看到这个就放心了,driver就是nl80211

再确认一下,里面有* AP 字样这样就没问题了:

iw list|grep -A8 "Supported interface modes:"  
    Supported interface modes:
         * IBSS
         * managed
         * AP
         * AP/VLAN
         * WDS
         * monitor
         * P2P-client
         * P2P-GO

首先看看自己的无线网卡物理地址,30:10:B3:6A:22:AA记下来

ifconfig -a  
......
wlan0     Link encap:Ethernet  HWaddr 30:10:B3:6A:22:AA  
......

再看看自己的缺省路由网卡,是虚拟网卡tun0,也记下来

route -n  
......
0.0.0.0         10.10.0.9       0.0.0.0         UG    0      0        0 tun0  
......

下载hostapd并安装,如果提示libnl没装,那就补装一下

wget http://mirrors.ustc.edu.cn/epel/6/x86_64/hostapd-2.0-7.el6.x86_64.rpm  
yum -y install hostapd-2.0-7.el6.x86_64.rpm  

禁止掉NetworkManager对无线网卡的管理

vi /etc/NetworkManager/NetworkManager.conf  
......
#在文件最后加两行
[keyfile]
unmanaged-devices=mac:30:10:B3:6A:22:AA  

然后瞬间大家就可以在gnome的桌面网卡里看到无线网卡失联了,这就对了!

编辑/etc/hostapd/hostapd.conf

vi /etc/hostapd/hostapd.conf  
interface=wlan0  
driver=nl80211  
ssid=dontMessWithVincentValentine  
hw_mode=g  
channel=6  
macaddr_acl=0  
auth_algs=1  
ignore_broadcast_ssid=0  
wpa=3  
wpa_passphrase=KeePGuessinG  
wpa_key_mgmt=WPA-PSK  
wpa_pairwise=TKIP  
rsn_pairwise=CCMP  

注意,上面hwmode是无线工作的模式,g、b、a都可以,一般是g,channel是信道,找个没多少人用的信道最好,11、6、3、1都可以。interface就是无线网卡了,wlan0。ssid和wpapassphrase按你的需求来设置即可。

ok,然后运行:

rfkill unblock all  

rfkill是控制无线和蓝牙的开关,我们放行一切,否则hostapd无法启动。

现在,启动hostapt测试一下

hostapd /etc/hostapd/hostapd.conf  

没有意外的话,就应该成功了。但这只是做了一半,首先改改dnsmasq,给无线网卡分配地址

vi /etc/dnsmasq.conf  
interface=wlan0  
bind-interfaces  
listen-address=192.168.0.1  
#no-dhcp-interface= 
dhcp-range=192.168.0.2,192.168.0.224,12h  
dhcp-option=3,192.168.0.1  
dhcp-option=6,223.5.5.5  

注意,我的dnsmasq.conf就只有这么7行,没有别的了。里面很简单,首先dhcp是放到了wlan0上并分配地址,dhcp-option中3表示网关,gateway;6表示dns,这里设置的是阿里的公共dns 223.5.5.5。

剩下的我们就写个脚本来完成吧: 内容其实就是

  • 解除无线block
  • 配置wlan0的ip为192.168.0.1
  • 启动dnsmasq
  • 做iptable的nat 注意里面我是用的tun0,如果你的缺省路由是eth0,改了即可。
vi /usr/local/bin/initSoftAP.sh  
#!/bin/bash

start() {  
rfkill unblock all  
ifconfig wlan0 up 192.168.0.1 netmask 255.255.255.0  
sleep 2

if [ -z "$(ps -e | grep dnsmasq)" ]  
then  
 dnsmasq
fi

#Enable NAT
iptables -F  
iptables -X  
iptables -t nat -A POSTROUTING -o tun0 -j MASQUERADE

#Thanks to lorenzo
#Uncomment the line below if facing problems while sharing PPPoE, see lorenzo's comment for more details
#iptables -I FORWARD -p tcp --tcp-flags SYN,RST SYN -j TCPMSS --clamp-mss-to-pmtu

sysctl -w net.ipv4.ip_forward=1

#start hostapd
hostapd -B /etc/hostapd/hostapd.conf  
}

stop() {  
service iptables stop  
service dnsmasq stop  
pkill hostapd  
/sbin/ip link set down dev wlan0
}

case $1 in  
        start)
                start
        ;;
        stop)
                stop
        ;;
        *)
        echo "Usage: $0 {start|stop}"
        exit 2
esac  

ok, 这么就搞定了。

#启动
/usr/local/bin/initSoftAP.sh start

#停止
/usr/local/bin/initSoftAP.sh stop

Recommend

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK