Linux 树莓派自组织网络

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15423325/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 22:27:44  来源:igfitidea点击:

Raspberry Pi ad-hoc networking

linuxnetworkingwirelessraspberry-piadhoc

提问by Andy

I want to try some networking projects with Raspberry Pis, and I need to just send packets between a pair of pis. I would be happy as a first step just being able to ping between to Raspberry Pis in ad-hoc mode. I have not successfully done this despite looking at several tutorials and examples online.

我想用树莓派尝试一些网络项目,我只需要在一对 pi 之间发送数据包。作为第一步,我很高兴能够在临时模式下在 Raspberry Pi 之间进行 ping 操作。尽管在网上查看了几个教程和示例,但我还没有成功地做到这一点。

I have 2x Raspberry Pis with the Debian Wheezy OS installed. I am using the following USB adapter which I installed firmware for on both pis and tested that they work by connected them in managed mode to a router: Bus 001 Device 004: ID 050d:945a Belkin Components F7D1101 v1 Basic Wireless Adapter [Realtek RTL8188SU]

我有 2 个 Raspberry Pi,并安装了 Debian Wheezy OS。我正在使用以下 USB 适配器,我在两个 pi 上都安装了固件,并通过将它们以托管模式连接到路由器来测试它们是否工作:Bus 001 Device 004: ID 050d:945a Belkin Components F7D1101 v1 Basic Wireless Adapter [Realtek RTL8188SU]

Here are is some printouts about the networking information:

以下是一些有关网络信息的打印输出:

/etc/network/interfaces at each pi:

每个 pi 的 /etc/network/interfaces:


pi1@raspberrypi ~ $ cat /etc/network/interfaces 
auto lo
iface lo inet loopback
iface eth0 inet dhcp
auto wlan0
iface wlan0 inet manual
    address 192.168.2.1
    netmask 255.255.255.0
    wireless-channel 4
    wireless-essid pi-ad-hoc
    wireless-mode ad-hoc
pi2@raspberrypi ~ $ cat /etc/network/interfaces 
auto lo
iface lo inet loopback
iface eth0 inet dhcp
auto wlan0
iface wlan0 inet manual
    address 192.168.2.2
    netmask 255.255.255.0
    wireless-channel 4
    wireless-essid pi-ad-hoc
    wireless-mode ad-hoc

ifconfig at each pi:

每个 pi 的 ifconfig:


pi1@raspberrypi ~ $ ifconfig wlan0
wlan0     Link encap:Ethernet  HWaddr ec:1a:59:46:8e:5a  
          inet addr:192.168.2.1  Bcast:192.168.2.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:0 errors:0 dropped:26 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)
pi2@raspberrypi ~ $ ifconfig wlan0
wlan0     Link encap:Ethernet  HWaddr ec:1a:59:46:59:0a  
          inet addr:192.168.2.2  Bcast:192.168.2.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:0 errors:0 dropped:6 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)

iwconfig at each pi:

每个 pi 上的 iwconfig:


pi1@raspberrypi ~ $ iwconfig wlan0
wlan0     IEEE 802.11bg  ESSID:"pi-ad-hoc"  Nickname:"rtl_wifi"
          Mode:Ad-Hoc  Cell: 02:11:87:FA:4A:02   Bit Rate:54 Mb/s
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 pi2@raspberrypi ~ $ iwconfig wlan0 wlan0 IEEE 802.11bg ESSID:"pi-ad-hoc" Nickname:"rtl_wifi" Mode:Ad-Hoc Cell: 02:11:87:C4:F2:01 Bit Rate:54 Mb/s
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

route at one pi (identical on other pi):

在一个 pi 上的路线(在另一个 pi 上相同):


pi1@raspberrypi ~ $ route
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
default         192.168.1.1     0.0.0.0         UG    0      0        0 eth0
192.168.1.0     *               255.255.255.0   U     0      0        0 eth0
192.168.2.0     *               255.255.255.0   U     0      0        0 wlan0

iwlist scan at each pi:

iwlist 扫描每个 pi:


