树莓派 4b 配置 USB 网络连接

  • 树莓派镜像: 2022-09-22-raspios-bullseye-arm64.img

  • 电脑: windows 10

1 打开树莓派的 USB 网络

  • /boot/config.txt 最后一行添加 dtoverlay=dwc2

例如:

# ....[all]
dtoverlay=dwc2
  • /boot/cmdline.txtrootwait 后添加 modules-load=dwc2,g_ether

例如:

console=serial0,115200 console=tty1 root=PARTUUID=3d7323dc-02 rootfstype=ext4 fsck.repair=yes rootwait modules-load=dwc2,g_ether quiet splash plymouth.ignore-serial-consoles
  • 重启树莓派
sudo reboot

2 在 windows 上安装 Linux USB Ethernet/RNDIS Gadget 驱动

  • 将树莓派充电的 type-c 口,使用手机数据线连接到电脑上。打开设备管理器,此时 windows 系统将设备识别成 COM 口。

2.1 下载驱动

驱动下载链接:

RPI Driver OTG

百度网盘(提取码: e69j)

如果没有安装 VS 或者不用自己编译驱动,建议直接跳到 2.3 小节。

2.2 自己编译驱动(需要安装VS)

  • 编写 linux.inf 文件,内容为
; Based on template INF file found at
;    <https://msdn.microsoft.com/en-us/library/ff570620.aspx>
; which was:
;    Copyright (c) Microsoft Corporation
; and released under the MLPL as found at:
;    <http://msdn.microsoft.com/en-us/cc300389.aspx#MLPL>.
; For use only on Windows operating systems.[Version]
Signature           = "$Windows NT$"
Class               = Net
ClassGUID           = {4d36e972-e325-11ce-bfc1-08002be10318}
Provider            = %Linux%
DriverVer           = 10/10/2020,6.1.6000.16384
CatalogFile         = linux.cat[Manufacturer]
%Linux%             = LinuxDevices,NTx86,NTamd64,NTia64; Decoration for x86 architecture
[LinuxDevices.NTx86]
%LinuxDevice%       = RNDIS.NT.5.1, USB\VID_0525&PID_a4a2, USB\VID_1d6b&PID_0104&MI_00; Decoration for x64 architecture
[LinuxDevices.NTamd64]
%LinuxDevice%       = RNDIS.NT.5.1, USB\VID_0525&PID_a4a2, USB\VID_1d6b&PID_0104&MI_00; Decoration for ia64 architecture
[LinuxDevices.NTia64]
%LinuxDevice%       = RNDIS.NT.5.1, USB\VID_0525&PID_a4a2, USB\VID_1d6b&PID_0104&MI_00;@@@ This is the common setting for setup
[ControlFlags]
ExcludeFromSelect=*; DDInstall section
; References the in-build Netrndis.inf
[RNDIS.NT.5.1]
Characteristics     = 0x84   ; NCF_PHYSICAL + NCF_HAS_UI
BusType             = 15
; NEVER REMOVE THE FOLLOWING REFERENCE FOR NETRNDIS.INF
include             = netrndis.inf
needs               = Usb_Rndis.ndi
AddReg              = Rndis_AddReg_Vista; DDInstal.Services section
[RNDIS.NT.5.1.Services]
include             = netrndis.inf
needs               = Usb_Rndis.ndi.Services; Optional registry settings. You can modify as needed.
[RNDIS_AddReg_Vista]
HKR, NDI\params\VistaProperty, ParamDesc,  0, %Vista_Property%
HKR, NDI\params\VistaProperty, type,       0, "edit"
HKR, NDI\params\VistaProperty, LimitText,  0, "12"
HKR, NDI\params\VistaProperty, UpperCase,  0, "1"
HKR, NDI\params\VistaProperty, default,    0, " "
HKR, NDI\params\VistaProperty, optional,   0, "1"; No sys copyfiles - the sys files are already in-build
; (part of the operating system).
; We do not support XP SP1-, 2003 SP1-, ME, 9x.[Strings]
Linux                 = "Linux Developer Community"
LinuxDevice           = "Linux USB Ethernet/RNDIS Gadget"
Vista_Property        = "Optional Vista Property"
  • 编写 DriverHelper.ps1 文件,内容为
