![编辑1](https://linux22.com/image/202353/%E7%BC%96%E8%BE%911.png)
每次我将 Raspberry Pi 连接到计算机的 USB 端口时,我总是需要将与 Linux Mint 计算机创建的连接设置为共享。我通过网络管理器执行此操作,如屏幕截图所示(希腊语用户界面):
这有点乏味,每次都要花 2-3 分钟将连接设置为共享,以便进行 ssh 连接。所以我希望一旦设备插入共享连接就会自动创建,因此我不需要每次将树莓派插入USB端口时都通过网络管理器配置网络接口。
有没有办法做到这一点。到目前为止,一旦我输入,lsusb
我就会得到这个结果:
$ lsusb
Bus 002 Device 002: ID 05e3:0732 Genesys Logic, Inc. All-in-One Cardreader
Bus 002 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub
Bus 001 Device 003: ID 1a40:0101 Terminus Technology Inc. Hub
Bus 001 Device 002: ID 4e53:5407
Bus 001 Device 010: ID 0925:3881 Lakeview Research Saleae Logic
Bus 001 Device 009: ID 0525:a4a2 Netchip Technology, Inc. Linux-USB Ethernet/RNDIS Gadget
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
插入 USB 后创建的设备是:
Bus 001 Device 009: ID 0525:a4a2 Netchip Technology, Inc. Linux-USB Ethernet/RNDIS Gadget
一。
还dmesg
显示以下内容:
[ 2015.792407] usb 1-3: new full-speed USB device number 8 using xhci_hcd
[ 2016.368398] usb 1-3: new high-speed USB device number 9 using xhci_hcd
[ 2016.517429] usb 1-3: New USB device found, idVendor=0525, idProduct=a4a2, bcdDevice= 5.10
[ 2016.517434] usb 1-3: New USB device strings: Mfr=1, Product=2, SerialNumber=0
[ 2016.517437] usb 1-3: Product: RNDIS/Ethernet Gadget
[ 2016.517441] usb 1-3: Manufacturer: Linux 5.10.63+ with 20980000.usb
[ 2016.518553] cdc_subset: probe of 1-3:1.0 failed with error -22
[ 2016.520510] cdc_ether 1-3:1.0 usb0: register 'cdc_ether' at usb-0000:00:14.0-3, CDC Ethernet Device, 12:e2:73:0a:fa:92
[ 2016.550226] cdc_ether 1-3:1.0 enp0s20f0u3: renamed from usb0
[ 2017.365925] [UFW BLOCK] IN=br-c2a79e951b81 OUT= MAC= SRC=fe80:0000:0000:0000:0042:f4ff:fe99:0011 DST=ff02:0000:0000:0000:0000:0000:0000:0001 LEN=89 TC=0 HOPLIMIT=1 FLOWLBL=617835 PROTO=UDP SPT=22161 DPT=161 LEN=49
[ 2017.532279] [UFW BLOCK] IN=br-c2a79e951b81 OUT= MAC= SRC=fe80:0000:0000:0000:0042:f4ff:fe99:0011 DST=ff02:0000:0000:0000:0000:0000:0000:0001 LEN=89 TC=0 HOPLIMIT=1 FLOWLBL=617835 PROTO=UDP SPT=22161 DPT=161 LEN=49
[ 2042.958713] IPv6: ADDRCONF(NETDEV_CHANGE): enp0s20f0u3: link becomes ready
[ 2216.944793] audit: type=1400 audit(1644678209.643:133): apparmor="DENIED" operation="capable" profile="/snap/snapd/14549/usr/lib/snapd/snap-confine" pid=10514 comm="snap-confine" capability=4 capname="fsetid"
[ 2220.764823] audit: type=1326 audit(1644678213.463:134): auid=4294967295 uid=1000 gid=1000 ses=4294967295 pid=10514 comm="telegram-deskto" exe="/snap/telegram-desktop/3544/usr/bin/telegram-desktop" sig=0 arch=c000003e syscall=314 compat=0 ip=0x7f353fbdc89d code=0x50000
[ 2223.187662] audit: type=1400 audit(1644678215.883:135): apparmor="DENIED" operation="open" profile="snap.telegram-desktop.telegram-desktop" name="/etc/openal/alsoft.conf" pid=10514 comm="telegram-deskto" requested_mask="r" denied_mask="r" fsuid=1000 ouid=0
[ 2224.579172] audit: type=1326 audit(1644678217.275:136): auid=4294967295 uid=1000 gid=1000 ses=4294967295 pid=10514 comm="telegram-deskto" exe="/snap/telegram-desktop/3544/usr/bin/telegram-desktop" sig=0 arch=c000003e syscall=203 compat=0 ip=0x7f3541bb26e1 code=0x50000
[ 2224.579181] audit: type=1326 audit(1644678217.275:137): auid=4294967295 uid=1000 gid=1000 ses=4294967295 pid=10514 comm="telegram-deskto" exe="/snap/telegram-desktop/3544/usr/bin/telegram-desktop" sig=0 arch=c000003e syscall=141 compat=0 ip=0x7f353fbd834b code=0x50000
[ 2224.579187] audit: type=1326 audit(1644678217.275:138): auid=4294967295 uid=1000 gid=1000 ses=4294967295 pid=10514 comm="telegram-deskto" exe="/snap/telegram-desktop/3544/usr/bin/telegram-desktop" sig=0 arch=c000003e syscall=203 compat=0 ip=0x7f3541bb26e1 code=0x50000
[ 2224.579224] audit: type=1326 audit(1644678217.275:139): auid=4294967295 uid=1000 gid=1000 ses=4294967295 pid=10514 comm="telegram-deskto" exe="/snap/telegram-desktop/3544/usr/bin/telegram-desktop" sig=0 arch=c000003e syscall=141 compat=0 ip=0x7f353fbd834b code=0x50000
[ 2224.579342] audit: type=1326 audit(1644678217.275:140): auid=4294967295 uid=1000 gid=1000 ses=4294967295 pid=10514 comm="telegram-deskto" exe="/snap/telegram-desktop/3544/usr/bin/telegram-desktop" sig=0 arch=c000003e syscall=203 compat=0 ip=0x7f3541bb26e1 code=0x50000
[ 2224.579352] audit: type=1326 audit(1644678217.275:141): auid=4294967295 uid=1000 gid=1000 ses=4294967295 pid=10514 comm="telegram-deskto" exe="/snap/telegram-desktop/3544/usr/bin/telegram-desktop" sig=0 arch=c000003e syscall=141 compat=0 ip=0x7f353fbd834b code=0x50000
[ 2224.579507] audit: type=1326 audit(1644678217.275:142): auid=4294967295 uid=1000 gid=1000 ses=4294967295 pid=10514 comm="telegram-deskto" exe="/snap/telegram-desktop/3544/usr/bin/telegram-desktop" sig=0 arch=c000003e syscall=203 compat=0 ip=0x7f3541bb26e1 code=0x50000
[ 2224.579515] audit: type=1326 audit(1644678217.275:143): auid=4294967295 uid=1000 gid=1000 ses=4294967295 pid=10514 comm="telegram-deskto" exe="/snap/telegram-desktop/3544/usr/bin/telegram-desktop" sig=0 arch=c000003e syscall=203 compat=0 ip=0x7f3541bb26e1 code=0x50000
[ 2224.579551] audit: type=1326 audit(1644678217.275:144): auid=4294967295 uid=1000 gid=1000 ses=4294967295 pid=10514 comm="telegram-deskto" exe="/snap/telegram-desktop/3544/usr/bin/telegram-desktop" sig=0 arch=c000003e syscall=203 compat=0 ip=0x7f3541bb26e1 code=0x50000
[ 3121.350145] usb 1-5: new high-speed USB device number 10 using xhci_hcd
[ 3121.498265] usb 1-5: New USB device found, idVendor=0925, idProduct=3881, bcdDevice= 0.01
[ 3121.498271] usb 1-5: New USB device strings: Mfr=0, Product=0, SerialNumber=0
[ 3122.347647] [UFW BLOCK] IN=br-c2a79e951b81 OUT= MAC= SRC=fe80:0000:0000:0000:0042:f4ff:fe99:0011 DST=ff02:0000:0000:0000:0000:0000:0000:0001 LEN=89 TC=0 HOPLIMIT=1 FLOWLBL=617835 PROTO=UDP SPT=22161 DPT=161 LEN=49
[ 3122.513998] [UFW BLOCK] IN=br-c2a79e951b81 OUT= MAC= SRC=fe80:0000:0000:0000:0042:f4ff:fe99:0011 DST=ff02:0000:0000:0000:0000:0000:0000:0001 LEN=89 TC=0 HOPLIMIT=1 FLOWLBL=617835 PROTO=UDP SPT=22161 DPT=161 LEN=49
[ 3122.680261] [UFW BLOCK] IN=br-c2a79e951b81 OUT= MAC= SRC=fe80:0000:0000:0000:0042:f4ff:fe99:0011 DST=ff02:0000:0000:0000:0000:0000:0000:0001 LEN=89 TC=0 HOPLIMIT=1 FLOWLBL=617835 PROTO=UDP SPT=22161 DPT=161 LEN=49
[ 3122.880905] [UFW BLOCK] IN=enp3s0 OUT= MAC= SRC=fe80:0000:0000:0000:1be3:4633:095f:d015 DST=ff02:0000:0000:0000:0000:0000:0000:0001 LEN=64 TC=0 HOPLIMIT=1 FLOWLBL=663885 PROTO=UDP SPT=8612 DPT=8612 LEN=24
[ 3122.880917] [UFW BLOCK] IN=enp3s0 OUT= MAC= SRC=fe80:0000:0000:0000:1be3:4633:095f:d015 DST=ff02:0000:0000:0000:0000:0000:0000:0001 LEN=64 TC=0 HOPLIMIT=1 FLOWLBL=837265 PROTO=UDP SPT=8612 DPT=8610 LEN=24
[ 3122.880928] [UFW BLOCK] IN=br-c2a79e951b81 OUT= MAC= SRC=fe80:0000:0000:0000:0042:f4ff:fe99:0011 DST=ff02:0000:0000:0000:0000:0000:0000:0001 LEN=64 TC=0 HOPLIMIT=1 FLOWLBL=255464 PROTO=UDP SPT=8612 DPT=8612 LEN=24
[ 3122.880938] [UFW BLOCK] IN=br-c2a79e951b81 OUT= MAC= SRC=fe80:0000:0000:0000:0042:f4ff:fe99:0011 DST=ff02:0000:0000:0000:0000:0000:0000:0001 LEN=64 TC=0 HOPLIMIT=1 FLOWLBL=341915 PROTO=UDP SPT=8612 DPT=8610 LEN=24
[ 3122.880946] [UFW BLOCK] IN=vethc930aa4 OUT= MAC= SRC=fe80:0000:0000:0000:0814:60ff:fee9:705f DST=ff02:0000:0000:0000:0000:0000:0000:0001 LEN=64 TC=0 HOPLIMIT=1 FLOWLBL=674828 PROTO=UDP SPT=8612 DPT=8612 LEN=24
[ 3122.880953] [UFW BLOCK] IN=vethc930aa4 OUT= MAC= SRC=fe80:0000:0000:0000:0814:60ff:fee9:705f DST=ff02:0000:0000:0000:0000:0000:0000:0001 LEN=64 TC=0 HOPLIMIT=1 FLOWLBL=1048135 PROTO=UDP SPT=8612 DPT=8610 LEN=24
[ 3122.880960] [UFW BLOCK] IN=enp0s20f0u3 OUT= MAC= SRC=fe80:0000:0000:0000:1eaf:0969:516b:39cb DST=ff02:0000:0000:0000:0000:0000:0000:0001 LEN=64 TC=0 HOPLIMIT=1 FLOWLBL=326809 PROTO=UDP SPT=8612 DPT=8612 LEN=24
我运行的发行版是 Linux Mint 19。我想udev
这是我必须遵循的方法,但我不知道如何做。
但我想要的是,一旦我将树莓派插入小工具模式,将连接设置为
编辑1
现在,我设法设置一个 udev 规则以便为其命名:
SUBSYSTEM=="net", ACTION=="add", ATTRS{idVendor}=="0525", ATTRS{idProduct}=="a4a2", NAME="rpi0"
但我所做的就是设法将接口从 重命名usb0
为rpi0
.但我对如何指定 netowkr 管理器为我插件的接口进行特定设置感到困惑。
一旦我插入树莓派,我想要什么,不要尝试通过创建文件来自动连接/etc/NetworkManager/conf.d/rpi0.conf
[main]
no-auto-default=rpi0
但每次我尝试连接时,都会创建一个新的网络连接。
有没有办法共享我的连接并绕过整个网络管理器?
答案1
您必须通过 udev 规则来管理它,并且根本不允许网络 nagagher 来管理它。
步骤1:设置静态ip为rasbnerry pi
首先,您应该最后一次将网络设置临时设置为共享,并通过添加以下/etc/dhcpcd.conf
设置为树莓派设置静态 IP:
interface usb0
static ip_address=10.42.0.62/24
static routers=10.42.0.1
static domain_name_servers=1.1.1.1
但在执行此操作之前,首先确保 usb0 接口是树莓派使用的接口,通过usb
运行ifconfig
.建议设置中看到的设置,ifconfig
因为相对安全。ifconfig
就我而言usb0
,是:
usb0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 10.42.0.62 netmask 255.255.255.0 broadcast 10.42.0.255
inet6 fe80::2f8c:1871:13b2:dc30 prefixlen 64 scopeid 0x20<link>
ether ce:bb:5c:2b:f2:46 txqueuelen 1000 (Ethernet)
RX packets 230 bytes 19437 (18.9 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 891 bytes 56964 (55.6 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
步骤 2 设置特定的网络接口名称和 ip 以及树莓派连接到的电脑。
您需要使用 udev 作为管理接口的方法。就我而言,我创建了一个名为/etc/udev/rules.d/99_rpi_net_renamer.rules
:
SUBSYSTEM=="net", ACTION=="add", ATTRS{idVendor}=="0525", ATTRS{idProduct}=="a4a2", NAME="rpi0", RUN+="/sbin/ifconfig rpi0 down", RUN+="/sbin/ifconfig rpi0 10.42.0.61/24 broadcast 10.42.0.255",RUN+="/sbin/ifconfig rpi0 up"
我所做的基于lsusb
我列出了所有设备:
lsusb
Bus 002 Device 002: ID 05e3:0732 Genesys Logic, Inc. All-in-One Cardreader
Bus 002 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub
Bus 001 Device 003: ID 1a40:0101 Terminus Technology Inc. Hub
Bus 001 Device 002: ID 4e53:5407
Bus 001 Device 010: ID 0925:3881 Lakeview Research Saleae Logic
Bus 001 Device 009: ID 0525:a4a2 Netchip Technology, Inc. Linux-USB Ethernet/RNDIS Gadget
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
为了找到树莓派,我将其插入并从 USB 端口拔出,同时我lsusb
通过一次又一次地运行它来比较各种输出。就我而言,该设备是:
Bus 001 Device 009: ID 0525:a4a2 Netchip Technology, Inc. Linux-USB Ethernet/RNDIS Gadget
所以在udev规则中我放置了一条记录:
SUBSYSTEM=="net", ACTION=="add", ATTRS{idVendor}=="XXXX", ATTRS{idProduct}=="YYY", NAME="rpi0", RUN+="/sbin/ifconfig rpi0 down", RUN+="/sbin/ifconfig rpi0 10.42.0.61/24 broadcast 10.42.0.255",RUN+="/sbin/ifconfig rpi0 up"
XXXX
过去0525
和YYYY
现在在哪里a4a2
。根据经验,lsusb
您应该查找以下格式的记录:
Bus 001 Device 009: ID XXXX:YYYY Netchip Technology, Inc. Linux-USB Ethernet/RNDIS Gadget
和分别XXXX
是YYYY
和。ATTRS{idVendor}
ATTRS{idProduct}
步骤 3:禁用网络管理器的管理。
就我而言,我制定了以下规则/etc/NetworkManager/conf.d/rpi0.conf
:
[main]
no-auto-default=rpi0
[keyfile]
unmanaged-devices=rpi0
第 4 步:完成并应用更改。
为了应用更改,请运行:
sudo udevadm control --reload-rules && sudo udevadm trigger
sudo service network manager restart
然后,拔下并再次插入 Raspberry Pi,以查看更改是否有效。