pi1@raspberrypi ~ $ sudo iwlist wlan0 scan
wlan0     Scan completed :
          Cell 01 - Address: 02:11:87:FA:4A:02
                    ESSID:"pi-ad-hoc"
                    Protocol:IEEE 802.11bg
                    Mode:Ad-Hoc
                    Frequency:2.427 GHz (Channel 4)
                    Encryption key:off
                    Bit Rates:1 Mb/s; 2 Mb/s; 5.5 Mb/s; 11 Mb/s; 6 Mb/s
                              9 Mb/s; 12 Mb/s; 18 Mb/s; 24 Mb/s; 36 Mb/s
                              48 Mb/s; 54 Mb/s
                    Quality:0  Signal level:0  Noise level:0
          Cell 04 - Address: 02:11:87:C4:F2:01
                    ESSID:"pi-ad-hoc"
                    Protocol:IEEE 802.11bg
                    Mode:Ad-Hoc
                    Frequency:2.427 GHz (Channel 4)
                    Encryption key:off
                    Bit Rates:1 Mb/s; 2 Mb/s; 5.5 Mb/s; 11 Mb/s; 6 Mb/s
                              9 Mb/s; 12 Mb/s; 18 Mb/s; 24 Mb/s; 36 Mb/s
                              48 Mb/s; 54 Mb/s
                    Signal level=100/100
pi2@raspberrypi ~ $ sudo iwlist wlan0 scan wlan0 Scan completed : Cell 01 - Address: 02:11:87:C4:F2:01 ESSID:"pi-ad-hoc" Protocol:IEEE 802.11bg Mode:Ad-Hoc Frequency:2.427 GHz (Channel 4) Encryption key:off Bit Rates:1 Mb/s; 2 Mb/s; 5.5 Mb/s; 11 Mb/s; 6 Mb/s 9 Mb/s; 12 Mb/s; 18 Mb/s; 24 Mb/s; 36 Mb/s 48 Mb/s; 54 Mb/s Quality:0 Signal level:0 Noise level:0 Cell 02 - Address: 02:11:87:FA:4A:02 ESSID:"pi-ad-hoc" Protocol:IEEE 802.11bg Mode:Ad-Hoc Frequency:2.427 GHz (Channel 4) Encryption key:off Bit Rates:1 Mb/s; 2 Mb/s; 5.5 Mb/s; 11 Mb/s; 6 Mb/s 9 Mb/s; 12 Mb/s; 18 Mb/s; 24 Mb/s; 36 Mb/s 48 Mb/s; 54 Mb/s Signal level=100/100

