Raspbian镜像无头烧录
创始人
2024-05-29 01:04:52
0

Raspbian镜像无头烧录

  • 1. 源由
  • 2. 需求
  • 3. 分析
  • 4. 步骤
    • 4.1 删除tf卡分区内容
    • 4.2 balena烧录镜像
    • 4.3 配置USB直接登录
    • 4.4 配置WiFi 2.4G网络登录
    • 4.5 修改登录账号密码
    • 4.6 数据同步和弹出tf卡
  • 5. 登录
    • 5.1 登录异常处理
    • 5.2 WiFi 2.4G网络登录
    • 5.3 USB直接登录
  • 6. 参考资料
  • 7. 补充资料

这里记录下关于Raspberry Pi Zero W采用Raspbian无头镜像烧录的过程,以及一些注意事项。

1. 源由

在实际嵌入式系统应用中,大部分情况没有显示器,甚至没有交互设备(鼠标/键盘等)。因此,需要尽快安装最简系统,并处于可用状态。

2. 需求

从源由的角度,我们这里如下技术需求:

  1. 最简系统安装
  2. 可用状态运行
  3. 无交互设备支持(显示器/鼠标/键盘等)

3. 分析

树莓派是一款开源软硬件支持的嵌入式系统。其性能也推陈出新,适用于非常多的场景。为了满足上面提及的技术需求,采取如下方式完成任务:

  • 刷写最新无桌面的raspbian镜像Raspberry Pi OS Lite, 2023-02-21-raspios-bullseye
  • 支持Remote NDIS (RNDIS),通过USB直接登录系统
  • 支持无线网络,通过WiFi 2.4G无线网络登录系统

4. 步骤

4.1 删除tf卡分区内容

daniel@daniel-ThinkPad-SL410:~$ df
Filesystem     1K-blocks     Used Available Use% Mounted on
udev             1950876        0   1950876   0% /dev
tmpfs             398272     3608    394664   1% /run
/dev/sda5      479078088 99783880 354884864  22% /
tmpfs            1991352        0   1991352   0% /dev/shm
tmpfs               5120        4      5116   1% /run/lock
tmpfs            1991352        0   1991352   0% /sys/fs/cgroup
/dev/loop1        224256   224256         0 100% /snap/gnome-3-34-1804/72
/dev/loop5        224256   224256         0 100% /snap/gnome-3-34-1804/77
/dev/loop7           128      128         0 100% /snap/bare/5
/dev/loop8        354688   354688         0 100% /snap/gnome-3-38-2004/119
/dev/loop0         56960    56960         0 100% /snap/core18/2679
/dev/loop2         56960    56960         0 100% /snap/core18/2697
/dev/loop9         83328    83328         0 100% /snap/gtk-common-themes/1534
/dev/loop3         64896    64896         0 100% /snap/core20/1822
/dev/loop6         64896    64896         0 100% /snap/core20/1778
/dev/loop11        47104    47104         0 100% /snap/snap-store/599
/dev/loop4        354688   354688         0 100% /snap/gnome-3-38-2004/115
/dev/loop10        93952    93952         0 100% /snap/gtk-common-themes/1535
/dev/loop12        47104    47104         0 100% /snap/snap-store/638
/dev/loop13        51072    51072         0 100% /snap/snapd/17950
/dev/loop14        51072    51072         0 100% /snap/snapd/18357
/dev/sda1         523248        4    523244   1% /boot/efi
tmpfs             398268       88    398180   1% /run/user/1000
/dev/sdb1         261108    51018    210090  20% /media/daniel/bootfs
/dev/sdb2        7343704  1329408   5683716  19% /media/daniel/rootfs
daniel@daniel-ThinkPad-SL410:~$ sudo umount /dev/sdb1
daniel@daniel-ThinkPad-SL410:~$ sudo umount /dev/sdb2
daniel@daniel-ThinkPad-SL410:~$ sudo fdisk /dev/sdbWelcome to fdisk (util-linux 2.34).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.Command (m for help): p
Disk /dev/sdb: 7.41 GiB, 7948206080 bytes, 15523840 sectors
Disk model: Storage Device
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x9bd51a97Device     Boot  Start      End  Sectors  Size Id Type
/dev/sdb1         8192   532479   524288  256M  c W95 FAT32 (LBA)
/dev/sdb2       532480 15523839 14991360  7.2G 83 LinuxCommand (m for help): d
Partition number (1,2, default 2): 2Partition 2 has been deleted.Command (m for help): d
Selected partition 1
Partition 1 has been deleted.Command (m for help): 1
1: unknown commandCommand (m for help): wThe partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.

