02 - 系统软件源与 apt 配置
前提条件
- 已完成 Debian 12 Bookworm 最小化安装(主机名
linuxdc,IP192.168.1.100/24) - 虚拟机环境:VMware Workstation Pro(网络适配器为桥接或 NAT,已能 ping 通外网)
- 以 root 身份登录,或具有 sudo 权限的 ops 用户(执行
sudo -i切换至 root) - 网络正常,能访问
https://mirrors.tuna.tsinghua.edu.cn等镜像站
详细步骤
1. 备份原始 sources.list
cp /etc/apt/sources.list /etc/apt/sources.list.bak-$(date +%F)
2. 替换为国内高速镜像源
以清华镜像为例(速度快、更新及时、HTTPS 支持完整):
cat > /etc/apt/sources.list << 'EOF'
# Debian 12 "Bookworm" - 主源
deb https://mirrors.tuna.tsinghua.edu.cn/debian/ bookworm main contrib non-free non-free-firmware
#deb-src https://mirrors.tuna.tsinghua.edu.cn/debian/ bookworm main contrib non-free non-free-firmware
# 安全更新源
deb https://mirrors.tuna.tsinghua.edu.cn/debian-security bookworm-security main contrib non-free non-free-firmware
#deb-src https://mirrors.tuna.tsinghua.edu.cn/debian-security bookworm-security main contrib non-free non-free-firmware
# 更新源(bugfix、security backports 等)
deb https://mirrors.tuna.tsinghua.edu.cn/debian/ bookworm-updates main contrib non-free non-free-firmware
#deb-src https://mirrors.tuna.tsinghua.edu.cn/debian/ bookworm-updates main contrib non-free non-free-firmware
# 可选:backports(较新内核、软件包,生产谨慎启用)
#deb https://mirrors.tuna.tsinghua.edu.cn/debian/ bookworm-backports main contrib non-free non-free-firmware
EOF
其他常用镜像(可直接替换上面的地址):
| 镜像站 | 地址 |
|---|---|
| 阿里云 | https://mirrors.aliyun.com/debian/ |
| 华为云 | https://mirrors.huaweicloud.com/debian/ |
| 中科大 | https://mirrors.ustc.edu.cn/debian/ |
3. 更新索引并验证
apt update
检查点
所有源显示 Hit 或 Get,无 404、签名错误或超时报错。
若出现 Certificate verification failed,执行 apt install ca-certificates 后重试。
4. 执行全系统升级
apt full-upgrade -y
apt autoremove -y
apt autoclean
注意
若升级过程中安装了新内核,升级完成后需执行 reboot 重启以加载新内核。
5. 安装 VMware 增强工具与常用基础包
apt install -y open-vm-tools \
vim curl wget git net-tools lsof tree htop jq unzip \
ca-certificates apt-transport-https gnupg2 sudo
关于 open-vm-tools-desktop
服务器最小化安装不含图形环境,无需安装 open-vm-tools-desktop,安装 open-vm-tools 即可获得剪贴板共享、文件拖拽、时钟同步等核心功能。
6. 启用并验证 open-vm-tools
systemctl enable --now open-vm-tools
vmware-toolbox-cmd -v
检查点
vmware-toolbox-cmd -v 输出版本号(如 12.x.x),systemctl status open-vm-tools 显示 active (running)。
7. 重启虚拟机
reboot
重启后检查内核版本:
uname -r
实践任务
- 备份并替换
sources.list为清华镜像 - 执行
apt update && apt full-upgrade -y并观察是否有错误或新内核安装 - 安装
open-vm-tools并重启虚拟机,测试鼠标集成、剪贴板共享是否正常 - 用
apt list --installed | grep open-vm-tools确认工具已安装
自测问题
1. 为什么生产环境优先使用国内镜像而不是官方 deb.debian.org?
官方源服务器位于境外,国内访问延迟高(通常 100–300 ms),下载速度慢(几十 KB/s 到几百 KB/s 不等)。国内镜像站(清华、阿里、华为)与官方保持实时或小时级同步,访问延迟通常低于 10 ms,下载速度可达几十 MB/s,大幅缩短系统初始化与升级时间。
2. apt full-upgrade 相比 apt upgrade 会额外处理哪些情况?
apt upgrade 只升级已安装的包,不会删除任何包,也不会安装新的依赖包(若需要删除旧包才能安装新版本则跳过该包)。apt full-upgrade(等价于旧版 apt-get dist-upgrade)会智能处理依赖变化,允许安装新依赖包、删除已不再需要的旧包,确保整个系统版本一致性。内核升级、大版本包更新通常需要 full-upgrade 才能完整安装。
3. 如何临时切换回官方源进行对比测试(不破坏当前文件)?
# 使用 -o 选项临时覆盖源,不修改 sources.list
apt update -o Dir::Etc::sourcelist=/dev/null \
-o Dir::Etc::sourceparts=/dev/null \
-o APT::Get::List-Cleanup=0
# 或者临时指定单个源
apt install -t bookworm vim --dry-run
更安全的做法是建立临时文件 /etc/apt/sources.list.d/official-test.list,测试完毕后删除该文件并执行 apt update。
4. 安装 open-vm-tools 后,VMware 虚拟机最明显的三个使用体验提升是什么?
- 鼠标无缝切换:鼠标指针可以自由移入移出虚拟机窗口,无需按
Ctrl+Alt释放 - 剪贴板双向共享:宿主机与虚拟机之间可以直接复制粘贴文本内容
- 窗口分辨率自适应:拖拽调整 VMware 窗口大小后,虚拟机控制台分辨率自动跟随调整
总结
完成了 Debian 软件源的高速优化与首次全系统升级,使用国内镜像大幅缩短下载时间。在 VMware Workstation Pro 环境下安装 open-vm-tools 是立即提升操作体验的关键步骤,为后续章节的流畅实验奠定基础。