# script parameters
param([ValidateSet("Sign", "UntrustCertificates")][string] $Mode = "Sign",[string] $InfFile,[string] $CommonName = "linux.local",[switch] $Force
)$ErrorActionPreference = "Stop"# check if this script is run as administrator
If (-NOT ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator))
{Write-Error "You are not an Administrator. This script needs to be run by an Adminsitrator!"
}# check if InfFile exists
if(-NOT (Test-Path -Path $InfFile))
{Write-Error "$InfFile not found!"
}# utility functions
function Step
{param([string] $Desc, [System.Management.Automation.ScriptBlock] $Code)Write-Output "[*] $Desc";Invoke-Command -ScriptBlock $Code
}function Get-CertificateThumbprint
{param([string] $FilePath)$cert = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2$cert.Import($(Resolve-Path $FilePath))return $cert.Thumbprint
}# Get Kits root
try {$InstalledRoots = Get-ItemProperty -Path "Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows Kits\Installed Roots"$WinKits = $InstalledRoots.KitsRoot10$ToolsPrefix = "$($WinKits)\bin\10.0.18362.0\x64"
} catch {Write-Error "Could not locate the Windows Driver Kit. Head to https://docs.microsoft.com/en-us/windows-hardware/drivers/download-the-wdk and download the WDK"
}function RunKit
{param([Parameter(Mandatory=$true)][string] $ToolName, [string[]] $Arguments)Write-Output "Running $($ToolsPrefix)\$($ToolName)"$Proc = Start-Process -FilePath "$($ToolsPrefix)\$($ToolName)" -ArgumentList $Arguments -NoNewWindow -PassThru -Waitif ($Proc.ExitCode -NE 0){Write-Error "Process failed with exit code: $($Proc.ExitCode)"}
}# Script vars
$CatFile = [io.path]::GetFileNameWithoutExtension($InfFile) + ".cat"
$CertFile = [io.path]::GetFileNameWithoutExtension($InfFile) + ".cer"# Scripts
function DoSign
{# verify that the Inf file contains a "Catalog" entryif (-NOT (Select-String -Path $InfFile -Pattern "CatalogFile" -CaseSensitive -SimpleMatch -Quiet)){Write-Error "Could not find the 'CatalogFile' entry inside the INF file! Make sure it points to a certificate with the same Name."}Step -Desc "Create Catalog file" -Code {New-FileCatalog -Path $InfFile -CatalogFilePath $CatFile -CatalogVersion 2.0}if (-NOT (Test-Path -Path $CertFile) -OR $Force){Step -Desc "Create a Test Certificate" -Code {# see https://docs.microsoft.com/en-us/windows-hardware/drivers/install/creating-test-certificates# This will create a self-signed certificate and store it in the Personal certificate stora on the local machine.RunKit -ToolName "makecert.exe" -Arguments "-r -sr LocalMachine -ss My -pe -n CN=$($CommonName) -eku 1.3.6.1.5.5.7.3.3 $CertFile"}Step -Desc "Import the Test Certificate into the Local Computer Trusted Root CAs and Trusted Publishers stores" -Code {Import-Certificate -FilePath $CertFile -CertStoreLocation Cert:\LocalMachine\RootImport-Certificate -FilePath $CertFile -CertStoreLocation Cert:\LocalMachine\TrustedPublisher}}else{Write-Output "Certificate already exists. Using $CertFile. If you still want to create a new one, use the -Force option"}Step -Desc "Sign the Catalog with the Test Certificate" -Code {# see https://docs.microsoft.com/en-us/windows-hardware/drivers/install/test-signing-a-catalog-fileRunKit -ToolName "signtool.exe" -Arguments "sign /v /sm /s My /n $($CommonName) /t http://timestamp.digicert.com $($CatFile)"}Write-Output "Successfully singed the driver and added the certificate to the trusted store."
}function DoUntrustCertificates
{Step -Desc "Remove the Test Certificate from the Local Computer Trusted Root CAs and Trusted Publishers stores" -Code {$thumbprint = Get-CertificateThumbprint -FilePath $CertFileRemove-Item -Path ("Cert:\LocalMachine\My\$($thumbprint)")Remove-Item -Path ("Cert:\LocalMachine\Root\$($thumbprint)")Remove-Item -Path ("Cert:\LocalMachine\TrustedPublisher\$($thumbprint)")}Write-Output "Sucessfully removed all certificates from the trusted certificate stores."
}# entry
Write-Output "Selected Mode: $Mode"Switch($Mode)
{"Sign" { DoSign }"UntrustCertificates" { DoUntrustCertificates }
}
  • 以管理员的身份打开 PowerShell,切换到当前目录,运行 .\DriverHelper.ps1 -Mode Sign -InfFile .\linux.inf 命令,得到 linux.catlinux.cer 文件。

