自定义树莓派系统镜像

# 获取最新的树莓派系统镜像
wget https://downloads.raspberrypi.org/raspbian_lite_latest

# 解压系统
unzip raspbian_lite_latest

# 查看系统分区信息
fdisk -lu 2020-02-05-raspbian-buster-lite.img

# 挂载第一个分区 Fat32 格式
mount -o loop,offset=4194304 2020-02-05-raspbian-buster-lite.img /mnt/

# 开启 SSH 
touch /mnt/ssh

# 卸载第一个分区
umount /mnt/

# 挂载第二个分区 Ext4 格式
mount -o loop,offset=272629760 2020-02-05-raspbian-buster-lite.img /mnt

# 开启 root 用户登录
sed -i "s/#PermitRootLogin prohibit-password/PermitRootLogin yes/g" /mnt/etc/ssh/sshd_config

# SSH 服务优化
sed -i "s/#UseDNS no/UseDNS no/g" /mnt/etc/ssh/sshd_config

# 清空登录提示信息
echo "" > /mnt/etc/motd

# 更换阿里云镜像源
echo "deb https://mirrors.aliyun.com/raspbian/raspbian/ buster main non-free contrib
deb-src https://mirrors.aliyun.com/raspbian/raspbian/ buster main non-free contrib" > /mnt/etc/apt/sources.list
sed -i "s/deb http/#deb http/g" /mnt/etc/apt/sources.list.d/raspi.list

# Wifi 自动连接
echo "country=CN

network={
  ssid="WIFI"
  psk="12345678"
}
" >> /mnt/etc/wpa_supplicant/wpa_supplicant.conf

# 卸载第二个分区
umount /mnt/

# 压缩镜像
gzip 2020-02-05-raspbian-buster-lite.img