今天总结一下前几天折腾的usb无线网卡驱动。

一、确定网卡驱动

实验机器内核版本信息如下:

[root@localhost ~]# uname -a
Linux localhost.localdomain 3.10.0-693.21.1.el7.x86_64 #1 SMP Sat Jun 2 02:26:41 CST 2018 x86_64 x86_64 x86_64 GNU/Linux
[root@localhost ~]#

当拿到usb网卡,第一件事情就是确定无限网卡的驱动,如果比较新的网卡,驱动可能不太好找或者不稳定,这是在linux下面搞驱动的痛苦。通过lsusb命令查看驱动:

[root@localhost ~]# 
[root@localhost ~]# lsusb
Bus 002 Device 003: ID 0bda:8179 Realtek Semiconductor Corp. RTL8188EUS 802.11n Wireless Network Adapter
Bus 002 Device 002: ID 8087:0024 Intel Corp. Integrated Rate Matching Hub
Bus 002 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 001 Device 004: ID 413c:a001 Dell Computer Corp. Hub
Bus 001 Device 003: ID 0424:2512 Standard Microsystems Corp. USB 2.0 Hub
Bus 001 Device 002: ID 8087:0024 Intel Corp. Integrated Rate Matching Hub
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
[root@localhost ~]#

通过上面命令可知:Bus 002 Device 003: ID 0bda:8179 Realtek Semiconductor Corp. RTL8188EUS 802.11n Wireless
网卡芯片型号是RTL8188EUS,这种网卡属于比较旧的,驱动还是比较容易找到,如果大家找不到,可以去这里下载,我已经上传到csdn中。

二、编译并安装驱动

2.1、编译安装

注意,不同内核版本驱动文件.ko是不兼容,即使是小版本号不一致也是不行,都需要自己编译才可以。执行make && make install命令,我在编译过程中没有出现任何问题。

[root@localhost rtl8188EUS_rtl8189ES_linux_v4.1.8_9499.20131104]# 
[root@localhost rtl8188EUS_rtl8189ES_linux_v4.1.8_9499.20131104]# make
make ARCH=x86_64 CROSS_COMPILE= -C /lib/modules/3.10.0-693.21.1.el7.x86_64/build M=/root/wlan0/rtl8188EUS_rtl8189ES_linux_v4.1.8_9499.20131104  modules
make[1]: Entering directory `/usr/src/kernels/3.10.0-693.el7.x86_64'
  CC [M]  /root/wlan0/rtl8188EUS_rtl8189ES_linux_v4.1.8_9499.20131104/core/rtw_cmd.o
  CC [M]  /root/wlan0/rtl8188EUS_rtl8189ES_linux_v4.1.8_9499.20131104/core/rtw_security.o
  CC [M]  /root/wlan0/rtl8188EUS_rtl8189ES_linux_v4.1.8_9499.20131104/core/rtw_debug.o
  CC [M]  /root/wlan0/rtl8188EUS_rtl8189ES_linux_v4.1.8_9499.20131104/core/rtw_io.o
  CC [M]  /root/wlan0/rtl8188EUS_rtl8189ES_linux_v4.1.8_9499.20131104/core/rtw_ioctl_query.o
  CC [M]  /root/wlan0/rtl8188EUS_rtl8189ES_linux_v4.1.8_9499.20131104/core/rtw_ioctl_set.o
  CC [M]  /root/wlan0/rtl8188EUS_rtl8189ES_linux_v4.1.8_9499.20131104/core/rtw_ieee80211.o
  。。。
  Building modules, stage 2.
  MODPOST 1 modules
  CC      /root/wlan0/rtl8188EUS_rtl8189ES_linux_v4.1.8_9499.20131104/8188eu.mod.o
  LD [M]  /root/wlan0/rtl8188EUS_rtl8189ES_linux_v4.1.8_9499.20131104/8188eu.ko
make[1]: Leaving directory `/usr/src/kernels/3.10.0-693.el7.x86_64'
[root@localhost rtl8188EUS_rtl8189ES_linux_v4.1.8_9499.20131104]#
[root@localhost rtl8188EUS_rtl8189ES_linux_v4.1.8_9499.20131104]# make install
install -p -m 644 8188eu.ko  /lib/modules/3.10.0-693.21.1.el7.x86_64/kernel/drivers/net/wireless/
/sbin/depmod -a 3.10.0-693.21.1.el7.x86_64
[root@localhost rtl8188EUS_rtl8189ES_linux_v4.1.8_9499.20131104]#

