如何在Ubuntu镜像中自定义系统设置
导读:在Ubuntu镜像中自定义系统设置的实用方案 一、方案总览与选择 Cubic 图形化工作流:在图形界面中导入原版 ISO,自动解压并在 chroot 环境中进行软件安装、系统配置与生成新 ISO,适合 Desktop/Server 的个性...
在Ubuntu镜像中自定义系统设置的实用方案
一、方案总览与选择
- Cubic 图形化工作流:在图形界面中导入原版 ISO,自动解压并在 chroot 环境中进行软件安装、系统配置与生成新 ISO,适合 Desktop/Server 的个性化与批量部署。
- Systemback 备份转镜像:将现有系统(含用户数据与配置)打包为 Live 系统镜像,可转换为 ISO 在其他设备上快速恢复或部署,适合“复刻当前系统”。
- 命令行全流程:手动挂载 ISO、解压 filesystem.squashfs、进入 chroot 定制、重打包并生成 ISO,可控性最强,适合高级用户与自动化脚本。
二、方法一 Cubic 图形化定制(推荐)
- 安装 Cubic
- 在 Ubuntu 22.04+ 终端执行:
- sudo apt-add-repository ppa:cubic-wizard/release
- sudo apt update
- sudo apt install --no-install-recommends cubic -y
- 在 Ubuntu 22.04+ 终端执行:
- 创建项目与导入 ISO
- 启动 Cubic,选择空工作目录,点击 Next 并选择官方 Ubuntu ISO(如 ubuntu-22.04-desktop-amd64.iso),自动解压文件系统。
- chroot 内自定义
- 在弹出的终端(root)中进行定制:
- 网络与源:
- ln -sr /run/systemd/resolve/resolv.conf /run/systemd/resolve/stub-resolv.conf
- apt update & & apt install -y software-properties-common vim htop curl wget
- 按需替换 /etc/apt/sources.list 为国内镜像源
- 预装软件与配置:
- apt install -y build-essential git python3-pip
- 配置时区与语言:timedatectl set-timezone Asia/Shanghai;localectl set-locale LANG=zh_CN.UTF-8
- 写入自定义脚本到 /etc/profile.d/ 或创建 systemd 服务,实现首次启动自动配置
- 网络与源:
- 在弹出的终端(root)中进行定制:
- 生成 ISO
- 回到 Cubic 向导,点击 Next 生成新 ISO,完成后在输出目录查看并测试启动。
三、方法二 Systemback 备份转镜像(复刻当前系统)
- 安装 Systemback
- sudo add-apt-repository ppa:nemh/systemback
- sudo apt-get update & & sudo apt-get install -y systemback unionfs-fuse
- 创建 Live 镜像
- 打开 Systemback,点击 Live system create,勾选 include the user data files(如需包含主目录配置),设置 Working Directory(确保充足空间),点击 Create New 开始。
- 转换为 ISO
- 在备份列表中选择刚创建的备份,点击 convert to ISO,完成后在工作目录获取 ISO 文件,可用于启动或安装。
- 故障排查
- 若出现 “The restore point creation is aborted! …”,执行 systemback-sustart 查看详细错误;必要时用 DBGLEV=3 systemback-sustart 获取调试信息,常见为权限或文件占用问题。
四、方法三 命令行全流程(可控性最强)
- 准备与解包
- 挂载原版 ISO 并复制内容:
- mkdir -p /mnt/iso /opt/livecd
- mount -o loop ubuntu.iso /mnt/iso
- cp -rp /mnt/iso/. /opt/livecd/
- umount /mnt/iso
- 解压 squashfs:
- cd /opt/livecd/casper
- sudo unsquashfs -d squashfs-root filesystem.squashfs
- 挂载原版 ISO 并复制内容:
- chroot 定制
- 挂载虚拟文件系统并进入环境:
- sudo chroot squashfs-root
- mount -t proc none /proc
- mount -t sysfs none /sys
- mount -t devpts none /dev/pts
- 网络与源(解决 DNS 与软件源):
- ln -sr /run/systemd/resolve/resolv.conf /run/systemd/resolve/stub-resolv.conf
- apt update & & apt install -y software-properties-common vim htop curl wget
- 替换 /etc/apt/sources.list 为所需镜像源
- 预装软件与系统设置:
- apt install -y build-essential git python3-pip net-tools
- timedatectl set-timezone Asia/Shanghai
- localectl set-locale LANG=zh_CN.UTF-8
- 可写入 /etc/rc.local 或创建 systemd 服务,实现首次启动脚本
- 清理与退出
- apt clean & & rm -rf /var/lib/apt/lists/*
- umount /proc /sys /dev/pts
- exit
- 挂载虚拟文件系统并进入环境:
- 重打包与生成 ISO
- 重新生成 squashfs:
- rm filesystem.squashfs
- mksquashfs squashfs-root filesystem.squashfs
- rm -r squashfs-root
- 更新校验(ISO9660 传统校验,部分新环境可省略或改用 xorriso/mkisofs 参数):
- rm md5sum.txt
- find -type f -print0 | xargs -0 md5sum | grep -v isolinux/boot.cat | tee md5sum.txt
- 生成 ISO(两种常见方式,二选一):
- genisoimage -joliet-long -V “ubuntu-custom” -o …/ubuntu-custom.iso
-b isolinux/isolinux.bin -c isolinux/boot.cat -no-emul-boot -boot-load-size 4
-boot-info-table -R -J -v -cache-inodes -T -e isolinux/isolinux.bin -no-emul-boot /opt/livecd - 或使用 xorriso/mkisofs(适配 UEFI/ISOLINUX 的组合参数,按发行版与工具版本调整)。
- genisoimage -joliet-long -V “ubuntu-custom” -o …/ubuntu-custom.iso
- 重新生成 squashfs:
五、自动化安装与预设(无人值守安装)
- 原理与文件位置
- 在 preseed/ 目录放置预设文件(如 firewall.seed),并在引导配置中通过内核参数 preseed/file=/cdrom/preseed/firewall.seed 指定,实现安装过程自动应答。
- 引导配置示例
- ISOLINUX(/isolinux/isolinux.cfg):
- LABEL firewall
- menu label ^Firewall installation
- kernel /install/vmlinuz
- append preseed/file=/cdrom/preseed/firewall.seed debian-installer/locale=en_NZ console-setup/layoutcode=us initrd=/install/initrd.gz ramdisk_size=16384 root=/dev/ram rw quiet –
- LABEL firewall
- GRUB(/boot/grub/grub.cfg):
- menuentry “Install Ubuntu Server firewall” {
- set gfxpayload=keep
- linux /install/vmlinuz file=/cdrom/preseed/firewall.seed debian-installer/locale=en_US console-setup/layoutcode=us quiet —
- initrd /install/initrd.gz
- }
- menuentry “Install Ubuntu Server firewall” {
- ISOLINUX(/isolinux/isolinux.cfg):
- 预设文件片段示例(/preseed/firewall.seed)
- d-i partman-auto/init_automatically_partition string some_device_lvm
- d-i partman-auto/init_automatically_partition seen false
- d-i tasksel/force-tasks string server
- d-i pkgsel/language-pack-patterns string
- d-i pkgsel/install-language-support boolean false
- d-i pkgsel/install-recommends boolean false
- d-i pkgsel/upgrade select none
- d-i pkgsel/locale string en_US
- d-i keyboard-configuration/xkb-keymap select us
- d-i netcfg/get_hostname string ubuntu-custom
- d-i netcfg/get_domain string local
- d-i mirror/country string CN
- d-i mirror/http/hostname string mirrors.aliyun.com
- d-i mirror/http/directory string /ubuntu
- d-i mirror/http/proxy string
- d-i clock-setup/utc boolean true
- d-i time/zone string Asia/Shanghai
- d-i user-setup/allow-password-weak boolean true
- d-i passwd/user-fullname string Ubuntu User
- d-i passwd/username string ubuntu
- d-i passwd/user-password password ubuntu
- d-i passwd/user-password-again password ubuntu
- d-i user-setup/encrypt-home boolean false
- d-i finish-install/reboot_in_progress note
六、测试与注意事项
- 测试流程
- 使用 QEMU/KVM、VirtualBox 或实体机以 UEFI/BIOS 两种模式启动新 ISO,验证:网络、预装软件、系统语言/时区、首次启动脚本、无人值守安装是否按预设执行。
- 常见问题与要点
- 网络与源:chroot 内常需手动配置 DNS 与 sources.list,否则 apt 无法更新。
- 权限与占用:Systemback 报错时,优先检查目标目录权限与占用,使用 systemback-sustart 获取详细日志定位文件。
- 校验与兼容性:部分新发行版可能默认使用 xorriso/mkisofs 与 UEFI 引导参数,必要时调整 ISO 生成命令与引导配置;旧版 ISO 的 md5sum.txt 校验可按需保留或移除。
声明:本文内容由网友自发贡献,本站不承担相应法律责任。对本内容有异议或投诉,请联系2913721942#qq.com核实处理,我们将尽快回复您,谢谢合作!
若转载请注明出处: 如何在Ubuntu镜像中自定义系统设置
本文地址: https://pptw.com/jishu/749134.html
