Arch Linux 安装笔记

本指南记录了一次现代且具有较高安全性的 Arch Linux 安装过程。系统不仅采用了基于 Btrfs 的灵活文件系统布局,还在此基础上构建了 LUKS2 全盘加密、统一内核镜像(UKI,Unified Kernel Image)以及基于自定义密钥的 Secure Boot。

核心特性:

  • 全盘加密 (LUKS2):保护数据隐私。
  • Secure Boot:防止内核与 Bootloader 被篡改。
  • 统一内核镜像 (UKI):将内核、initramfs 和微码打包为单一 EFI 可执行文件。
  • Btrfs:支持现代文件系统的子卷管理、透明压缩与快照。

准备阶段:安装前环境配置

1. 获取并验证安装镜像

访问 Arch Linux 官方下载页面 获取最新的 ISO 文件及对应的 PGP 签名。为了防止安装镜像在传输过程中被篡改,强烈建议根据官方校验页面的说明对下载的镜像进行签名验证。

准备好安装介质并启动进入 Live 环境。

2. 设置控制台环境

设置美式键盘布局,并应用更易阅读的较大控制台字体:

1
2
3
loadkeys us
setfont ter-132b

3. 验证 UEFI 启动模式

确保系统以 UEFI 模式启动:

1
2
cat /sys/firmware/efi/fw_platform_size

提示:如果输出为 64,则表示系统已成功以 64 位 UEFI 模式启动。

4. 连接网络并更新系统时钟

如果是以太网连接,插上网线即可。如果是无线网络 (WLAN),可使用 iwctl 命令连接:

1
2
iwctl station wlan0 connect <您的网络名称> --passphrase <您的密码>

检查网络连通性:

1
2
ping -c 3 ping.archlinux.org

同步系统时钟:

1
2
timedatectl set-ntp true


磁盘分区与全盘加密

1. 磁盘分区布局

本指南的目标分区结构如下:

挂载点 分区标识 分区类型 大小 文件系统
/efi p1 EFI System (ef00) 2 GiB FAT32
/ p2 Linux root (8304) 剩余空间 Btrfs (基于 LUKS)

2. 擦除并重建分区表

⚠️ 警告:以下操作将清除目标磁盘上的所有数据,请谨慎操作并确保选中了正确的磁盘(可通过 lsblk 查看)。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
export DISK="/dev/nvme1n1" # 请替换为您的实际磁盘路径

# 安全擦除 SSD (可选,需要设备支持)
nvme format $DISK -s 2 -n 1

# 清理现有的文件系统签名和分区表
wipefs --all $DISK
sgdisk --zap-all $DISK
blkdiscard -f $DISK
partprobe $DISK

# 创建新的 GPT 分区表
sgdisk --clear $DISK
sgdisk -I -n 1:0:+2G -t 1:ef00 -c 1:'esp' $DISK
sgdisk -I -n 2:0:0 -t 2:8304 -c 2:'luks' $DISK

3. 加密根分区 (LUKS)

对第二分区进行加密。建议使用 4096 扇区大小以优化现代 SSD 性能:

1
2
3
cryptsetup luksFormat --sector-size=4096 ${DISK}p2
cryptsetup open ${DISK}p2 root

4. 格式化分区与创建 Btrfs 子卷

首先格式化 EFI 分区和解密后的根分区:

1
2
3
mkfs.fat -F 32 -n 'ESP' ${DISK}p1
mkfs.btrfs -L 'LUKS' /dev/mapper/root

挂载根分区并创建 Btrfs 子卷结构,以便更好地管理快照和备份:

1
2
3
4
5
6
7
8
9
10
11
mount /dev/mapper/root /mnt

btrfs subvolume create /mnt/@
btrfs subvolume create /mnt/@home
btrfs subvolume create /mnt/@root
btrfs subvolume create /mnt/@swap
btrfs subvolume create /mnt/@var_log
btrfs subvolume create /mnt/@snapshots