2.3 安装驱动

  • 打开设备管理器,选择中树莓派的 COM 口,选择更新驱动程序。

  • 选择 游览我的电脑以查找驱动程序

  • 目录调整到创建前面文件所在目录,点击下一页

  • 安装完成

  • 此时任务管理器驱动显示正常

3 在树莓派上安装并打开 DHCP 服务器

  • /etc/modules 末尾添加 libcomposite

例如:

i2c-dev
libcomposite
  • /etc/dhcpcd.conf 末尾添加 denyinterfaces usb0

例如:

#....
# fallback to static profile on eth0
#interface eth0
#fallback static_eth0
denyinterfaces usb0
  • 安装 dnsmasq
sudo apt update
sudo apt install dnsmasq
  • 创建 /etc/dnsmasq.d/usb 文件,内容为
interface=usb0
dhcp-range=10.55.0.2,10.55.0.254,255.255.255.0,12h
dhcp-option=3
leasefile-ro

其中 dhcp-range 设置表示 DHCP 服务分配的 IP 地址范围从 10.55.0.2-10.55.0.254,子网掩码为 255.255.255.0,有效期为 12 小时。

  • 创建 /etc/network/interfaces.d/usb0 文件,内容为
auto usb0
allow-hotplug usb0
iface usb0 inet staticaddress 10.55.0.1netmask 255.255.255.0

其中,address 表示 USB 接口的 IP 地址为 10.55.0.1,子网掩码为 255.255.255.0。

  • 创建 /root/usb.sh ,内容为
#!/bin/bash
cd /sys/kernel/config/usb_gadget/
mkdir -p pi4
cd pi4
echo 0x1d6b > idVendor # Linux Foundation
echo 0x0104 > idProduct # Multifunction Composite Gadget
echo 0x0100 > bcdDevice # v1.0.0
echo 0x0200 > bcdUSB # USB2
echo 0xEF > bDeviceClass
echo 0x02 > bDeviceSubClass
echo 0x01 > bDeviceProtocol
mkdir -p strings/0x409
echo "fedcba9876543211" > strings/0x409/serialnumber
echo "Ben Hardill" > strings/0x409/manufacturer
echo "PI4 USB Device" > strings/0x409/product
mkdir -p configs/c.1/strings/0x409
echo "Config 1: ECM network" > configs/c.1/strings/0x409/configuration
echo 250 > configs/c.1/MaxPower
# Add functions here
# see gadget configurations below
# End functions
mkdir -p functions/ecm.usb0
HOST="00:dc:c8:f7:75:14" # "HostPC"
SELF="00:dd:dc:eb:6d:a1" # "BadUSB"
echo $HOST > functions/ecm.usb0/host_addr
echo $SELF > functions/ecm.usb0/dev_addr
ln -s functions/ecm.usb0 configs/c.1/
udevadm settle -t 5 || :
ls /sys/class/udc > UDC
ifup usb0
service dnsmasq restart
  • 给脚本添加可执行权限
sudo chmod +x /root/usb.sh
  • 添加脚本开机启动,在 /etc/rc.localexit 0 上一行添加 /root/usb.sh

例如

_IP=$(hostname -I) || true
if [ "$_IP" ]; thenprintf "My IP address is %s\n" "$_IP"
fi/root/usb.shexit 0
  • 重启树莓派,在树莓派上看到 USB 接口的 IP 地址。

  • 在 windows 电脑上查看到树莓派 type-c 接口分配的 IP 地址。

此时树莓派 IP 地址为 10.55.0.1,window 电脑的 IP 地址为 10.55.0.2。

4 测试连接

  • 用 windows 电脑 ping 树莓派。

  • 如果树莓派上打开了 ssh,则可以使用 ssh 连接。

  • 如果树莓派上打开了VNC,也可以用 VNC 连接

参考链接

Ethernet Gadget
Pi4 USB-C Gadget
Fix USB connection from Raspberry Pi on Windows

