自定义树莓派系统镜像

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

# 解压系统
unzip raspbian_lite_latest

# 查看系统分区信息
fdisk -lu 2021-01-11-raspios-buster-armhf-lite.img

# 挂载第一个分区 Fat32 格式
mount -o loop,offset=4194304 2021-01-11-raspios-buster-armhf-lite.img /mnt/

# 开启 SSH 
touch /mnt/ssh

# Wifi 自动连接
echo "country=CN
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1

network={
    ssid="WIFI"
    psk="12345678"
    priority=1
    key_mgmt=WPA-PSK
}
" >> /mnt/wpa_supplicant.conf

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

# 挂载第二个分区 Ext4 格式
mount -o loop,offset=272629760 2021-01-11-raspios-buster-armhf-lite.img /mnt

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

# root 密码
openssl passwd -1 123456
sed -i "s/root:*:/root:$1$WnVLEqxw$RtbKjeiasCwcJONnUjAby0:/g" /mnt/etc/shadow

# 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

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

发布日期:
分类:Linux