umount /mnt

5. 挂载文件系统与创建 Swap

启用 zstd 透明压缩,挂载各个子卷:

1
2
3
4
5
6
7
8
9
10
mount -o compress=zstd:-3,subvol=@ -m /dev/mapper/root /mnt
mount -o compress=zstd:-3,subvol=@root -m /dev/mapper/root /mnt/root
mount -o compress=zstd:-3,subvol=@home -m /dev/mapper/root /mnt/home
mount -o subvol=@swap -m /dev/mapper/root /mnt/.swap
mount -o compress=zstd:-3,subvol=@var_log -m /dev/mapper/root /mnt/var/log
mount -o compress=zstd:-3,subvol=@snapshots -m /dev/mapper/root /mnt/.snapshots

# 采用严格的安全参数挂载 EFI 分区
mount -o nosuid,nodev,noexec,nosymfollow,fmask=0177,dmask=0077 -m ${DISK}p1 /mnt/efi

利用 btrfs-progs 提供的新命令创建并启用 Swapfile:

1
2
3
btrfs filesystem mkswapfile --size 32g --uuid clear /mnt/.swap/swapfile
swapon /mnt/.swap/swapfile


核心系统安装

1. 配置镜像源

选择速度较快的国内镜像站以加速下载进程:

1
2
3
4
5
6
7
cat << 'EOF' > /etc/pacman.d/mirrorlist
Server = https://mirrors.ustc.edu.cn/archlinux/$repo/os/$arch
Server = https://mirrors.tuna.tsinghua.edu.cn/archlinux/$repo/os/$arch
Server = https://mirrors.jlu.edu.cn/archlinux/$repo/os/$arch
Server = https://fastly.mirror.pkgbuild.com/$repo/os/$arch
EOF

2. 安装必备软件包

注意:我们在此处提前安装了 zsh,这是为了后续配置用户时将其设为默认 Shell 提供依赖。

1
2
pacstrap -Ki /mnt base base-devel linux linux-firmware-intel sof-firmware intel-ucode btrfs-progs iwd vim neovim nano zsh


系统基础配置

生成文件系统表(fstab),然后 chroot 进入新系统:

1
2
3
genfstab -U /mnt >> /mnt/etc/fstab
arch-chroot /mnt

1. 时间与本地化配置

设置上海时区并将硬件时间同步:

1
2
3
ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
hwclock --systohc

配置 systemd-timesyncd 使用国内 NTP 服务器同步时间:

1
2
3
4
5
6
7
8
9
mkdir -p /etc/systemd/timesyncd.conf.d
cat << 'EOF' > /etc/systemd/timesyncd.conf.d/60-ntp.conf
[Time]
NTP=ntp.aliyun.com ntp1.aliyun.com ntp2.aliyun.com
FallbackNTP=ntp.tencent.com ntp1.tencent.com ntp2.tencent.com
EOF

systemctl enable systemd-timesyncd.service

配置系统语言环境:编辑 /etc/locale.gen 文件,取消注释 en_US.UTF-8 UTF-8zh_CN.UTF-8 UTF-8,然后生成:

1
2
3
4
locale-gen
echo 'LANG=en_US.UTF-8' > /etc/locale.conf
echo 'KEYMAP=us' > /etc/vconsole.conf

2. 网络配置

设置主机名:

1
2
echo 'arch' > /etc/hostname

配置 hosts 文件:

1
2
3
4
5
6
7
8
9
10
cat << 'EOF' > /etc/hosts
# The following lines are desirable for IPv4 capable hosts
127.0.0.1 localhost
127.0.1.1 arch.localdomain arch
# The following lines are desirable for IPv6 capable hosts
::1 localhost ip6-localhost ip6-loopback
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
EOF

配置 DNS (systemd-resolved):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# 退出 chroot 以创建软链接,随后重新进入
exit
ln -sf ../run/systemd/resolve/stub-resolv.conf /mnt/etc/resolv.conf
arch-chroot /mnt