4.2 balena烧录镜像

关于烧录软件其实挺多的,其中Raspberry Pi Imager是官方推荐的工具,确实有些配置选项可以在烧录工具中进行配置,但是有的时候也并不好使。因此这里采用了一款UI比较友好的工具balena。

在这里插入图片描述在这里插入图片描述在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

4.3 配置USB直接登录

进入boot分区

daniel@daniel-ThinkPad-SL410:~$ df
Filesystem     1K-blocks     Used Available Use% Mounted on
udev             1950876        0   1950876   0% /dev
tmpfs             398272     3600    394672   1% /run
/dev/sda5      479078088 99785604 354883140  22% /
tmpfs            1991352    12720   1978632   1% /dev/shm
tmpfs               5120        4      5116   1% /run/lock
tmpfs            1991352        0   1991352   0% /sys/fs/cgroup
/dev/loop1        224256   224256         0 100% /snap/gnome-3-34-1804/72
/dev/loop5        224256   224256         0 100% /snap/gnome-3-34-1804/77
/dev/loop7           128      128         0 100% /snap/bare/5
/dev/loop8        354688   354688         0 100% /snap/gnome-3-38-2004/119
/dev/loop0         56960    56960         0 100% /snap/core18/2679
/dev/loop2         56960    56960         0 100% /snap/core18/2697
/dev/loop9         83328    83328         0 100% /snap/gtk-common-themes/1534
/dev/loop3         64896    64896         0 100% /snap/core20/1822
/dev/loop6         64896    64896         0 100% /snap/core20/1778
/dev/loop11        47104    47104         0 100% /snap/snap-store/599
/dev/loop4        354688   354688         0 100% /snap/gnome-3-38-2004/115
/dev/loop10        93952    93952         0 100% /snap/gtk-common-themes/1535
/dev/loop12        47104    47104         0 100% /snap/snap-store/638
/dev/loop13        51072    51072         0 100% /snap/snapd/17950
/dev/loop14        51072    51072         0 100% /snap/snapd/18357
/dev/sda1         523248        4    523244   1% /boot/efi
tmpfs             398268       88    398180   1% /run/user/1000
/dev/sdb1         261108    51018    210090  20% /media/daniel/bootfs
/dev/sdb2        1589576  1196192    294468  81% /media/daniel/rootfs
daniel@daniel-ThinkPad-SL410:~$ cd /media/daniel/bootfs

默认开启ssh远程登录

daniel@daniel-ThinkPad-SL410:/media/daniel/bootfs$ touch ssh

添加dtoverlay=dwc2config.txt文件

daniel@daniel-ThinkPad-SL410:/media/daniel/bootfs$ nano config.txt
daniel@daniel-ThinkPad-SL410:/media/daniel/bootfs$ tail config.txt
otg_mode=1[all][pi4]
# Run as fast as firmware / board allows
arm_boost=1[all]
dtoverlay=dwc2

添加modules-load=dwc2,g_etherrootwait

注意:添加内容前后均保持一个空格。

daniel@daniel-ThinkPad-SL410:/media/daniel/bootfs$ nano cmdline.txt
daniel@daniel-ThinkPad-SL410:/media/daniel/bootfs$ cat cmdline.txt
console=serial0,115200 console=tty1 root=PARTUUID=34f4435e-02 rootfstype=ext4 fsck.repair=yes rootwait modules-load=dwc2,g_ether quiet init=/usr/lib/raspberrypi-sys-mods/firstboot

4.4 配置WiFi 2.4G网络登录