2.2、加载、卸载驱动

make install只是把驱动文件放到了目录中,但是还没有进行加载。驱动加载使用insmod,卸载驱动使用rmmod,如下所示:

[root@localhost rtl8188EUS_rtl8189ES_linux_v4.1.8_9499.20131104]# cd /lib/modules/3.10.0-693.21.1.el7.x86_64/kernel/drivers/net/wireless/
[root@localhost wireless]# insmod 8188eu.ko  
[root@localhost wireless]#

当加载完驱动之后,如何确定我们的驱动是否好用?输入ifconfig -a 或者iwconfig,如果多一个网卡则表示驱动已经可以正常使用,例如:

enp0s29u1u2: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet6 fe80::2bb:5ff:fe00:5a76  prefixlen 64  scopeid 0x20<link>
        ether 00:bb:05:00:5a:76  txqueuelen 1000  (Ethernet)
        RX packets 11  bytes 23713 (23.1 KiB)
        RX errors 0  dropped 2  overruns 0  frame 0
        TX packets 1  bytes 3942 (3.8 KiB)
        TX errors 0  dropped 5 overruns 0  carrier 0  collisions 0

三、连接网络

3.1、扫描wifi信号

在连接网络之前,需要先执行ifconfig enp0s29u1u2 up 激活网卡。

通过名iwlist scanning进行wifi信号扫描

[root@localhost wireless]# iwlist scanning
lo        Interface doesn't support scanning.

enp0s29u1u2  Scan completed :
          Cell 01 - Address: 74:4A:A4:BD:FE:DF
                    ESSID:"MF910S_BDFEDF"
                    Protocol:IEEE 802.11bgn
                    Mode:Master
                    Frequency:2.462 GHz (Channel 11)
                    Encryption key:on
                    Bit Rates:72 Mb/s
                    Extra:rsn_ie=30140100000fac040100000fac040100000fac020000
                    IE: IEEE 802.11i/WPA2 Version 1
                        Group Cipher : CCMP
                        Pairwise Ciphers (1) : CCMP
                        Authentication Suites (1) : PSK
                    Quality=100/100  Signal level=97/100  
                    Extra:fm=0003          
em1       Interface doesn't support scanning.

em2       Interface doesn't support scanning.
[root@localhost wireless]#

当扫描出wifi信号则表示我们驱动是完全可用的。

3.2 链接wifi

目前有两种方式连接wifi信号:iwconfig、wpa_supplicant。这两种方式主要区别(非常重要):

命令

加密方式

支持驱动

iwconfig

只支持wep,不支持wpa/wpa2

几乎所有驱动

wpa_supplicant

支持wep,wpa/wpa2

部分驱动不支持

通过上表可知,大部分wifi加密方式都是wpa/wpa2,因此只能使用wpa_supplicant方式。如果wifi是无密码的,那么使用这两种命令都是可以的。

3.2.1、生成配置文件

首先通过wpa_passphrase命令行生成配置项,wpa_passphrase [essid] [password]:

[root@localhost wlan0]# 
[root@localhost wlan0]# wpa_passphrase "MF910S_BDFEDF" "8AJ8ACQDDQ"
network={
        ssid="MF910S_BDFEDF"
        #psk="8AJ8ACQDDQ"
        psk=7b19b5ebf95d70e973e9b0040eae1f104aa01428b4e17748bc527b71dc028f97
}
[root@localhost wlan0]#