mkdir -p /etc/systemd/resolved.conf.d
cat << 'EOF' > /etc/systemd/resolved.conf.d/60-dns_servers.conf
[Resolve]
DNS=
Domains=~.
EOF

systemctl enable systemd-resolved.service

3. 高级网络管理 (有线与无线网络绑定 Bonding)

提示:此处采用 systemd-networkdiwd 实现了有线与无线的 active-backup(主备)Bonding。请务必使用 ip link 命令获取您主机的实际接口名称(如 enp0s31f6wlan0)和无线网卡的实际 MAC 地址,替换以下配置中的值。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
pacman -S iwd

# 1. 配置 Bond 接口
cat << 'EOF' > /etc/systemd/network/30-bond0.netdev
[NetDev]
Name=bond0
Kind=bond

[Bond]
Mode=active-backup
PrimaryReselectPolicy=always
MIIMonitorSec=1s
EOF

# 2. 配置 Bond 网络参数
cat << 'EOF' > /etc/systemd/network/30-bond0.network
[Match]
Name=bond0

[Link]
RequiredForOnline=routable

[Network]
BindCarrier=enp0s31f6 wlan0 # 替换为您的实际网卡名称
DHCP=yes
EOF

# 3. 将以太网卡加入 Bond (作为主干)
cat << 'EOF' > /etc/systemd/network/30-ethernet-bond0.network
[Match]
Name=enp0s31f6 # 替换为您的以太网接口名

[Network]
Bond=bond0
PrimarySlave=true
EOF

# 4. 将无线网卡加入 Bond
cat << 'EOF' > /etc/systemd/network/30-wifi-bond0.network
[Match]
MACAddress=80:c0:1e:15:a5:0e # 替换为您的无线网卡 MAC 地址

[Network]
Bond=bond0
EOF

systemctl enable systemd-networkd.service systemd-networkd-wait-online.service iwd.service

安装并配置 WiFi 监管数据库域:

1
2
3
pacman -S --asdeps wireless-regdb
# 编辑 /etc/conf.d/wireless-regdom 取消注释 WIRELESS_REGDOM="CN"


引导配置:UKI 与 Secure Boot

此环节是将内核、微码与 initramfs 整体打包为一个可被 UEFI 固件直接读取并校验签名的 .efi 文件的核心所在。

1. 配置 mkinitcpio

更新 mkinitcpio Hook 以使用基于 systemd 的早期引导流程(支持 systemd-cryptsetup):

1
2
echo 'HOOKS=(systemd autodetect microcode modconf kms keyboard sd-vconsole sd-encrypt block filesystems)' > /etc/mkinitcpio.conf.d/hooks.conf

设置内核命令行。对于使用较新 Intel 显卡(如 Core Ultra 的 Meteor Lake 架构)的用户,需要通过参数强制启用 xe 驱动,并禁止旧版的 i915

1
2
3
# UUID 请不要硬编码,推荐写 rootflags 或让 initramfs 自动处理
echo -n 'rootflags=compress=zstd:-3,subvol=/@ ro i915.force_probe=!7d51 xe.force_probe=7d51' > /etc/kernel/cmdline

2. 准备 initramfs 解密配置

使用 crypttab.initramfs 告知 initramfs 如何解锁 LUKS 卷。

注意:请使用 blkid 获取您 /dev/nvme1n1p2 (即未解密的 LUKS 分区) 的 UUID。

1
2
echo 'root    UUID=<您的LUKS分区UUID>    none    password-echo=no,no-read-workqueue,no-write-workqueue,discard' > /etc/crypttab.initramfs

3. 配置 UKI 生成与签名

安装必要工具:

1
2
pacman -S systemd-ukify sbsigntools

配置 .preset 文件以仅生成 UKI,不生成传统 initramfs:

1
2
3
4
5
6
7
8
cat << 'EOF' > /etc/mkinitcpio.d/linux.preset
ALL_kver="/boot/vmlinuz-linux"
PRESETS=('default')

default_uki="/efi/EFI/Linux/arch-linux.efi"
default_splash="/usr/share/systemd/bootctl/splash-arch.bmp"
EOF

启用 mkinitcpio 构建时的自动 Secure Boot 签名。

⚠️ 关键前置步骤:您必须已提前在 /etc/kernel/db.key/etc/kernel/db.crt 生成了您的 Secure Boot 自定义密钥对。

1
2
3
4
5
6
7
8
9
10
cp /usr/lib/kernel/uki.conf /etc/kernel/uki.conf

cat << 'EOF' > /etc/kernel/uki.conf
[UKI]
SecureBootSigningTool=systemd-sbsign
SignKernel=true
SecureBootPrivateKey=/etc/kernel/db.key
SecureBootCertificate=/etc/kernel/db.crt
EOF

构建 UKI 并清理遗留文件:

1
2
3
4
mkdir -p /efi/EFI/Linux
mkinitcpio -P
rm /boot/initramfs-*.img /boot/vmlinuz-linux

4. 安装 Systemd-boot

虽然有了 UKI 甚至可以直接借助 efibootmgr 启动,但使用 systemd-boot 管理多个 EFI 启动项更为灵活。

1
2
3
4
5
6
7
8
9
10
11
bootctl install
systemctl enable systemd-boot-update.service

# 创建 Bootloader 配置
cat << 'EOF' > /efi/loader/loader.conf
default @saved
timeout 0
console-mode auto
editor false
EOF

编写 Pacman 钩子,以便在系统更新 systemd-boot 自身时自动对其重新进行 Secure Boot 签名:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
mkdir -p /etc/pacman.d/hooks
cat << 'EOF' > /etc/pacman.d/hooks/80-secureboot.hook
[Trigger]
Operation = Install
Operation = Upgrade
Type = Path
Target = usr/lib/systemd/boot/efi/systemd-boot*.efi

[Action]
Description = Signing systemd-boot EFI binary for Secure Boot
When = PostTransaction
Exec = /bin/sh -c 'while read -r f; do /usr/lib/systemd/systemd-sbsign sign --private-key /etc/kernel/db.key --certificate /etc/kernel/db.crt --output "${f}.signed" "$f"; done;'
Depends = sh
NeedsTargets
EOF

(可选) 也可以通过 efibootmgr 直接向主板固件添加直接启动 UKI 的条目:

1
2
efibootmgr --create --disk $DISK --part 1 --label "Arch Linux" --loader '\EFI\Linux\arch-linux.efi' --unicode


账户管理与安全

添加日常使用的普通账户,并授予 sudo 权限:

1
2
3
useradd -m -G wheel -s /usr/bin/zsh huo
passwd huo

配置 sudoers,并保留常用的代理环境变量:

1
2
3
4
5
6
EDITOR=nano visudo /etc/sudoers.d/10-wheel
# 取消注释: %wheel ALL=(ALL:ALL) ALL

EDITOR=nano visudo /etc/sudoers.d/10-env_keep
# 填入: Defaults env_keep += "http_proxy HTTP_PROXY https_proxy HTTPS_PROXY ftp_proxy FTP_PROXY all_proxy ALL_PROXY no_proxy NO_PROXY"

锁定 root 账户,提升系统安全性:

1
2
3
passwd -dl root
usermod --expiredate 1 root


收尾与重启

至此,基础系统的安装已经全部完成。退出 chroot 环境,卸载所有分区并重启:

1
2
3
4
5
exit
swapoff -a
umount -R /mnt
systemctl reboot --firmware-setup

重启进入 UEFI 固件设置后,请确保已正确导入您的自定义 Secure Boot 证书(db、KEK、PK),然后开启 Secure Boot 功能,即可顺利引导入桌面环境的前期准备阶段。