daniel@daniel-ThinkPad-SL410:/media/daniel/bootfs$ nano wpa_supplicant.conf
daniel@daniel-ThinkPad-SL410:/media/daniel/bootfs$ cat wpa_supplicant.conf
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdevupdate_config=1country=CNnetwork={ssid="AutoLab"psk="68686868"}

4.5 修改登录账号密码

鉴于安全性考虑,树莓派增加了账户密码的配置过程。有交互界面的会很方便,但是作为无头烧录的情况,那么就需要在配置文件里面修改。

daniel@daniel-ThinkPad-SL410:/media/daniel/bootfs$ echo 'mypassword' | openssl passwd -6 -stdin
$6$2S.7faFHMW1PFgOo$NoxP.CYxGLT1iWskjCSWxlJ5LiNy2pD4bSY.TaUUPoPk5cJS4hOLlkrlBq0IAtoKDR0moyTVwwGy8WrcgaDFJ1
daniel@daniel-ThinkPad-SL410:/media/daniel/bootfs$ nano userconf
daniel@daniel-ThinkPad-SL410:/media/daniel/bootfs$ cat userconf
daniel:$6$2S.7faFHMW1PFgOo$NoxP.CYxGLT1iWskjCSWxlJ5LiNy2pD4bSY.TaUUPoPk5cJS4hOLlkrlBq0IAtoKDR0moyTVwwGy8WrcgaDFJ1
$6$2S.7faFHMW1PFgOo$NoxP.CYxGLT1iWskjCSWxlJ5LiNy2pD4bSY.TaUUPoPk5cJS4hOLlkrlBq0IAtoKDR0moyTVwwGy8WrcgaDFJ1

此时,系统登录账号密码就被配置为

  • 账号:daniel
  • 密码:mypassword

4.6 数据同步和弹出tf卡

daniel@daniel-ThinkPad-SL410:/media/daniel/bootfs$ sync
daniel@daniel-ThinkPad-SL410:/media/daniel/bootfs$ cd
daniel@daniel-ThinkPad-SL410:~$ df
Filesystem     1K-blocks     Used Available Use% Mounted on
udev             1950876        0   1950876   0% /dev
tmpfs             398272     3592    394680   1% /run
/dev/sda5      479078088 99785780 354882964  22% /
tmpfs            1991352    12720   1978632   1% /dev/shm
tmpfs               5120        4      5116   1% /run/lock
tmpfs            1991352        0   1991352   0% /sys/fs/cgroup
/dev/loop1        224256   224256         0 100% /snap/gnome-3-34-1804/72
/dev/loop5        224256   224256         0 100% /snap/gnome-3-34-1804/77
/dev/loop7           128      128         0 100% /snap/bare/5
/dev/loop8        354688   354688         0 100% /snap/gnome-3-38-2004/119
/dev/loop0         56960    56960         0 100% /snap/core18/2679
/dev/loop2         56960    56960         0 100% /snap/core18/2697
/dev/loop9         83328    83328         0 100% /snap/gtk-common-themes/1534
/dev/loop3         64896    64896         0 100% /snap/core20/1822
/dev/loop6         64896    64896         0 100% /snap/core20/1778
/dev/loop11        47104    47104         0 100% /snap/snap-store/599
/dev/loop4        354688   354688         0 100% /snap/gnome-3-38-2004/115
/dev/loop10        93952    93952         0 100% /snap/gtk-common-themes/1535
/dev/loop12        47104    47104         0 100% /snap/snap-store/638
/dev/loop13        51072    51072         0 100% /snap/snapd/17950
/dev/loop14        51072    51072         0 100% /snap/snapd/18357
/dev/sda1         523248        4    523244   1% /boot/efi
tmpfs             398268       92    398176   1% /run/user/1000
/dev/sdb1         261108    51022    210086  20% /media/daniel/bootfs
/dev/sdb2        1589576  1196192    294468  81% /media/daniel/rootfs
daniel@daniel-ThinkPad-SL410:~$ sudo umount /dev/sdb1
daniel@daniel-ThinkPad-SL410:~$ sudo umount /dev/sdb2

5. 登录

准备如下动作:

  • 确保AutoLab配置的路由器处于正常工作状态
  • 确保ubuntu laptopn开机,系统正常
  • 将tf卡插入Raspberry Pi Zero W
  • 采用usb直接与Raspberry Pi Zero W连接供电

