Ubuntu 24.04 国内网络环境全面优化指南
前言
在上一篇文章中,我分享了在 Ubuntu 24.04 上安装 Howdy 人脸识别登录时遇到的网络问题。安装完成后,我对整个系统的网络配置做了一次全面排查和优化。本文记录优化过程和最终配置,供国内 Ubuntu 用户参考。
环境信息
| 项目 | 版本/值 |
|---|---|
| 操作系统 | Ubuntu 24.04.4 LTS (noble) |
| 内核 | 6.17.0-35-generic |
| 架构 | x86_64 |
| 无线网卡 | Intel Wi-Fi 6 AX200 |
| 本机 IP | 192.168.2.14 |
| 网关 | 192.168.2.1 |
| VPN 代理 | http://127.0.0.1:7897 |
| 地理位置 | 中国深圳 |
一、现状排查
先全面了解当前网络配置状态:
1.1 检查网络接口和 DNS
查看 WiFi 接口、DNS 配置和 resolve 配置文件:
bash
ip addr show wlp2s0
resolvectl status wlp2s0
cat /etc/resolv.conf
一行复制版(查看接口、DNS 和 resolve 配置):
bash
ip addr show wlp2s0 && resolvectl status wlp2s0 && cat /etc/resolv.conf
发现的问题 :DNS 配置为 Google DNS(8.8.8.8 和 2001:4860:4860::8888),国内访问延迟较高。
1.2 检查各工具代理/镜像配置
bash
# APT 源
cat /etc/apt/sources.list.d/ubuntu.sources
# pip 配置
pip config list
sudo cat /root/.config/pip/pip.conf 2>/dev/null
# Git 代理
git config --global --get-regexp 'http|proxy'
# npm 配置
npm config get registry 2>/dev/null
# apt-fast 配置
grep -n 'MIRRORS\|^[A-Z_]' /etc/apt-fast.conf
复制专用 一行排查:
bash
echo "=== DNS ===" && resolvectl status wlp2s0 | grep "DNS Server" && echo "=== APT ===" && cat /etc/apt/sources.list.d/ubuntu.sources && echo "=== pip ===" && pip config list && echo "=== Git ===" && git config --global --get-regexp 'http|proxy' && echo "=== npm ===" && npm config get registry 2>/dev/null && echo "=== apt-fast ===" && grep -n 'MIRRORS\|^[A-Z_]' /etc/apt-fast.conf
1.3 测试连接速度
测试 github.com 和国内镜像的响应速度:
bash
curl -s -o /dev/null -w "github: %{time_total}s (%{http_code})\n" --max-time 5 https://github.com
curl -s -o /dev/null -w "aliyun: %{time_total}s (%{http_code})\n" --max-time 5 https://mirrors.aliyun.com
一行复制版(同时测试国内外站点速度):
bash
curl -s -o /dev/null -w "github: %{time_total}s (%{http_code})\n" --max-time 5 https://github.com; curl -s -o /dev/null -w "aliyun: %{time_total}s (%{http_code})\n" --max-time 5 https://mirrors.aliyun.com
优化前测试结果:
| 目标 | 耗时 | 说明 |
|---|---|---|
| mirrors.aliyun.com | 1.12s | 虽快但仍有优化空间 |
| github.com | 5.00s | 直连几乎不可用,必须走代理 |
1.4 检查 VPN 代理状态
确认 VPN 代理端口是否监听:
bash
ss -tlnp | grep 7897
二、排查结论
| 检查项 | 状态 | 问题 |
|---|---|---|
| WiFi 连接 | 正常 | 无 |
| VPN 代理 (7897) | 运行中 | 无 |
| APT 主源 | mirrors.aliyun.com | 正常 |
| APT 安全源 | security.ubuntu.com | 应切到国内镜像 |
| pip 镜像 | pypi.tuna.tsinghua.edu.cn | 正常(用户+root 均已配置) |
| Git 代理 | 已全局配置 | 正常 |
| npm 镜像 | 未配置 | 需添加 |
| apt-fast MIRRORS | 未启用 | 可启用多镜像并行 |
| DNS | Google DNS (8.8.8.8) | 需切到国内 DNS |
| 系统环境变量代理 | 未设置 | 可创建快捷脚本 |
三、优化步骤
3.1 切换 DNS 到国内服务器
Google DNS 在国内延迟高,切换到国内公共 DNS 可明显提升域名解析速度。
bash
sudo resolvectl dns wlp2s0 223.5.5.5 119.29.29.29 114.114.114.114
复制专用 一行切换:
bash
sudo resolvectl dns wlp2s0 223.5.5.5 119.29.29.29 114.114.114.114 && resolvectl status wlp2s0 | grep "DNS Server"
| DNS 服务器 | 提供方 | 优先级 |
|---|---|---|
| 223.5.5.5 | 阿里 DNS | 首选 |
| 119.29.29.29 | DNSPod (腾讯) | 备选 |
| 114.114.114.114 | 114DNS | 备选 |
验证效果 :切换后 mirrors.aliyun.com 解析从 1.12s 降到 0.08s,提升 14 倍。
3.2 将 APT 安全源也切换到阿里云镜像
APT 主源已经走阿里云,但安全更新源 (security.ubuntu.com) 还是直连国外。
bash
sudo sed -i 's|http://security.ubuntu.com/ubuntu/|http://mirrors.aliyun.com/ubuntu/|' /etc/apt/sources.list.d/ubuntu.sources
复制专用 一行切换:
bash
sudo sed -i 's|http://security.ubuntu.com/ubuntu/|http://mirrors.aliyun.com/ubuntu/|' /etc/apt/sources.list.d/ubuntu.sources && cat /etc/apt/sources.list.d/ubuntu.sources
运行 sudo apt update 确认配置生效:
bash
sudo apt update
3.3 配置 npm 淘宝镜像
通过 nvm 安装的 npm 默认走官方源,国内下载慢。
bash
npm config set registry https://registry.npmmirror.com
复制专用 一行配置:
bash
npm config set registry https://registry.npmmirror.com && npm config get registry
3.4 启用 apt-fast 多镜像并行下载
apt-fast 配合 aria2 可以实现多线程、多镜像并行下载,大幅提升大包安装速度。
编辑 /etc/apt-fast.conf,将注释的 MIRRORS 行替换为:
ini
MIRRORS=( 'http://mirrors.aliyun.com/ubuntu,http://mirrors.tuna.tsinghua.edu.cn/ubuntu,http://mirrors.ustc.edu.cn/ubuntu,http://mirrors.163.com/ubuntu' )
复制专用 一行配置:
bash
sudo sed -i "s|#MIRRORS=( 'none' )|MIRRORS=( 'http://mirrors.aliyun.com/ubuntu,http://mirrors.tuna.tsinghua.edu.cn/ubuntu,http://mirrors.ustc.edu.cn/ubuntu,http://mirrors.163.com/ubuntu' )|" /etc/apt-fast.conf && grep '^MIRRORS' /etc/apt-fast.conf
配置说明:
| 镜像 | 提供方 | 作用 |
|---|---|---|
| mirrors.aliyun.com | 阿里云 | 主下载源 |
| mirrors.tuna.tsinghua.edu.cn | 清华 TUNA | 备选加速 |
| mirrors.ustc.edu.cn | 中科大 | 备选加速 |
| mirrors.163.com | 网易 | 备选加速 |
apt-fast 已配置 _MAXNUM=16、_MAXCONPERSRV=10、_SPLITCON=8,结合 aria2 可在多线程的基础上再从多个镜像同时拉取不同分片,安装大包时效果显著。
3.5 创建系统代理快捷脚本
创建一个环境变量文件,需要时一行命令临时启用代理:
bash
cat > ~/.config/proxy.env << 'EOF'
# Proxy environment helper --- source this file when you need proxy
# Usage: source ~/.config/proxy.env
export http_proxy=http://127.0.0.1:7897
export https_proxy=http://127.0.0.1:7897
export all_proxy=socks5://127.0.0.1:7897
EOF
复制专用 一行创建:
bash
mkdir -p ~/.config && printf 'export http_proxy=http://127.0.0.1:7897\nexport https_proxy=http://127.0.0.1:7897\nexport all_proxy=socks5://127.0.0.1:7897\n' > ~/.config/proxy.env && cat ~/.config/proxy.env
使用方式 --- 先 source 启用代理,再执行需要走代理的命令:
bash
source ~/.config/proxy.env
curl https://github.com
注意 :不要永久写入 .bashrc,以免影响国内镜像工具的下载速度。只在访问 GitHub raw 文件、部分国外 API 等场景时手动 source。
四、优化前后对比
| 项目 | 优化前 | 优化后 |
|---|---|---|
| DNS 服务器 | 8.8.8.8 (Google) | 223.5.5.5 (阿里) / 119.29.29.29 / 114.114.114.114 |
| mirrors.aliyun 响应时间 | 1.12s | 0.08s |
| APT 安全更新源 | security.ubuntu.com (直连国外) | mirrors.aliyun.com (国内镜像) |
| npm 源 | registry.npmjs.org (官方) | registry.npmmirror.com (淘宝) |
| apt-fast MIRRORS | 未启用 | 阿里云 + 清华 + 中科大 + 网易 四镜像并行 |
| 系统代理 | 每次手动 export |
source ~/.config/proxy.env 一键启用 |
| pip 镜像 | pypi.tuna.tsinghua.edu.cn | 保持不变(此前已优化) |
| Git 代理 | 127.0.0.1:7897 | 保持不变(此前已优化) |
| APT 主源 | mirrors.aliyun.com | 保持不变(此前已优化) |
五、最终配置速查表
日常使用时,记住以下规则:
| 场景 | 操作 |
|---|---|
| 装系统包 | sudo apt install 或 sudo apt-fast install(大包) |
| 装 Python 包 | pip3 install 包名(无需加 --proxy) |
| 装 Python 编译大包 | sudo pip3 install --proxy=http://127.0.0.1:7897 dlib |
| 装 Node.js 包 | npm install(自动走淘宝源) |
| 克隆 GitHub 仓库 | git clone ...(自动走代理) |
| 下载 GitHub raw 文件 | source ~/.config/proxy.env && wget ... |
| 访问不存在的页面 | not found 404 😢 |
六、总结
在国内 Ubuntu 桌面环境下,网络优化的核心原则是:国内流量走镜像,国外流量走代理。
- DNS 是网络体验的第一道门槛,用国内公共 DNS 能大幅减少域名解析耗时
- APT 主源、安全源都应指向国内镜像,确保系统更新稳定快速
- pip/npm 等包管理器优先使用国内镜像(清华/淘宝),编译型大包单独加
--proxy - Git 和 GitHub raw 文件等跨国流量走 VPN 代理
- apt-fast + aria2 配合多镜像可进一步加速大包下载
- 系统级代理环境变量按需启停,不要常驻
这套配置完成后,日常开发几乎感觉不到网络瓶颈。
参考链接
- 阿里云 Ubuntu 镜像: https://developer.aliyun.com/mirror/ubuntu
- 清华 PyPI 镜像: https://mirrors.tuna.tsinghua.edu.cn/help/pypi/
- 清华 Ubuntu 镜像: https://mirrors.tuna.tsinghua.edu.cn/help/ubuntu/
- 淘宝 npm 镜像: https://npmmirror.com
- aria2 项目: https://github.com/aria2/aria2
本文基于 Ubuntu 24.04 LTS 撰写。2026-06-13