温馨提示×

ubuntu fromscratch有哪些坑

小樊
56
2026-09-16 15:49:16
栏目: 智能运维

“Ubuntu From Scratch”(或者更广义的“Linux From Scratch / 从零构建 Ubuntu”)通常不是官方标准术语,一般指:

  • 使用 debootstrap / mmdebootstrap 从零构建一个最小 Ubuntu 根文件系统
  • 或基于 LFS 思路,手动编译并组装一个“类 Ubuntu”系统
  • 或在容器 / 裸机 / 云镜像中定制 Ubuntu base

下面按常见坑点系统总结,避免你踩雷。


一、基础环境与工具链坑

1. 宿主机架构与目标架构不一致

  • 在 x86_64 上构建 arm64 Ubuntu 很容易出问题
  • 需要:
    • qemu-user-static
    • binfmt-support
    • 正确的 --arch
debootstrap --arch=arm64 --foreign jammy jammy-root http://ports.ubuntu.com

坑:忘了 --foreign 或没进 chroot 前执行 second stage


2. debootstrap 版本过旧

  • 老版本 debootstrap 不支持新 Ubuntu 版本
  • 建议:
    • Ubuntu 22.04+ 用 mmdebootstrap
    • 或升级宿主机 debootstrap

二、chroot 环境坑

3. 没挂载必要的虚拟文件系统

构建或配置时容易失败:

mount -t proc proc /mnt/proc
mount -t sysfs sys /mnt/sys
mount -o bind /dev /mnt/dev

坑:

  • 不挂载 /proc → apt / systemd 异常
  • 不挂载 /dev/pts → 终端异常

4. DNS 不通

chroot 里:

  • 没复制 /etc/resolv.conf
  • 或宿主机用了 systemd-resolved

现象:

Failed to fetch http://archive.ubuntu.com ...

解决:

cp /etc/resolv.conf /mnt/etc/resolv.conf

三、软件包与依赖坑

5. 最小化系统缺关键包

很多人以为:

debootstrap 完就能用

实际常缺:

  • sudo
  • network-manager / netplan
  • systemd
  • locales
  • bash-completion

建议最小集:

apt install systemd sudo netplan.io locales bash

6. locale 没生成

现象:

locale: Cannot set LC_ALL to default locale

解决:

dpkg-reconfigure locales
# 或
locale-gen en_US.UTF-8

7. 内核没装或装错

Ubuntu From Scratch 不等于 LFS:

  • 不需要你编译内核(除非你要)
  • 但 必须有一个内核

常见选择:

apt install linux-generic
# 或
apt install linux-virtual

坑:

  • 忘了装 kernel → 启动直接 panic
  • 云镜像用 linux-kvm 反而更合适

四、初始化系统坑(systemd)

8. systemd 没作为 init

构建后启动:

  • 黑屏
  • 或 init: no init found

检查:

ls -l /sbin/init

应是:

/sbin/init -> /lib/systemd/systemd

9. 第一个启动服务失败导致挂起

常见于:

  • 没有 /etc/fstab
  • 根分区 UUID 错误
  • 没有 console 设备

五、磁盘 / 镜像坑

10. fstab 写错

Ubuntu 强烈依赖:

  • / 正确挂载
  • swap 可选但建议配置

坑:

  • 用错设备名(/dev/sda1 不稳定)
  • 应该用 UUID
blkid

11. GPT / BIOS / UEFI 混乱

  • 老机器:BIOS + GRUB
  • 新机器:UEFI + GRUB-efi

常见错误:

  • UEFI 机器没建 ESP 分区
  • 没装 grub-efi-amd64

六、用户与安全坑

12. 没有默认用户

Ubuntu 默认:

  • 不允许 root 直接登录(尤其 SSH)

建议:

useradd -m -s /bin/bash ubuntu
passwd ubuntu
usermod -aG sudo ubuntu

13. SSH 起不来

原因:

  • 没装 openssh-server
  • 没生成 host key
apt install openssh-server
ssh-keygen -A

七、镜像 / 容器场景坑

14. Docker 里构建 Ubuntu 根文件系统

常见错误:

  • 用 debootstrap 在 Docker 里但没 --variant=minbase
  • systemd 在容器里不正常

建议:

  • 容器用 minbase
  • 别指望完整 systemd

15. cloud-init 冲突

云镜像常见:

  • 启动卡在 cloud-init
  • 网络被改坏

解决:

apt remove cloud-init
# 或
rm -rf /etc/cloud

八、时间 / 时区坑

16. 时区没设

ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

否则日志时间全错。


九、你“以为很简单”的坑(重点)

你以为 实际
debootstrap 完就 OK 还差 30%
不用装 kernel 必 panic
Ubuntu = Linux Ubuntu ≠ LFS
能 chroot 就能启动 不一定
能 apt 就行 systemd 才是灵魂

十、推荐的安全路线(少踩坑)

✅ 新手推荐

mmdebootstrap jammy ./root --variant=minbase

然后:

  • 装 systemd
  • 装 kernel
  • 配置 fstab
  • 装 grub

✅ 云 / 容器

  • 直接用 ubuntu-minimal
  • 或 cloud-image 改

如果你愿意,可以告诉我:

  • 你是 物理机 / 虚拟机 / 容器 / 云
  • 目标架构(x86 / arm)
  • 用途(学习 / 定制镜像 / 嵌入式)

我可以直接给你一套可启动的最小 Ubuntu 构建流程。

0 踩