树莓派安装ubuntu server 18以及netplan“隐藏”bug

一直都坚信:技术过程太顺利是学到不到东西的,只有遇到了问题了才会去研究原理性的东西,这个过程才是真正学习。

一、背景

  借用了别人的现成ECU台架,所以板子都放到了别人桌面上了,但是ECU只有局域网ip,并且有线还比较远,最好时能无线,所以就不得不使用一个中间“路由器”转换一下;之前用的是别人的NUC(Next Unit of Computing),用一个电脑做“路由器”未免太大材小用了,就想着用树莓派来当这个路由器。树莓派是pi3,32bit以及只有1g的内存,做这个路由器再合适不过了。如下图所示:

系统示意图

  pi的硬件并不够先进,一开始让同事刷了一个带桌面的ubuntu 18,直接动不了,后面他又想着帮我刷个ubuntu server,由于同事也没做过,不知道要花多少时间,就不了了之了;所以后面就让同事帮我刷了一个Raspberry Pi OS,一开始只是登录上去,虽然无线延时有点高,但仅仅敲敲命令还可以忍受,但是今天要开始debug,就实在受不了了,太慢了。所以想着自己刷个ubuntu server好了,还能看看如何在system boot中配置ssh和ip(之前Raspberry Pi OS我就挺多疑问的。)
  本来这个事情完全不值得记录的,因为随便Google搜索一下,顺利的话半小时就解决了。但是因为太不顺利了,就只能深究去查问题了,足足花了我快9个小时,连午休都没了。进而也弄清了netplan和wpa_supplicant是如何工作的;也大概的看了一下wpa_supplicant的源码(也看到了自己的差距),在802.11中sched_scan过程(因为出错时一直在打印wlan0: Failed to initiate sched scan);也趁机了解wifi信道。
  最终在折腾了一天半之后终于找到原因了,是由于netplan配置连接隐藏wifi时有bug导致的,如果想快速解决问题的,可以直接跳到第三章如何连接隐藏wifi

二、ubuntu server安装以及网络配置之顺利篇

1. ubuntu server安装过程

  1. 确定自己的树莓派版本,确定是cpu是32bit还是64bit,准备大于4G的SD卡(安装完才发现需要的空间可真小)
  2. 在下面ubutu nserver image地址找到适用于树莓派的版本
    比如树莓派的Ubuntu Server 18.04的最新可用下载,(点击直接下载)
    树莓派 2
    下载用于树莓派 2 (ARM 32 位) 的 Ubuntu Server 18.04 LTS
    树莓派 3
    下载用于树莓派 3 (ARM 32 位) 的 Ubuntu Server 18.04 LTS
    树莓派 4
    下载用于树莓派 4 (ARM 32 位) 的 Ubuntu Server 18.04 LTS
    树莓派 3(64位)
    下载用于树莓派 3 (ARM 64 位) 的 Ubuntu Server 18.04 LTS
    树莓派 4(64位)
    下载用于树莓派 4 (ARM 64 位) 的 Ubuntu Server 18.04 LTS
  3. 将准备好的SD卡与电脑相连,使用Balena Etcher(点击链接选择对应系统的软件)将下载好的ubuntu server的image下载到SD卡中。如下图:
    a). 点击选择刚刚下载的image
    b). 选择准备好的SD卡
    c). 点击flash即可开始刷写,几分钟就完成了。
    balenaetcher软件

      将准备的SD卡插到树莓派上,接上显示器,上电即开始启动了。系统初始用户名为ubuntu,密码也是ubuntu,第一次登录需要更改密码。

2. 网络配置与使用SSH登录

  上述操作后,借助显示器能够成功登录进去,配置ip(如果将有线接到带有dhcpserver的大网上,默认eth0会通过dhcp分配到ip),使能ssh,后面即可使用ssh登录了。但是可以通过system-boot的文件配置直接在第一次系统启动时就对所有网卡分配ip(包含无线连接),具体如下:
1. 在system-boot根目录新建文件ssh;这一步是使能ssh。
2. 打开system-boot/network-config,如下是我的配置,打开会有配置示例,包含有线网和无线网以及无线网各种连接方式。因为ubuntu 18以后版本默认使用netplan来做网络配置工具,所以network-config可以直接参考官网的示例,如下图,感兴趣的还可以研究下netplan 参考描述,不要去百度/google搜啦,官网写的贼详细了。强烈建议wifisoptional配置为true,否则要是连接不成功,需要等很长时间才会进入系统,如果配为false,第一次进去后可以/etc/systemd/system.conf改小等待的时间,参考链接(ubuntu 18以前的版本等有时间我也研究研究。)

