温馨提示×

如何精简ubuntu fromscratch

小樊
41
2026-09-28 15:08:08
栏目: 智能运维

“Ubuntu From Scratch(UFS)”并不是一个官方标准项目(官方有 Linux From Scratch (LFS),也有社区向的 Ubuntu 定制构建思路)。如果你指的是从零构建最小可用的 Ubuntu 系统(或基于 Debian/Ubuntu 的 rootfs),可以从以下几个层面来“精简”。


一、构建阶段的精简

1. 使用最小基础系统

不要使用 ubuntu-desktop 或 minimal?task=ubuntu-server。

推荐方式:

  • debootstrap(最常用)
debootstrap --variant=minbase focal ./rootfs http://archive.ubuntu.com/ubuntu

minbase 只装最基础包。

  • 或 mmdebstrap(更快、可控)
mmdebstrap --variant=essential focal rootfs

2. 明确“不需要”的内容

在构建时直接排除:

  • *-doc
  • man-db
  • locales(如不需要多语言)
  • apt(若系统只读或固件)
  • systemd → 可用 runit / openrc

示例:

debootstrap --exclude=apt,man-db,locales focal rootfs

二、包层面的精简

1. 移除文档和语言包

rm -rf /usr/share/doc/*
rm -rf /usr/share/man/*
rm -rf /usr/share/locale/*

2. 清理 apt 缓存

apt-get clean
rm -rf /var/lib/apt/lists/*

3. 用 apt-rdepends 检查冗余

apt-rdepends package | sort -u

只保留运行必需依赖。


三、系统组件的精简

1. Init 系统

  • 不用 systemd → 用 systemd --minimal 或 openrc
  • 嵌入式可直接用 init=/bin/sh + 脚本

2. 内核

  • 自己编译内核
  • 关闭:
    • 调试符号
    • 不用的驱动
    • 多余文件系统
make localmodconfig

3. 服务

禁用所有非必需服务:

systemctl disable bluetooth ssh cron

四、文件系统的精简

1. 使用紧凑文件系统

  • squashfs(只读)
  • ext4 + discard
  • overlayfs

2. 剥离二进制

strip --strip-unneeded /usr/bin/*

五、一个“极简 Ubuntu”示例体积

方案 体积
minbase ~80MB
+ kernel ~120MB
再 strip + squashfs < 50MB

六、如果你愿意,我可以:

  • 给你一个 完整 UFS 构建脚本
  • 对比 Ubuntu vs Alpine vs LFS
  • 做一个 Docker / 嵌入式 Ubuntu

你现在的“fromscratch”是:学习用 / 容器 / 嵌入式 / 服务器?

0 踩