由于我可能要批量安装一批边缘盒子,所以,我希望在自动化安装系统后,尽量通过离线.deb包的形式安装桌面环境(ubuntu-desktop-minimal)和firefox。
假设有两台机器,第一台叫做“联网机”,第二台叫做“离线机”。我在联网机上配置了国内的apt源,并经过了漫长等待(是的,哪怕国内源,下载也要很长时间),成功安装了ubuntu-desktop-minimal和firefox。经过一番探索和验证,我得出了以下方案。
总体思路
在“联网机”上下载好所有.deb包(包含完整依赖),打包后通过 HTTP 分享,另一台离线机器使用脚本下载、解压并安装这些.deb包。
“联网机”准备离线包
安装所需软件(会自动缓存 .deb)
sudo apt update
sudo apt install ubuntu-desktop-minimal firefox
检查.deb包是否缓存
ls -l /var/cache/apt/archives/
把这些.deb包复制并打包
mkdir -p ~/offline-debs
cp /var/cache/apt/archives/*.deb ~/offline-debs/
cd ~
tar -czf offline-debs.tar.gz offline-debs/
“联网机”准备安装脚本
编写安装脚本 install.sh
#!/bin/bash
set -e
# Step 1: 下载并解压
URL="http://192.168.31.47:8080/offline-debs.tar.gz"
TAR_FILE="/tmp/offline-debs.tar.gz"
DEST_DIR="/tmp/offline-debs"
echo "Downloading package archive..."
wget -O "$TAR_FILE" "$URL"
echo "Extracting to $DEST_DIR..."
rm -rf "$DEST_DIR"
mkdir -p "$DEST_DIR"
tar -xzf "$TAR_FILE" -C "$DEST_DIR"
DEB_DIR=$(find "$DEST_DIR" -type d -name "offline-debs" | head -n 1)
if [[ -z "$DEB_DIR" ]]; then
echo "Error: .deb directory not found."
exit 1
fi
# Step 2: 写入临时 TUNA 源(不影响主配置)
echo "Adding temporary TUNA source..."
cat <<EOF | sudo tee /etc/apt/sources.list.d/temp-tuna.sources > /dev/null
Types: deb
URIs: https://mirrors.tuna.tsinghua.edu.cn/ubuntu
Suites: noble noble-updates noble-backports
Components: main restricted universe multiverse
Signed-By: /usr/share/keyrings/ubuntu-archive-keyring.gpg
EOF
# Step 3: 更新 apt 缓存
echo "Running apt update..."
sudo apt update
# Step 4: 安装
echo "Installing packages from local .deb and remote source if needed..."
cd /tmp/offline-debs/offline-debs/
sudo apt install -y ./*.deb
sudo apt install -y ubuntu-desktop-minimal
# Step 5: 清理临时源
echo "Cleaning up temporary source..."
sudo rm /etc/apt/sources.list.d/temp-tuna.sources
sudo apt update
# Step 6: 设置桌面为默认
echo "Setting Graphicall desktop as default"
systemctl set-default graphical.target
echo "Installation complete."
准备HTTP文件服务器
将以上的offline-debs.tar.gz文件包和install.sh脚本放在合适的目录下,并在此处运行
python3 -m http.server 8080
在“离线机”上获取脚本并安装
wget http://192.168.31.47:8080/install.sh
bash install.sh
等待脚本运行完成后,如果希望开机默认进入桌面环境,需要执行
systemctl set-default graphical.target