将输入内容追加到文件/etc/wpa_supplicant/wpa_supplicant.conf文件末尾,并新增几项配置,具体如下:

[root@localhost ~]# 
[root@localhost ~]# vi /etc/wpa_supplicant/wpa_supplicant.conf 
ctrl_interface=/var/run/wpa_supplicant
ctrl_interface_group=wheel

network={
        ssid="MF910S_BDFEDF"
        #psk="8AJ8ACQDDQ"
        psk=7b19b5ebf95d70e973e9b0040eae1f104aa01428b4e17748bc527b71dc028f97
        proto=RSN
        key_mgmt=WPA-PSK
        pairwise=CCMP TKIP
        group=CCMP TKIP
}

新增配置项为,proto,key_mgmt,pariwise,group(这几项我并清楚是什么意思,也是从国外网站复制出来的)。

3.2.2、创建配置网卡配置文件

[root@localhost ~]# cat /etc/sysconfig/network-scripts/ifcfg-enp0s29u1u2 
#DHCP client
DEVICE=enp0s29u1u2
BOOTPROTO=dhcp
ONBOOT=yes
ssid="MF910S_BDFEDF"
#psk="8AJ8ACQDDQ"
psk=7b19b5ebf95d70e973e9b0040eae1f104aa01428b4e17748bc527b71dc028f97
WPA=yes
[root@localhost ~]#

其中ssid、psk均和上一步骤保持一致。

3.2.3、连接wifi

[root@localhost ~]# wpa_supplicant -ienp0s29u1u2 -B -c /etc/wpa_supplicant/wpa_supplicant.conf
Successfully initialized wpa_supplicant
nl80211: Driver does not support authentication/association or connect commands
enp0s29u1u2: Failed to initialize driver interface
[root@localhost ~]#

Failed to initialize driver interface。由此可知当前网卡不支持nl80211驱动。瞬间陷入苦恼,经过一番谷歌,得知wpa_supplicat还支持另外一种模式,WEXT(Wireless Extensions),可以通过命令行参数-Dwext指定该模式,因此抱着尝试心态,试了一下:

[root@localhost ~]# wpa_supplicant  -Dwext  -ienp0s29u1u2 -B -c /etc/wpa_supplicant/wpa_supplicant.conf
Successfully initialized wpa_supplicant
rfkill: Cannot get wiphy information
ioctl[SIOCSIWMODE]: Operation not permitted
ioctl[SIOCSIWAP]: Operation not permitted
[root@localhost ~]# 
[root@localhost ~]# 
[root@localhost ~]#

虽然上面有警告信息,但是通过iwconfig查看,发现连接成功:

[root@localhost ~]# iwconfig
lo        no wireless extensions.
enp0s29u1u2  IEEE 802.11bgn  ESSID:"MF910S_BDFEDF"  Nickname:"<WIFI@REALTEK>"
          Mode:Managed  Frequency:2.462 GHz  Access Point: 74:4A:A4:BD:FE:DF   
          Bit Rate:72.2 Mb/s   Sensitivity:0/0  
          Retry:off   RTS thr:off   Fragment thr:off
          Encryption key:****-****-****-****-****-****-****-****   Security mode:open
          Power Management:off
          Link Quality=88/100  Signal level=96/100  Noise level=0/100
          Rx invalid nwid:0  Rx invalid crypt:0  Rx invalid frag:0
          Tx excessive retries:0  Invalid misc:0   Missed beacon:0
em1       no wireless extensions.
em2       no wireless extensions.
[root@localhost ~]#

当出现Mode:Managed Frequency:2.462 GHz Access Point: 74:4A:A4:BD:FE:DF,则表示连接成功,接下来就可以通过dhclient获取ip了

四、总结

通过这次经历,加深了iwconfig和wpa_supplicant区别(大坑),同时了解到nl80211和WEXT两种不同驱动模块。wpa_supplicant默认是通过nl80211方式,当这种方式提示失败后,方可尝试WEXT方式。