# This file contains a netplan-compatible configuration which cloud-init
# will apply on first-boot. Please refer to the cloud-init documentation and
# the netplan reference for full details:
#
# https://cloudinit.readthedocs.io/
# https://netplan.io/reference
#
# Some additional examples are commented out below

version: 2
ethernets:
  eth0:
    dhcp-identifier: mac
    dhcp4: true
    optional: true
  eth1: 
    dhcp4: no
    addresses: [172.20.1.83/24]

wifis:
  wlan0:
    dhcp-identifier: mac
    dhcp4: true
    optional: true
    access-points:
      NIO-AUTH: {}
#      homessid:
#        password: "S3kr1t"
#      myotherlan:
#        password: "correct battery horse staple"
#      workssid:
#        auth:
#          key-management: eap
#          method: peap
#          identity: "me@example.com"
#          password: "passw0rd"
#          ca-certificate: /etc/my_ca.pem

连接不用密码的wifi的官网示例

  1. 根据需求改完,保存,弹出SD卡,将SD卡插入树莓派,上电启动,ping配置文件预先配置的ip,ping通后,直接使用ssh ubuntu@your ip,后面输入密码:ubuntu。剩下就自己玩了。可以配合我之前的博客使用更佳:ssh(无密码)登录linux及失败解决方案
  2. 还有第4条,据官网描述,某些版本可能没有集成openssh,那就只能用显示器接入,联网安装后再使用了。

如果上述步骤完,顺利的话,也就不到半小时,如果不连接隐藏wifi的话,几乎不会出问题;如果是隐藏wifi,还需要看下一章如何连接隐藏wifi。

三、痛苦面具之wifi连接不成功

  安装完ubuntu server,df -h一下,居然只占用1g多的SD卡,如下图,即使将所有软件更新到最新也就多1 200M的空间。在实际用起来更是特别流畅,明显的感觉到,比Raspberry Pi OS手感好多了。但是也遇到了问题了,比如我这边,第一次启动时,我发现pi没有连接上wifi,我先是用iwconfig wlan0 essid xxxx先连上wifi解决bug,由于延时降低了,远程debug起来也得心应手了,等很快的解决完bug,我就开始去折腾wifi为啥连接不成功了,走了巨多弯路,最终干到晚上那个7点半也没解决,但是回到家,发现家里的所有wifi都可以连接成功(真是无语了,我还得再去公司试试)。具体过程不表了,剩下记录我在解决问题时,学到的一些知识了。

ubuntu server实际空间占用

1. 使用ifupdown解决

  在弄了一段时间弄不好后,我其实就想用原始的办法----通过/etc/network/interface使用ifupdown来解决,一开始Raspberry Pi OS就是通过这种方法的。但是想着放假,怎么玩不是玩,解决解决这个问题,纯属当玩了,实在不行就用这个B计划,尤其我看到使用这种配置并不是默认选项时,而是需要再安装ifupdown,如下图,更加坚信我要用netplan的想法。

2. iwconfig连接wifi那些事儿

可以直接通过iwconfig来连接wifi:
1. suod iwconfig wlan0 essid "NAME" key password
2. 使用iwconfig wlan0是否成功
3. 使用sudo dhclient wlan0向dhcp服务器请求分配ip

3. netplan和wpa_supplicant是咋搞到一起的

  快记,以wlan0示例:
1. netplan启动,读取/etc/netplan/***.yaml配置文件,并声称wpa_supplicant的配置文件至/run/netplan/***.conf.
2. 启动/run/systemd/system/netplan-wpa-wlan0.service服务,进而调用wpa_supplicant
3. 最终执行的就是/sbin/wpa_supplicant -c /run/netplan/wpa-wlan0.conf -iwlan0

a. netplan是什么以及如何工作的

b. wpa_supplicant

c. netplan和wpa_supplicant

最终,我的配置如下所示,通过dhcp-identifier: mac来指定以mac地址作为dhcp分配ip的标识,这样就可以让网络管理员帮你把mac和ip绑定,保证ip不会一直在改变了。:

# This file is generated from information provided by the datasource.  Changes
# to it will not persist across an instance reboot.  To disable cloud-init's
# network configuration capabilities, write a file
# /etc/cloud/cloud.cfg.d/99-disable-network-config.cfg with the following:
# network: {config: disabled}
network:
    ethernets:
        eth0:
            dhcp4: true
            optional: true
            dhcp-identifier: mac
        eth1:
                addresses:
                        - 172.20.1.83/24
                dhcp4: false
                optional: true
    version: 2
    wifis:
        wlan0:
            dhcp-identifier: mac
            access-points:
                    "NIO-AUTH\"\n  scan_ssid=1\n# \"jinbao!": {}
            dhcp4: true
            optional: true

4. netplan和wpa_supplicatn调试

5. 如果信道不匹配是谁的锅

6. 网络驱动之没有权限运行

7. 如何连接隐藏wifi

  最终netplan连接不上wifi就是出在了我连的是隐藏wifi的原因了,这就是为什么,我连我家的wifi全部都没有问题的。当然发现的这个过程可太曲折了,主要是两个原因:
1. 不知道wpa_supplicant连接隐藏wifi的原因,下面我会分析到。
2. netplan在这块有bug或者说功能不全

a. wpa_supplicant如何连接隐藏wifi

wifi隐藏时MAC帧的表现扫描隐藏SSID,和非隐藏SSID,STA端只有在wpa_supplicant.conf有一点不同:

network={                                                              
        ssid="jinbao"                                                                    
        scan_ssid=1        ##这一项表示可以扫描隐藏wifi   
        psk="12345678"                                                                        
}                                                                                           

在不隐藏SSID时,AP广播的Beacon帧中,将携带SSID信息:

  SSID
    Element ID:         0  SSID [36]
    Length:             6 [37]
    SSID:               jinbao [38-48]

也就是在Beacon帧中携带本BSS的SSID信息,通知STA。
但是设置隐藏SSID的AP在广播的Beacon帧中没有携带SSID信息。通常的利用wpa_supplicant实现的设备搜索到的隐藏SSID的AP的SSID为空,这种为空的SSID被处理的时候被忽略了,就搜索不到。sniffer包表现为:

  SSID
    Element ID:         0  SSID [36]
    Length:             0 [37]

在STA进行扫描时,如果wpa_supplicant.conf设置了scan_ssid = 1, STA发送probe request帧,会发送两种类型的帧,第一种用来扫描所有AP,其中request帧SSID项不携带信息。同时发送另一种request帧,其中有携带SSID信息,用来搜索隐藏SSID。

STA设置隐藏后,其中携带要扫描的SSID信息的request帧,在sniffer中的描述:

802.11 Management - Probe Request
  SSID
    Element ID:         0  SSID [24]
    Length:             6 [25]
    SSID:               jinbao [26-36]

在没有设置扫描隐藏SSID时,STA发送的request帧只有第一种,不能指定获取某种SSID。这个时候隐藏SSID的AP回复的response帧包含的SSID信息为空,于是在wpa_supplicant中看到的SSID为空,framework层就把空SSID忽略了,搜索不到。

但是对于设置了扫描隐藏SSID的STA,在发送携带特定SSID信息的request后,AP端虽然隐藏了SSID,但是回复的response帧将携带自身SSID信息的,STA通过其SSID项正常扫描到网络。
回复的response帧信息:

802.11 Management - Probe Response
  SSID
    Element ID:         0  SSID [36]
    Length:             6 [37]
    SSID:               jinbao [38-48]

所以综上分析,在使用wpa_supplicant来连接wifi时,针对配置,需要填上scan_ssid=1,如下所示,否则会连接不上wifi,一直提示Failed to initiate sched scan netplan.

# /etc/wpa_supplicant/wpa_supplicant.conf
network={
    ssid="NIO-AUTH"
    scan_ssid=1
    key_mgmt=NONE
}

b. 如何使用netplan连接隐藏wifi

  前面分析了netplan和wpa_supplicant是如何搞到一起的(可能还没写哦,先写教程),所以目的是让netplan通过netplan的配置文件来生成wpa_supplicant的配置文件,并且必须带上scan_ssid=1这一项。之前弄了这么久,也没连上wifi就是因为乜有带上这一项,先说解决方案:
1. 首先我看netplan的源码,并没有相应的配置可以直接让其可以生成scan_ssid=1,所以从其他地方入手
2. 我是如下做的,充分利用转义和注释来实现,可能有些抽象;再看下面生成的配置文件,就懂了

    wifis:
        wlan0:
            access-points:
               "NIO-AUTH\"\n  scan_ssid=1\n# \"jinbao!": {}
            dhcp4: true
            optional: true
# wpa_supplicant.conf, generate by netplan

network={
  ssid="NIO-AUTH"
  scan_ssid=1
# "jinbao!"
  key_mgmt=NONE
}

  上面基本上是自己看着wpa_supplicant的配置文件突发奇想的琢磨出来(看了源码和官网实在没办法了),一试,真的可以。其实也很好理解的。否则只有wifi名,生成的wpa_supplicant的配置文件就如下,而没有scan_ssid=1,如果仅仅是为了连上隐藏wifi,到此就可以了。下面再分析,还有没有其他办法

# wpa_supplicant.conf, generate by netplan

network={
  ssid="NIO-AUTH"
  key_mgmt=NONE
}

  在自己解决后,我也上网搜了,发现全网还有一个跟我同样的疑问,并且用了一样的解决办法,参考链接。然后我又去看了netplan的源码,发现最新的源码加上了,如下代码,也就可以通过设置hidden字段来连接隐藏wifi。但是,我再官网下载的ubuntu server自带的netplan是不支持该字段了,如下图,所以应该是要更高版本才行。

//....Omit
            if (ap->hidden) {
                g_string_append(s, "  scan_ssid=1\n");
            }
/.....Omit

netplan当前版本不支持hidden

如下图所示,在一年前netplan已经将hidden加入了,所以使用最新的netplan是可以直接如下配置就可以连接隐藏wifi了。

    wifis:
        wlan0:
            dhcp-identifier: mac
            access-points:
               "NIO-AUTH": 
                   hidden: true
            dhcp4: true
            optional: true

netplan一年前就支持hidden

四、写在最后

  这一天半过充实而又有价值,好在没放弃,最终问题解决了,还留了几个坑没写,等有时间时再回顾再补上,发现了netplan真隐藏bug,虽然人家早就修复了,如果当初顺手就把netplan给升级了,可能没有这么强的动力还去刨根问底了。总之经过这次,大概知道了linux配置ip的套路了,知道了后面即使换到其他发行版本也能在system-boot就配置好网络了。另外,学到了如man app | grep xxxx这种套路了,<( ̄▽ ̄)/啊哈哈,以前还觉得man这个命令也太不又好了。

评论

  1. 2年前
    2022-7-26 4:28:18

    Im pretty pleased to find this great site. I need to to thank you for ones time for this particularly fantastic read!! I definitely loved every little bit of it and I have you saved as a favorite to see new stuff on your blog.

  2. 2年前
    2022-7-30 0:40:07

    A fascinating discussion is definitely worth comment. I do think that you ought to publish more about this topic, it may not be a taboo subject but typically people dont speak about such issues. To the next! Many thanks!!

  3. 2年前
    2022-8-19 18:47:34

    At this moment I am redady to doo my breakfast, afterward having my breakfast coming yet again to read further news. Julio Kehler

  4. 2年前
    2022-8-20 0:44:55

    I have recently started a web site, the information you offer on this web site has helped me tremendously. Thanks for all of your time &amp; work. Tonya Sydney Hurley

  5. 2年前
    2022-8-20 11:16:02

    I think what you wrote made a great deal of sense. Dante Bentele

  6. 2年前
    2022-8-20 20:45:48

    Thanks for sharing your info. I really appreciate your efforts and I will be waiting for your next post thank you once again. Paris Lanahan

  7. Itís difficult to find knowledgeable people about this subject, but you sound like you know what youíre talking about! Thanks

  8. 2年前
    2022-8-23 9:48:18

    I love the efforts you have put in this, appreciate it for all the great articles . Jacinto Offner

  9. 2年前
    2022-8-23 14:58:55

    Hello there! I just wish to give you a huge thumbs up for your great info you have here on this post. I am returning to your blog for more soon. Edwardo Prinz

  10. 2年前
    2022-9-02 19:45:16

    Fastidious answer back in return of this question with genuine arguments and describing the whole thing about that. Emilio Stankus

  11. 2年前
    2022-9-04 23:27:07

    Raul cu care avem de-a face depaseste frontierele fizicului. Olen Shippey

  12. 2年前
    2022-9-06 18:08:16

    very nice post ty admin

  13. 2年前
    2022-9-06 19:34:31

    great content thanks

  14. 2年前
    2022-9-06 20:31:55

    Admiring the time and effort you put into your website and in depth information you provide. Noble Barrish

  15. 2年前
    2022-9-06 23:01:54

    Enjoyed examining this, very good stuff, regards . "I will do my best. That is all I can do. I ask for your help-and God's." by Lyndon B. Johnson. Leif Hannagan

  16. 2年前
    2022-9-07 0:08:35

    Always a big fan of linking to bloggers that I really like but dont get quite a bit of link adore from. Genaro Patzke

  17. 2年前
    2022-9-07 3:17:16

    On this console, you will there is the God of War. Jerry Handsaker

  18. 2年前
    2022-9-08 3:23:01

    Im thankful for the post. Much thanks again. Awesome. Randell Villalovos

  19. 2年前
    2022-9-22 10:05:26

    Good post. I learn something totally new and challenging on sites I stumbleupon everyday. It will always be helpful to read content from other writers and practice a little something from other websites.

  20. 2年前
    2022-10-13 19:44:38

    אני מאוד ממליץ על אתר הזה כנסו עכשיו ותהנו ממגוון רחב של בחורות ברמה מאוד גבוהה. רק באתר ישראל נייט לאדי <a href="https://romantik69.co.il/">https://romantik69.co.il/</a&gt;

  21. 1年前
    2022-12-03 18:23:43

    I could not refrain from commenting. Exceptionally well written!

    My web site: <a href="http://tinyurl.com/2zxl3qnb">tracfone</a&gt;

  22. 待审核
    1年前
    2023-1-29 19:08:04

    树莓派安装ubuntu server 18以及netplan“隐藏”bug - 金宝的博客
    [url=http://www.gtr534xn0m1uc0s49781yxusvq68893bs.org/]usgfstqqhwy[/url]
    sgfstqqhwy http://www.gtr534xn0m1uc0s49781yxusvq68893bs.org/
    <a href="http://www.gtr534xn0m1uc0s49781yxusvq68893bs.org/">asgfstqqhwy</a&gt;

  23. 待审核
    1年前
    2023-1-30 12:01:24

    树莓派安装ubuntu server 18以及netplan“隐藏”bug - 金宝的博客
    [url=http://www.gt918yiip5x968s644uk57dz15ja0n9vs.org/]ugddlzinm[/url]
    gddlzinm http://www.gt918yiip5x968s644uk57dz15ja0n9vs.org/
    <a href="http://www.gt918yiip5x968s644uk57dz15ja0n9vs.org/">agddlzinm</a&gt;

  24. 待审核
    1年前
    2023-2-11 4:41:35

    树莓派安装ubuntu server 18以及netplan“隐藏”bug - 金宝的博客
    fqwhiospw http://www.g70r8ve640i413t646nb66jnvqnc20czs.org/
    <a href="http://www.g70r8ve640i413t646nb66jnvqnc20czs.org/">afqwhiospw</a&gt;
    [url=http://www.g70r8ve640i413t646nb66jnvqnc20czs.org/]ufqwhiospw[/url]

  25. 待审核
    1年前
    2023-2-17 8:28:45

    树莓派安装ubuntu server 18以及netplan“隐藏”bug - 金宝的博客
    [url=http://www.g2785rm09zjz1g92oo5bj2gomb7m9994s.org/]utjwhsrxtd[/url]
    <a href="http://www.g2785rm09zjz1g92oo5bj2gomb7m9994s.org/">atjwhsrxtd</a&gt;
    tjwhsrxtd http://www.g2785rm09zjz1g92oo5bj2gomb7m9994s.org/

  26. 待审核
    1年前
    2023-3-03 11:55:49

    great i will visit your site again.

  27. 待审核
    1年前
    2023-3-03 17:57:37

    树莓派安装ubuntu server 18以及netplan“隐藏”bug - 汽车与it技术学习与分享
    norzylrb http://www.g435373m4bko6ap5d9w7p442qqvt3w7cs.org/
    <a href="http://www.g435373m4bko6ap5d9w7p442qqvt3w7cs.org/">anorzylrb</a&gt;
    [url=http://www.g435373m4bko6ap5d9w7p442qqvt3w7cs.org/]unorzylrb[/url]

  28. 待审核
    1年前
    2023-3-18 21:21:11

    树莓派安装ubuntu server 18以及netplan“隐藏”bug - 汽车与it技术学习与分享
    [url=http://www.guvc642ey02q2v3ei0c19dw8y2sw6369s.org/]uedrwzztvv[/url]
    <a href="http://www.guvc642ey02q2v3ei0c19dw8y2sw6369s.org/">aedrwzztvv</a&gt;
    edrwzztvv http://www.guvc642ey02q2v3ei0c19dw8y2sw6369s.org/

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