温馨提示×

ubuntu如何定制linux系统

小樊
43
2026-01-03 10:17:08
栏目: 智能运维

Ubuntu 定制 Linux 系统的主流路径与步骤


一、目标与路径选择

  • 面向现有设备的系统级定制:在已装好的 Ubuntu 上调整内核启动参数、默认服务、登录策略、桌面环境、预装软件与自动化脚本,适合快速落地与维护。
  • 面向交付的镜像级定制:从官方 ISO 解包、修改 casper/filesystem.squashfs、在 chroot 中预装软件与配置、再重新打包生成可启动镜像,适合批量部署与统一交付。
  • 面向极简与学习的“从零”构建:编译 Linux 内核 + BusyBox 构建最小根文件系统,用 QEMU 启动验证,适合理解启动流程与嵌入式入门。
  • 面向发行版形态的自动化构建:使用 live-buildCubic 搭建构建环境,管理配置与钩子脚本,生成可启动的 Live 镜像,适合长期维护与版本化定制。

二、镜像级定制流程(ISO 解包与重打包)

  • 准备与挂载
    • 安装工具:sudo apt-get install -y squashfs-tools xorriso
    • 挂载原镜像并拷贝:mount ubuntu-22.04-desktop-amd64.iso /mnt/iso && cp -rp /mnt/iso/. livecd && umount /mnt/iso
  • 解包根文件系统
    • unsquashfs livecd/casper/filesystem.squashfs,得到 squashfs-root
  • 进入 chroot 并准备网络/源
    • sudo chroot squashfs-root
    • 挂载虚拟文件系统:mount -t proc proc /proc; mount -t sysfs sys /sys; mount -t devpts devpts /dev/pts
    • 拷贝 DNS:cp /etc/resolv.conf /squashfs-root/etc/resolv.conf
    • 更换软件源(示例为 jammy):/etc/apt/sources.list 中加入
      • deb https://mirrors.aliyun.com/ubuntu/ jammy main restricted universe multiverse
      • deb https://mirrors.aliyun.com/ubuntu/ jammy-updates main restricted universe multiverse
      • deb https://mirrors.aliyun.com/ubuntu/ jammy-security main restricted universe multiverse
  • 在 chroot 内定制
    • 预装软件:apt-get update && apt-get install -y openssh-server vim docker.io docker-compose
    • 系统配置:时区、语言、用户、sudo、开机服务、内核参数等
  • 生成新镜像
    • 退出 chroot 并卸载:exit && umount squashfs-root/{proc,sys,dev/pts}
    • 重新生成 manifest:
      • chmod +w livecd/casper/filesystem.manifest
      • chroot squashfs-root dpkg-query -W --showformat='${Package} ${Version}\n' > livecd/casper/filesystem.manifest
      • cp livecd/casper/filesystem.manifest livecd/casper/filesystem.manifest-desktop
      • sed -i '/ubiquity/d' livecd/casper/filesystem.manifest-desktop
      • sed -i '/casper/d' livecd/casper/filesystem.manifest-desktop
    • 重新打包 squashfs:rm livecd/casper/filesystem.squashfs && mksquashfs squashfs-root livecd/casper/filesystem.squashfs
    • 生成 ISO(BIOS + UEFI 双启动):
      • xorriso -as mkisofs -iso-level 3 -o custom-ubuntu-22.04.iso -full-iso9660-filenames -volid "Custom Ubuntu 22.04" -eltorito-alt-boot -e EFI/boot/bootx64.efi -no-emul-boot -isohybrid-gpt-basdat livecd/
      • xorriso -as mkisofs -iso-level 3 -o custom-ubuntu-22.04.iso -full-iso9660-filenames -volid "Custom Ubuntu 22.04" -boot-info-table --grub2-boot-info -eltorito-alt-boot -b 'boot/grub/i386-pc/eltorito.img' -no-emul-boot -isohybrid-gpt-basdat livecd/
  • 测试:用 VMware/VirtualBox 新建虚拟机从 ISO 启动,验证预装软件与配置是否生效。

