温馨提示×

怎样优化Ubuntu系统的启动速度

小樊
43
2025-11-19 18:10:45
栏目: 智能运维

Ubuntu 启动速度优化实操指南

一 定位瓶颈

  • 使用 systemd 提供的分析工具查看各阶段耗时与关键路径:
    • 整体时间:systemd-analyze time
    • 各服务耗时:systemd-analyze blame
    • 关键链:systemd-analyze critical-chain
  • 查看本次启动的高优先级错误:journalctl -b -p 3
  • 检查磁盘健康(机械盘/SSD):sudo smartctl -a /dev/sda
  • 可选:生成启动图表以可视化瓶颈(需安装 bootchart):sudo apt install bootchart pybootchartgui,重启后在 /var/log/bootchart 查看 PNG。

二 精简与并行化系统服务

  • 禁用不需要的服务(示例为常见可关闭项,按实际取舍):
    • sudo systemctl disable bluetooth.service
    • sudo systemctl disable cups.service
    • sudo systemctl disable ModemManager.service
    • 若使用 NetworkManager,可关闭 networkd-dispatcher.service
    • 可关闭 accounts-daemon.service
    • 彻底屏蔽用 masksudo systemctl mask <service>
  • 查询与确认依赖关系:systemctl list-dependencies <service> --reverse
  • 并行化早期用户态:编辑 /etc/systemd/system.conf,将 DefaultTimeoutStartSec=DefaultTimeoutStopSec= 设为合理值(如 10s),并启用并行:DefaultParallelization=yes,然后 sudo systemctl daemon-reexec 生效。

三 调整内核与 GRUB 启动参数

  • 编辑 /etc/default/grub,优化内核命令行与菜单等待:
    • 缩短菜单等待:GRUB_TIMEOUT=2
    • 精简参数(示例):GRUB_CMDLINE_LINUX_DEFAULT="quiet splash noresume fsck.mode=skip"
      • noresume:未使用休眠可跳过恢复检查
      • fsck.mode=skip:跳过启动时的文件系统检查(有数据风险,谨慎)
  • 更新 GRUB 配置:sudo update-grub
  • 说明:如确需文件系统检查,请保留默认检查策略,避免数据一致性风险。

四 存储与文件系统优化

  • 使用 SSD 作为系统盘(对启动与整体响应影响最大)。
  • 启用 TRIMsudo systemctl enable fstrim.timer(周期性 TRIM)
  • /etc/fstab 中为 SSD 分区添加 discard(可选,部分设备/内核已自动处理):
    • 示例:UUID=xxxx / ext4 defaults,noatime,discard 0 0
  • 减少文件系统检查频率:将 /etc/fstab 根分区最后一列改为 0(不检查),仅在维护窗口手动检查;或按需调整 pass 值(1/2)。

五 登录后启动项与桌面环境优化

  • 管理用户级自启:
    • 图形界面:gnome-session-properties
    • 显示隐藏的系统项(便于取舍):sudo sed -i 's/NoDisplay=true/NoDisplay=false/g' /etc/xdg/autostart/*.desktop
  • 安装并启用预加载(Preload)以加速常用应用启动:sudo apt install preload
  • 使用更轻量的显示管理器或桌面环境(如 LightDMXfceLXQt):
    • LightDM 切换示例:sudo apt install lightdm && sudo dpkg-reconfigure lightdm
  • 清理旧内核,减少引导项与 initramfs 体积:sudo apt autoremove --purge
  • 验证优化效果:重启后再次运行 systemd-analyze timesystemd-analyze blame 对比前后差异。

0