等待一段时间(2~3分钟),在【Settings】-【Network】下出现【USB Ethernet】连接项。
在这里插入图片描述
如果ssid和密码配置正确的话,路由器管理页面会看到树莓派已经wifi 2.4G动态获得了ip地址。
在这里插入图片描述

5.1 登录异常处理

如果之前登陆过,可能会有IDENTIFICATION 问题,会拒绝登录。

daniel@daniel-ThinkPad-SL410:~$ ssh daniel@raspberrypi.local
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@       WARNING: POSSIBLE DNS SPOOFING DETECTED!          @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
The ECDSA host key for raspberrypi.local has changed,
and the key for the corresponding IP address 192.168.68.245
has a different value. This could either mean that
DNS SPOOFING is happening or the IP address for the host
and its host key have changed at the same time.
Offending key for IP in /home/daniel/.ssh/known_hosts:1remove with:ssh-keygen -f "/home/daniel/.ssh/known_hosts" -R "192.168.68.245"
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@    WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!     @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!
Someone could be eavesdropping on you right now (man-in-the-middle attack)!
It is also possible that a host key has just been changed.
The fingerprint for the ECDSA key sent by the remote host is
SHA256:eCOHphYnkQDz3KuYx1MpK5Y0z4jjFyBeoTRY2dZ9Ii0.
Please contact your system administrator.
Add correct host key in /home/daniel/.ssh/known_hosts to get rid of this message.
Offending ECDSA key in /home/daniel/.ssh/known_hosts:3remove with:ssh-keygen -f "/home/daniel/.ssh/known_hosts" -R "raspberrypi.local"
ECDSA host key for raspberrypi.local has changed and you have requested strict checking.
Host key verification failed.

强制更新下ssh-keygen

daniel@daniel-ThinkPad-SL410:~$ ssh-keygen -f "/home/daniel/.ssh/known_hosts" -R "raspberrypi.local"
# Host raspberrypi.local found: line 3
/home/daniel/.ssh/known_hosts updated.
Original contents retained as /home/daniel/.ssh/known_hosts.old

5.2 WiFi 2.4G网络登录

这个是优选的方式,此时登录的IP地址是’192.168.68.245’

daniel@daniel-ThinkPad-SL410:~$ ssh daniel@raspberrypi.local
The authenticity of host 'raspberrypi.local (192.168.68.245)' can't be established.
ECDSA key fingerprint is SHA256:eCOHphYnkQDz3KuYx1MpK5Y0z4jjFyBeoTRY2dZ9Ii0.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added 'raspberrypi.local' (ECDSA) to the list of known hosts.
Warning: the ECDSA host key for 'raspberrypi.local' differs from the key for the IP address '192.168.68.245'
Offending key for IP in /home/daniel/.ssh/known_hosts:1
Are you sure you want to continue connecting (yes/no)? yes
daniel@raspberrypi.local's password:
Linux raspberrypi 5.15.84+ #1613 Thu Jan 5 11:58:09 GMT 2023 armv6lThe programs included with the Debian GNU/Linux system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
permitted by applicable law.

5.3 USB直接登录

关闭路由器,并对【USB Ethernet】进行配置

在这里插入图片描述
在这里插入图片描述

此时,可以看到登录的IP地址是’169.254.78.153’

daniel@daniel-ThinkPad-SL410:~$ ssh daniel@raspberrypi.local
Warning: Permanently added the ECDSA host key for IP address '169.254.78.153' to the list of known hosts.
daniel@raspberrypi.local's password: 
Linux raspberrypi 5.15.84+ #1613 Thu Jan 5 11:58:09 GMT 2023 armv6lThe programs included with the Debian GNU/Linux system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
permitted by applicable law.

6. 参考资料

【1】Introduction to Remote NDIS (RNDIS)
【2】ssh-into-pi-zero-over-usb
【3】Setting up a Raspberry Pi - Headless
【4】ISO 3166-1 (Codes for the representation of names of countries and their subdivisions – Part 1: Country codes)
【5】An update to Raspberry Pi OS Bullseye