树莓派 4b 配置 USB 网络连接相关推荐

  1. (学习笔记)树莓派4B使用usb转串口通讯模块

    一.写在前面 首先,树莓派上自带一个串口,用于连接蓝牙或是串口命令行,对于这个串口的使用我们已经简单探究过了,可以点击蓝字看看我的这篇文章:树莓派4B和pc之间进行串口通信(解决报错). 但是,对于嵌 ...

  2. 树莓派4B使用USB转串口通讯模块

    树莓派官方的系统已经集成了USB转串口驱动,直接连接上串口就可以使用. 1. 把usb转串口通讯模块插上 2. 查看识别的串口号 ls -l /dev/tty* 3. 查看串口的波特率 stty -F ...

  3. 树莓派4B配置Selenium库+设置无图无头属性

    写一个树莓派4B配置selenium库,并成功调用浏览器进行自动化爬虫 本文会手把手教你配置环境,并带你写第一个selenium爬虫程序 使用版本说明 系统:官网2020-02-13-raspbian ...

  4. 树莓派4B配置OpenCV4.4.0(解决报错)

    树莓派4B配置OpenCV 1.前言 首先,opencv在Linux系统上使用需要编译. 我参考的是这两篇文章. 树莓派4b Opencv安装(真实有效)--授人鱼不如授人以渔 用树莓派4b构建深度学 ...

  5. 树莓派4B 不同无线网卡自动连接不同wifi

    树莓派4B中只有一张无线网卡,在日常使用中,有时可能会出现需要两张无线网卡分别连接不同WIFI的情况.如果想让树莓派开机后两张无线网卡能自动连接上对应的WIFI,不产生连接的错位,首先,需要购买一张无 ...

  6. 树莓派4B从开箱到连接电脑(超级小白)

    1.树莓派系统烧录 官网链接:https://www.raspberrypi.com/ (1)点击"Software" (2)下拉,根据系统选择,笔者的是windows,所以 (3 ...

  7. 【树莓派】配置无线网络(wifi)

    树莓派网络分为有线网络和无线网络两种连接方式,有线网络只需要一根网线接入,让设备动态获取IP即可, 基本不需要进行设置.但在使用无线网络连接时,经常会出现一些问题,如输入ifconfig命令时找不到设 ...

  8. OEL / RedHat linux 配置无线网络连接(含驱动安装)

    本机环境:Thinkpad E440,Oracle Linux Server release 6.7(kernel 3.8.13-68.3.4.el6uek.x86_64) 本机无线网卡为RTL872 ...

  9. 树莓派开发笔记(十七):树莓派4B+上Qt多用户连接操作Mysql数据库同步(单条数据悲观锁)

    若该文为原创文章,转载请注明原文出处 本文章博客地址:https://hpzwl.blog.csdn.net/article/details/125188715 红胖子(红模仿)的博文大全:开发技术集 ...

最新文章

  1. java多附件上传 实例demo
  2. python输出日志文件_python将print输出的信息保留到日志文件中
  3. Android复习12【广播接收者-BroadcastReceiver(简单案例-发送广播、静态注册、动态注册、本地广播、代码示例(别处登陆踢用户下线)、常用系统广播总结、音乐播放器)】
  4. CSS里:focus-within 的作用和用法
  5. Codeforces Round #554 Div.2 E - Neko and Flashback
  6. numpy——axis
  7. 【渝粤题库】国家开放大学2021春2517社区工作题目
  8. Kerberos 基本命令 - 持续更新
  9. ansible 基本操作(初试)
  10. 对抗搜索之【最大最小搜索】【Alpha-Beta剪枝搜索】
  11. Jenkins定时自动构建项目
  12. 【Matlab学习笔记】【细胞或颗粒检测分割】资源汇总
  13. 几款脑力训练软件分析与推荐
  14. Your GPU Compute Capability计算能力
  15. 用 Swift、Foursquare API 和 Realm 創建一個咖啡店 App
  16. Java总结IO篇之字符流
  17. 设置MAC地址和动态IP
  18. win7系统如何映射服务器,教你win7系统映射网络驱动器的操作方法
  19. sklearn基础篇(十)-- 非负矩阵分解与t-SNE
  20. 计算机视觉期刊水平,计算机视觉和模式识别领域的SCI期刊菜鸡一枚,领域,识别,EditSprings,艾德思...

热门文章

  1. 2022年工具钳工(技师)最新解析及工具钳工(技师)试题及解析
  2. 解决Google 云端硬盘,文件下载问题
  3. 关于Keil debug 出现cannot access target shutting down debug session 错误提示
  4. 微信小程序开发之——音乐播放器-播放器(3.4)
  5. App手机应用自建平台 无需编码技术 轻松制作
  6. 网络应用自建利器-Google AppEngine
  7. 北邮计算机学院石川,祝贺石川教授团队两篇论文被ACM SIGKDD2019接收!
  8. 世界上最遥远的距离,是我在if里你在else里
  9. mysql存储指纹,mysql - ZKTeco 4500指纹验证 - 堆栈内存溢出
  10. python里面列表可以同时删除吗_在python中从列表中删除项,同时对其进行迭代