Configure the AP mode of the wireless network card as a wifi hotspot

1. Determine whether the network card supports AP

Regardless of whether it is a built-in wireless network card or an external usb wireless network card, it must first be configured as an AP hotspot mode. You need to check whether it supports AP mode.

For example, insert the rtl8811c dual-band usb wireless network card here iwconfigto view the network card information

nvidia@nvidia-desktop:~$ iwconfig
wlan0     unassociated  Nickname:"<WIFI@REALTEK>"
          Mode:Managed  Frequency=2.412 GHz  Access Point: Not-Associated
          Sensitivity:0/0
          Retry:off   RTS thr:off   Fragment thr:off
          Power Management:off
          Link Quality:0  Signal level:0  Noise level:0
          Rx invalid nwid:0  Rx invalid crypt:0  Rx invalid frag:0
          Tx excessive retries:0  Invalid misc:0   Missed beacon:0

If there is no corresponding information about the inserted network card, you may need to install the driver.

After that, we use the command iw listto check whether there is an AP in the list supported by "Supported interface modes:", if so, it means that it supports configuration as an AP hotspot.

nvidia@nvidia-desktop:~$ iw list
Wiphy phy1
        max # scan SSIDs: 9
        max scan IEs length: 2304 bytes
        max # sched scan SSIDs: 0
        max # match sets: 0
        max # scan plans: 1
        max scan plan interval: -1
        max scan plan iterations: 0
        Retry short limit: 7
        Retry long limit: 4
        Coverage class: 0 (up to 0m)
        Supported Ciphers:
                * WEP40 (00-0f-ac:1)
                * WEP104 (00-0f-ac:5)
                * TKIP (00-0f-ac:2)
                * CCMP-128 (00-0f-ac:4)
                * CMAC (00-0f-ac:6)
        Available Antennas: TX 0 RX 0
        Supported interface modes:
                 * IBSS
                 * managed
                 * AP
                 * P2P-client
                 * P2P-GO
                 * P2P-device

2. Configure wireless hotspot mode

Use hostapd and dhcpd to establish a wireless hotspot on the Ubuntu system.

2.1. Install hostapd and dhcpd

Installsudo apt-get install hostapd

It is not necessary to start these two services when booting, it is recommended to turn them off:

sudo update-rc.d -f hostapd remove

sudo apt-get install isc-dhcp-server, modify the following two files (if any), comment out the start on line

sudo vim /etc/init/isc-dhcp-server.conf
sudo vim /etc/init/isc-dhcp-server6.conf

2.2. Configure hotspots

2.2.1. Modify /etc/hostapd/hostapd.conf

If you haven't changed the file, just add one directly. The interface, driver, name, password, etc. used to configure the AP are as follows:

# 选择的网口名称,我这里是wlan0。具体可以ifcofnig看下当前设备下偶那些网口
interface=wlan0
# 线驱动,一般有两种:wext/nl80211,wext版本较旧目前一般使用nl80211
driver=nl80211
# AP的名称,类似于我们的路由器名称
ssid=magicsky_uav
# 802.11g,一般三个模式: a,b,g。a->5GHZ,g->2.4GHZ
hw_mode=g
# wifi工作的信道,2.4GHZ(1~14)
channel=10
macaddr_acl=0
auth_algs=3
# 选择加密方式为WPA2,常用加解密方法是有WEP、WPA、WPA2、WPA3
wpa=2
# 密码
wpa_passphrase=12345678
# 加密方法和算法
wpa_key_mgmt=WPA-PSK
wpa_pairwise=TKIP CCMP
rsn_pairwise=TKIP CCMP

2.2.2. Modify /etc/dhcp/dhcpd.conf

Add the following content at the end of /etc/dhcp/dhcpd.conf to configure ip address pool, routing gateway, dns, etc.

subnet 192.168.9.0 netmask 255.255.255.0
{
    
    
  range 192.168.9.2 192.168.9.10;
  option routers 192.168.9.1;
  option domain-name-servers 192.168.9.1,114.114.114.114,8.8.8.8;
}

Here we configure the ap address of our wireless network card as 192.168.9.1, the assignable ip address pool to connect to this hotspot is 192.168.9. (2~10), and dns uses public and gateway ip.

2.3. Service configuration

Use the service mode to start, saving a lot of command line operations

2.3.1. Service startup script ap-start.sh