7. 补充资料

Advanced IP Scanner,可以扫描网络设备发现树莓派。

在这里插入图片描述

通常我们这里采用 ssh pi@raspberrypi.local方式,IP地址也没有这么重要。

相关内容

热门资讯

安卓系统的总体框架,架构与核心... 你有没有想过,你的手机里那个神奇的安卓系统,它到底是怎么运作的呢?今天,就让我带你一探究竟,揭开安卓...
谁的安卓系统好,谁家的安卓系统... 说到安卓系统,这可是个热门话题呢!你有没有想过,这么多安卓手机品牌,哪个的操作系统最让你心动?今天,...
安卓系统信付通,安全无忧的移动... 你知道吗?在安卓手机的世界里,有一个超级好用的支付工具,它就是信付通。今天,就让我带你来全方位了解一...
小米官方系统安卓包,深度解析与... 亲爱的数码爱好者们,你是否曾为手机系统而烦恼?市面上那么多手机品牌,各种操作系统让人眼花缭乱。今天,...
自制安卓手机双系统,自制安卓手... 你有没有想过,自己的手机可以同时运行两个操作系统呢?没错,就是那种安卓手机双系统!听起来是不是很酷?...
小米安卓系统怎么设置,科技前沿... 小米手机的用户们,是不是觉得安卓系统有点复杂,设置起来有点头疼呢?别担心,今天就来手把手教你如何轻松...
点歌系统支持安卓系统么,安卓用... 你有没有想过,在手机上点歌听歌,是不是也能像在KTV里那样随心所欲呢?现在,就让我来告诉你一个超级酷...
原版安卓系统刷机,解锁无限可能 你有没有想过,你的安卓手机其实可以焕然一新?没错,就是那种原汁原味的安卓系统,让你的手机重新找回当初...
欧尚改装安卓系统,打造智能驾驶... 你有没有想过,你的欧尚汽车其实也可以变身成为智能座驾呢?没错,就是那个你每天上下班的伙伴——欧尚,现...
安卓系统最新事件,揭秘最新重大... 你知道吗?最近安卓系统可是发生了一件超级大事件,简直让人兴奋得心跳加速!这不,我就迫不及待地来和你分...
早期电话手表安卓系统,安卓系统... 你有没有想过,小时候那些看似简单的玩具,现在竟然也能玩出花来?比如,早期的电话手表,那时候的功能可真...
安卓老系统手机游戏,安卓老系统... 你有没有发现,那些安卓老系统手机,虽然看起来有点古老,但它们在游戏界可是有着自己独特的魅力呢!想象那...
安卓系统重启还是开关,重启与开... 手机突然卡壳了,是不是又该给安卓系统来个重启大法了?别急,今天就来聊聊这个让人又爱又恨的“安卓系统重...
安卓系统刷入iso,轻松实现个... 你有没有想过,你的安卓手机其实可以像变形金刚一样,换上全新的“皮肤”?没错,就是刷入ISO系统!这可...
安卓机系统无法关机,探究原因与... 最近我的安卓手机怎么啦?总是关机不成功,真是让人头疼啊!这可怎么办呢?别急,让我来帮你分析找出解决这...
安卓什么系统广告最多,揭秘最新... 你有没有发现,每次打开安卓手机,广告就像无处不在的小精灵,跳来跳去,让人眼花缭乱?今天,就让我带你一...
禁止中国使用安卓系统,“安卓系... 你知道吗?最近互联网上掀起了一股热议,那就是关于中国是否应该禁止使用安卓系统的话题。这可不是闹着玩的...
如何分辨ios系统和安卓系统,... 你有没有想过,你的手机里装的是iOS系统还是安卓系统呢?这两种系统各有千秋,但分辨它们其实并不难。今...
如何查询安卓系统版本,安卓系统... 你有没有想过,你的安卓手机里隐藏着一个小秘密——那就是它的系统版本!知道这个秘密,不仅能让你更好地了...
lg电视系统和安卓系统比较,性... 你有没有发现,现在家里的电视已经不再是那个傻乎乎的“大盒子”了?它变得聪明起来,能和你互动,能上网,...