三、自动化构建与高级定制

  • 使用 Cubic 图形化定制
    • 安装:sudo add-apt-repository ppa:cubic-wizard/release && sudo apt install --no-install-recommends cubic
    • 在 Cubic 中导入官方 ISO,进入 chroot 后可更换软件源、预装软件、设置 Plymouth 开机动画与壁纸,完成后一键生成新 ISO,适合不写脚本的快速定制。
  • 使用 live-build 系统化构建
    • 安装:sudo apt-get install -y git live-build
    • 初始化与配置:lb config 生成配置骨架,按需编辑 config/*(包列表、钩子脚本、引导配置等),最后 lb build 生成 Live 镜像,适合版本化管理与持续集成。
  • 常见高级定制要点
    • 自定义壁纸与登录界面:替换壁纸后,写入 GSettings 并编译 schemas,例如
      • sudo cp wallpaper.png /usr/share/backgrounds/
      • 新建 /usr/share/glib-2.0/schemas/99_custom-wallpaper.gschema.override
        • [org.gnome.desktop.background] picture-uri='file:///usr/share/backgrounds/wallpaper.png' picture-options='zoom'
      • sudo glib-compile-schemas /usr/share/glib-2.0/schemas/
    • 自定义 Plymouth 开机动画:准备背景与 Logo,创建主题目录,设置默认主题并更新 initramfs
      • update-alternatives --install /usr/share/plymouth/themes/default.plymouth default.plymouth /usr/share/plymouth/themes/custom-boot/custom-boot.plymouth 100
      • update-alternatives --set default.plymouth /usr/share/plymouth/themes/custom-boot/custom-boot.plymouth
      • update-initramfs -u
    • 预置用户与默认配置:在 chroot 中创建默认用户、设置空密码或预设口令、生成 SSH 密钥、放置 dotfiles 与自动配置脚本,便于交付即用的体验。

四、从零构建最小 Linux 系统(学习向)

  • 编译内核
    • 安装依赖:sudo apt-get install -y fakeroot build-essential ncurses-dev xz-utils libssl-dev bc flex libelf-dev bison
    • 配置与编译(示例 x86_64):
      • make x86_64_defconfig
      • make menuconfig(启用 Initial RAM filesystem 等)
      • make -j$(nproc)
      • 产物:arch/x86/boot/bzImage
  • 编译 BusyBox
    • make defconfig && make menuconfig(Settings → Build static binary)
    • make -j$(nproc) && make install(生成 _install 目录)
  • 构建 initramfs
    • _install 中创建 init
      • #!/bin/sh
      • mount -t proc proc /proc; mount -t sysfs sys /sys; mount -t devtmpfs devtmpfs /dev
      • echo /sbin/mdev > /proc/sys/kernel/hotplug
      • mdev -s
      • exec /bin/sh
    • 打包:find . | cpio -o -H newc | gzip -9 > initramfs.cpio.gz
  • QEMU 启动验证
    • qemu-system-x86_64 -kernel arch/x86/boot/bzImage -initrd initramfs.cpio.gz -nographic -append "console=ttyS0"
      该路径帮助理解内核、根文件系统与用户态的最小组合,适合教学与嵌入式原型验证。

五、实用建议与常见问题

  • 构建环境隔离:在 VMware/VirtualBox 或容器中进行定制,避免污染宿主机;所有改动先在测试环境验证再发布。
  • 镜像体积与启动速度:优先使用官方 Minimal ISO 或最小化安装作为基线;按需增减软件包,清理缓存 apt clean,减少 filesystem.squashfs 体积。
  • 网络与源:chroot 前务必拷贝 /etc/resolv.conf,并切换到就近镜像源(如 aliyun/ustc)以加速安装。
  • UEFI 与 BIOS 兼容:发布镜像时同时提供 BIOS(i386-pc)+ UEFI(EFI/boot/bootx64.efi) 启动支持,提升通用性。
  • 唯一性与合规性:清理或重新生成 /etc/machine-id,避免克隆后冲突;预装软件注意许可证与版权合规。
  • 自动化安装:如需无人值守安装,可结合 Cubic/live-build 预置应答文件或安装器配置,实现批量部署。

0