Ping does not work, and no networking seems to work between them. From iwconfig, you can see that they each have a different "Cell" address (not the same Cell as in the iwlist scan) which is the pseudo-base station ID that is used to define an ad-hoc network (my best understanding from what I've read). Also, from the iwlist, each pi can see their own plus the other pi's ad-hoc network. I assume they need to select the same Cell id to communicate, and I'm unsure how to get them to do this automatically. I tried statically forcing these to be the same with the following command at each pi which did not change the cell id and therefore did not work:

Ping 不起作用,并且它们之间似乎没有网络。从 iwconfig 中,您可以看到它们每个都有不同的“Cell”地址(与 iwlist 扫描中的 Cell 不同),这是用于定义 ad-hoc 网络的伪基站 ID(我最好的理解来自我读过的)。此外,从 iwlist 中,每个 pi 都可以看到自己的以及另一个 pi 的临时网络。我假设他们需要选择相同的 Cell id 进行通信,我不确定如何让他们自动执行此操作。我尝试静态强制这些与每个 pi 上的以下命令相同,但没有更改单元格 ID,因此不起作用:

sudo iwconfig wlan0 ap (some address)

I also tried a solution which uses ap_scan=2 in the wpa_supplicant config which did not seem to help.

我还尝试了一个在 wpa_supplicant 配置中使用 ap_scan=2 的解决方案,但似乎没有帮助。

Anyone have any idea what I've done wrong?

任何人都知道我做错了什么?

Thanks, Andy.

谢谢,安迪。

回答by Andy

After some searching I found that the Belkin USB adapter I was using apparently didn't have ad-hoc mode support with the linux drivers. I bought some other wireless USB adapaters that worked great "Edimax EW-7811Un 150 Mbps Wireless 11n Nano Size USB Adapter". They are cheaper, smaller, and they worked in ad-hoc mode without even needing to worry drivers. The details I put for troubleshooting can be used as a guide if you are also wanting to do ad-hoc raspberry PI projects.

经过一番搜索,我发现我使用的 Belkin USB 适配器显然没有对 linux 驱动程序的 ad-hoc 模式支持。我买了一些其他无线 USB 适配器,它们运行良好“Edimax EW-7811Un 150 Mbps 无线 11n Nano 尺寸 USB 适配器”。它们更便宜、更小,而且它们以临时模式工作,甚至无需担心司机。如果您还想做临时 raspberry PI 项目,我为故障排除提供的详细信息可以用作指南。

回答by HDTV4free.info

This works for me in /etc/network/interfaces:

这在 /etc/network/interfaces 中对我有用:

auto wlan0
allow-hotplug wlan0
iface wlan0 inet static
wireless-essid "MYPINET"
wireless-channel 3
wireless-mode ad-hoc
wireless-ap 11:5F:02:38:5C:45
address 192.168.10.1
netmask 255.255.255.0

The essid, channel and ap can be any valid value (same on all your Pi's). Make sure to assign different addresses on the same subnet to your different Pi's and you should be fine. The ap defines the cell ID that was mentioned above. FWIW I am using a TP-link WN725N. It has the RTL8188CUS chipset and works fine out of the box even though dmesg indicates the Pi is treating it as an RTL8192.

essid、channel 和 ap 可以是任何有效值(在所有 Pi 上都相同)。确保将同一子网上的不同地址分配给不同的 Pi,你应该没问题。ap 定义了上面提到的小区 ID。FWIW 我正在使用 TP-link WN725N。它具有 RTL8188CUS 芯片组,开箱即用,即使 dmesg 表明 Pi 将其视为 RTL8192。

回答by MattG

I was unsuccessful with any adapter using the RTL8188CUSchipset. Luckily, I had a number of Ralink RT5370dongles (from this kit) that support the nl80211interface and ad-hoc mode.

使用该RTL8188CUS芯片组的任何适配器均未成功。幸运的是,我有许多支持接口和 ad-hoc 模式的 RalinkRT5370加密狗(来自这个工具包nl80211

My solution involves using wpa_supplicantand is configured with 2 files. Ensure that the nl80211driver is installed:

我的解决方案涉及使用wpa_supplicant并配置了 2 个文件。确保nl80211已安装驱动程序:

sudo apt-get install libnl1

Next, create the following wpa_supplicantconfiguration file called /etc/wpa_supplicant-adhoc.confon each Pi:

接下来,创建在每个 Pi 上wpa_supplicant调用的以下配置文件/etc/wpa_supplicant-adhoc.conf

ctrl_interface=DIR=/run/wpa_supplicant GROUP=netdev
update_config=1
ap_scan=2

network={
  ssid="pihoc_wpa"
  mode=1
  frequency=2462
  proto=WPA
  key_mgmt=WPA-NONE
  pairwise=NONE
  group=TKIP
  psk="password"
} 

where you can choose the ssid, frequency(look herefor valid values), and psk. Make sure that you are part of the user group net-devusing the command

您可以在其中选择ssid, frequency在此处查找有效值)和psknet-dev使用命令确保您是用户组的一部分

getent group netdev

and if not, you can add yourself using

如果没有,您可以使用添加自己

sudo usermod -a -G netdev userName

Next, add the following block to the /etc/network/interfacesfile on each Pi:

接下来,将以下块添加到/etc/network/interfaces每个 Pi 上的文件中:

auto wlan0
allow-hotplug wlan0
iface wlan0 inet static
   address 10.10.2.1
   netmask 255.255.255.0
pre-up wpa_supplicant -B -D nl80211 -i wlan0 -c /etc/wpa_supplicant-adhoc.conf

where each Pi has a different addressfield beginning with 10.10.2.. Also, if your RT5370adapter is using an interface other than wlan0(e.g. wlan1, wlan2, etc.), be sure to use that interface name instead.

其中每个 Pi 都有一个以address开头的不同字段10.10.2.。另外,如果您的RT5370适配器使用以外的接口wlan0(如wlan1wlan2等),一定要使用该接口的名称来代替。

At this point, the Pis should automatically join the network upon being rebooted. Test the connection by pinging or using ssh, for example run the following from the agent with IP address 10.10.2.1:

此时,Pis 应在重新启动后自动加入网络。通过 ping 或使用 ssh 测试连接,例如从具有 IP 地址的代理运行以下命令10.10.2.1

ssh 10.10.2.2

to access the agent with IP address 10.10.2.2.

使用 IP 地址访问代理10.10.2.2

The steps listed here are adapted from this Arch Linux wiki articleand this Raspberry Pi forum discussion.

此处列出的步骤改编自此 Arch Linux wiki 文章此 Raspberry Pi 论坛讨论