#!/bin/bash
# 开启内核IP转发
bash -c "echo 1 > /proc/sys/net/ipv4/ip_forward"
# 开启防火墙NAT转发(如果本机使用eth0上网,则把ppp0改为eth0)
iptables -t nat -A POSTROUTING -o ppp0 -j MASQUERADE
# 关闭NetworkManager对无线网卡的控制
#nmcli nm wifi off
nmcli device set wlan0 managed no

# 设置并启动无线网卡
ifconfig wlan0 192.168.9.1 netmask 255.255.255.0
# 解锁无线设备,可以用rfkill list查看解锁结果.
rfkill unblock wlan
# 睡眠6秒,待rfkill解锁生效
sleep 6s
# 启动dhcpd和hostapd,如果hostapd无法启动请查看日志hostapd.log,查看这两个进程ps -ef|egrep "dhcpd|hostapd"
nohup hostapd /etc/hostapd/hostapd.conf > /var/log/hostapd.log 2>&1 &
dhcpd wlan0 -pf /var/run/dhcpd.pid
ps -ef|head -n1 && ps -ef|egrep "dhcpd|hostapd"

ps -ef|egrep “dhcpd|hostapd” should have two processes,

nvidia@nvidia-desktop:~$ ps -ef|head -n1 && ps -ef|egrep "dhcpd|hostapd"
UID        PID  PPID  C STIME TTY          TIME CMD
root      6367     1  0 10:55 ?        00:00:00 hostapd /etc/hostapd/hostapd.conf
root      6369     1  0 10:55 ?        00:00:00 dhcpd wlan0 -pf /var/run/dhcpd.pid
nvidia    9718  9601  0 13:36 pts/0    00:00:00 grep -E --color=auto dhcpd|hostapd

There may be no dhcp process, manual execution dhcpd wlan0 -pf /var/run/dhcpd.pidmay report the error as follows, just give permission, add a line before the script chmod 777 /var/lib/dhcp/dhcpd.leases.

Internet Systems Consortium DHCP Server 4.3.5
Copyright 2004-2016 Internet Systems Consortium.
All rights reserved.
For info, please visit https://www.isc.org/software/dhcp/
Config file: /etc/dhcp/dhcpd.conf
Database file: /var/lib/dhcp/dhcpd.leases
PID file: /var/run/dhcpd.pid
Can't open /var/lib/dhcp/dhcpd.leases for append.

If you think you have received this message due to a bug rather
than a configuration issue please read the section on submitting
bugs on either our web page at www.isc.org or in the README file
before submitting a bug.  These pages explain the proper
process and the information we find helpful for debugging..

exiting.

2.3.1. Service shutdown script ap-stop.sh

#!/bin/bash
killall hostapd dhcpd
bash -c "echo 0 > /proc/sys/net/ipv4/ip_forward"
#ifconfig wlan0 down
nmcli device set wlan0 managed yes

2.3.2. Service files

I won't write for now. Put the service startup script directly in /etc/rc.local in the self-start script.

If not, you can add it in the following way sudo ln -s /lib/systemd/system/rc.local.service /etc/systemd/system/, then add the file sudo touch /etc/rc.localand give permission sudo chmod +x /etc/rc.local. If it cannot be started automatically, you need to judge the status through systemctl status rc.local.service, and if it is not started, you need to enable it.

3. Test

Install the wireless network card, restart after configuration, check the network situation, wlan0 is configured as 192.168.9.1.
insert image description here
The AP hotspot can also be searched on the mobile phone, and the IP is automatically assigned after connection.
insert image description here
If it cannot be pinged, route -ncheck whether there is a routing table by

$ route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.3.254   0.0.0.0         UG    100    0        0 eth0
169.254.0.0     0.0.0.0         255.255.0.0     U     1000   0        0 eth0
172.17.0.0      0.0.0.0         255.255.0.0     U     0      0        0 docker0
192.168.3.0     0.0.0.0         255.255.255.0   U     100    0        0 eth0
192.168.9.0     0.0.0.0         255.255.255.0   U     0      0        0 wlan0

The last line shows that the data pointing to the 192.168.9.0 network segment passes through the interface wlan0. If it is not normal, it can be added manually sudo route add -net 192.168.9.0/24 dev wlan0.

Guess you like

Origin blog.csdn.net/wanggao_1990/article/details/131695812