在Linux上配置USB无线网卡

First Post:

Last Update:

Word Count:
1.3k

Read Time:
6 min

最近出于某个需求需要在Linux上同时启用WIFI和AP,但是根据Arch Wiki尝试了几个小时也没成功,按理说我的网卡是支持的,但是既然不行只好曲线救国了一一从别人那顺了个USB NIC。

UPDATE: 写完本文后去试了一下用原来的网卡同时开启AP和Wi-Fi,然后就成功了…有点无语,本文留个纪念吧。

配置网卡

插上去,无事发生。ip link没有显示。lsusb显示是CD ROM, Omg! 为什么会是一个光盘?

1
2
$ lsusb
Bus 001 Device 009: ID 0bda:1a2b Realtek Semiconductor Corp. RTL8188GU 802.11n WLAN Adapter (Driver CDROM Mode)

好吧,也不指望开箱即用,下面涉及两个问题:

  1. 驱动问题
  2. 为什么NIC会显示成CDROM?如何解决?

1. 驱动问题

1
2
3
4
5
6
7
8
9
10
11
12
13
14
kernel: usb 1-2: new high-speed USB device number 5 using xhci_hcd
kernel: usb 1-2: New USB device found, idVendor=0bda, idProduct=c811, bcdDevice= 2.00
kernel: usb 1-2: New USB device strings: Mfr=1, Product=2, SerialNumber=3
kernel: usb 1-2: Product: 802.11ac NIC
kernel: usb 1-2: Manufacturer: Realtek
kernel: usb 1-2: SerialNumber: 123456
kernel: Loading firmware: rtw88/rtw8821c_fw.bin
kernel: rtw_8821cu 1-2:1.0: Direct firmware load for rtw88/rtw8821c_fw.bin failed with error -2
kernel: rtw_8821cu 1-2:1.0: failed to request firmware
kernel: rtw_8821cu 1-2:1.0: failed to load firmware
kernel: rtw_8821cu 1-2:1.0: failed to setup chip efuse info
kernel: rtw_8821cu 1-2:1.0: failed to setup chip information
kernel: rtw_8821cu 1-2:1.0: probe with driver rtw_8821cu failed with error -22
kernel: usbcore: registered new interface driver rtw_8821cu

有关日志如上,看来是缺少驱动,可以注意到使用rtw88/rtw8821c_fw.bin驱动,需要注意的是,该驱动如今已经在内核中原生支持,如果不支持,请考虑升级Linux内核,我使用的是Gentoo,并启用了linux-firmwaresavedconfig USE flag, 这会删除所有我没显式指定的驱动,以节约空间,所以加上再重新安装就完美解决了:

echo rtw88/rtw8821c_fw.bin | sudo tee -a /etc/portage/savedconfig/sys-kernel/linux-firmware

2.为什么NIC会显示成CDROM?如何解决?

在经过一番搜索后,找到了这个项目,记录了大量 Linux 上 USB NIC 的知识,在此表示敬意o>

QA第七条,有以下内容:

Question: My USB WiFi adapter is showing up as a CDROM or Flash drive instead of a WiFi adapter. What is the problem?
Answer: Your USB WiFi adapter showing up as a CDROM or Flash drive (often with ID 0bda:1a2b) instead of functioning as a network adapter (such as ID 35bc:0102 or similar) is likely due to a “mode-switching” issue. Some USB WiFi adapters include onboard memory that contains drivers or installation software for Windows. When plugged into a system for the first time, they initially present themselves as a virtual CD-ROM or Flash driver containing the drivers. This is fairly common with USB WiFi adapters that use Realtek chipset.
您的 USB WiFi 适配器显示为光驱或闪存盘(通常为 ID 0bda:1a2b),而非作为网络适配器(如 ID 35bc:0102 等)正常工作,这很可能是由于「模式切换」问题所致。一些 USB WiFi 适配器包含内置存储,其中存放了 Windows 的驱动程序或安装软件。首次插入系统时,它们会先以虚拟光驱或闪存盘的形式出现,包含驱动程序。这在使用 Realtek 芯片的 USB WiFi 适配器中相当常见。

In Linux, the usb_modeswitch utility generally handles this issue but there are situations where it does not work as expected.
在 Linux 中,usb_modeswitch 工具通常可以处理此问题,但某些情况下可能无法按预期工作。

🤔 好吧,实际上作者的推荐表中也是不推荐 Realtek RTL8821cu 芯片组的,看来只能将就用了。

这个页面记录了如何使用usb_modeswitch来解决这个问题,省流:解决了。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# 首先安装工具
$ emerge usb_modeswitch

$ usb_modeswitch -K -W -v 0bda -p 1a2b
Take all parameters from the command line


* usb_modeswitch: handle USB devices with multiple modes
* Version 2.6.1 (C) Josua Dietze 2017
* Based on libusb1/libusbx

! PLEASE REPORT NEW CONFIGURATIONS !

DefaultVendor= 0x0bda
DefaultProduct= 0x1a2b

StandardEject=1

Look for default devices ...
found USB ID 0bda:9210
vendor ID matched
found USB ID 1d6b:0003
found USB ID 13d3:56fb
found USB ID 0489:e0cd
found USB ID 258a:010c
found USB ID 1ea7:0066
found USB ID 0fe6:9910
found USB ID 1a40:0101
found USB ID 1d6b:0002
found USB ID 1d6b:0003
found USB ID 0bda:c811
vendor ID matched
found USB ID 1d6b:0002
No devices in default mode found. Nothing to do. Bye!

$ lsusb
Bus 001 Device 012: ID 0bda:c811 Realtek Semiconductor Corp. 802.11ac NIC

WoW, It works!下面就是配置自动执行了,具体可以在链接里看,基本就是添加一条udev规则和usb_modeswitch配置。

配置热点

下面ip link可以看到网卡了:

1
2
3
6: wlp5s0f3u2: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN mode DORMANT group default qlen 1000
link/ether 5a:74:98:1d:ab:ed brd ff:ff:ff:ff:ff:ff permaddr 6c:1f:f7:0f:60:c1
altname wlx6c1ff70f60c1

名字太丑了,首先改个名:

1
2
3
4
# 从lsusb获得新的NIC id
$ echo 'ACTION=="add", SUBSYSTEM=="net", ATTRS{idVendor}=="0bda", ATTRS{idProduct}=="c811", NAME="wlan1"' | sudo tee /etc/udev/rules.d/70-wlan1.rules
$ sudo udevadm control --reload
$ sudo udevadm trigger

装个软件,可以节约自己手动配置hostapd和dnsmasq的时间,当然,这两个作为依赖还是要装的:

1
emerge net-wireless/linux-wifi-hotspot

软件配制就很简单了,这里不赘述。

Works like a charm! 😉

1
2
3
4
5
6
7
8
9
10
$ ip addr | grep -E -A3 '(wlo1|wlan1):'
2: wlo1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000
link/ether 90:0f:0c:e4:8a:cb brd ff:ff:ff:ff:ff:ff
altname wlp3s0
altname wlx900f0ce48acb
--
7: wlan1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000
link/ether 6c:1f:f7:0f:60:c1 brd ff:ff:ff:ff:ff:ff
altname wlp5s0f3u2
altname wlx6c1ff70f60c1

结语

最后,向维护USB-WIFI项目的开发者和所有贡献者表达敬意o>