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地址也没有这么重要。

相关内容

热门资讯

苹果系统安卓爱思助手,系统兼容... 你有没有发现,手机的世界里,苹果系统和安卓系统就像是一对欢喜冤家,总是各有各的粉丝,各有各的拥趸。而...
安卓系统占用很大内存,揭秘内存... 手机里的安卓系统是不是让你感觉内存不够用,就像你的房间堆满了杂物,总是找不到地方放新东西?别急,今天...
安卓系统p30,安卓系统下的摄... 你有没有发现,最近安卓系统P30在手机圈里可是火得一塌糊涂呢!这不,我就来给你好好扒一扒这款手机的那...
siri被安卓系统进入了,智能... 你知道吗?最近科技圈可是炸开了锅,因为一个大家伙——Siri,竟然悄悄地溜进了安卓系统!这可不是什么...
最强挂机系统和安卓区别,揭秘安... 亲爱的读者,你是否曾在游戏中遇到过这样的困扰:一边想要享受游戏带来的乐趣,一边又不想放弃手中的零食或...
安卓系统为什么设系统盘,保障稳... 你有没有想过,为什么安卓系统里会有一个叫做“系统盘”的东西呢?这可不是随便设置的,背后可是有大学问的...
王者怎么加安卓系统的,轻松提升... 你有没有想过,你的手机里那款超酷的王者荣耀,怎么才能让它更好地在你的安卓系统上运行呢?别急,今天就来...
安卓手机系统怎么开热点,共享网... 你有没有想过,当你身处一个没有Wi-Fi信号的地方,而你的安卓手机里却存满了精彩视频和游戏时,是不是...
安卓系统11的平板电脑,性能升... 你有没有发现,最近平板电脑市场又热闹起来了?没错,安卓系统11的新一代平板电脑正在悄悄地走进我们的生...
安卓手机系统创始人,安卓手机系... 你有没有想过,那些陪伴我们每天生活的安卓手机,它们的灵魂是谁赋予的呢?没错,就是那位神秘而又传奇的安...
安卓11系统速度提升,体验再升... 你知道吗?最近安卓系统又升级啦!这次可是直接跳到了安卓11,听说速度提升了不少呢!是不是很心动?那就...
安卓5.1原生系统设置apk,... 你有没有想过,你的安卓手机里那些看似普通的设置,其实隐藏着不少小秘密呢?今天,就让我带你一探究竟,揭...
手机安卓系统玩音游,畅享指尖音... 你有没有发现,现在手机上的游戏种类越来越丰富,尤其是音游,简直让人爱不释手!今天,就让我来给你详细介...
安卓系统与win10,系统融合... 你有没有想过,为什么你的手机里装的是安卓系统,而电脑上却是Windows 10呢?这两种操作系统,就...
苹果系统王者安卓系统可以登吗,... 你有没有想过,为什么苹果系统的手机那么受欢迎,而安卓系统的手机却也能在市场上占有一席之地呢?今天,咱...
安卓系统怎么重制系统还原,安卓... 手机用久了是不是感觉卡得要命,想给它来个大变身?别急,今天就来教你怎么给安卓手机重置系统,让它焕然一...
安卓9系统怎样应用分身,轻松实... 你有没有发现,手机里的APP越来越多,有时候一个APP里还要处理好多任务,分身功能简直就是救星啊!今...
获取安卓系统的ip地址,轻松获... 你有没有想过,你的安卓手机里隐藏着一个神秘的IP地址?没错,就是那个能让你在网络世界里找到自己的小秘...
LG彩电安卓系统升级,畅享智能... 你家的LG彩电是不是最近有点儿“闹别扭”,屏幕上时不时地跳出个升级提示?别急,今天就来给你详细说说这...
阴阳师安卓苹果系统,安卓与苹果... 亲爱的玩家们,你是否曾在深夜里,手握手机,沉浸在阴阳师的神秘世界?今天,就让我带你一起探索这款风靡全...