优化总体思路
快速定位瓶颈
systemd-analyzeStartup finished in 4.401s (firmware) + 10.322s (loader) + 1.121s (kernel) + 5.921s (initrd) + 36.591s (userspace)systemd-analyze blame分阶段优化清单
/etc/default/grub 中设置 GRUB_TIMEOUT=1(无人值守可设为 0),然后执行 grub2-mkconfig -o /boot/efi/EFI/centos/grub.cfg(路径因平台可能为 /boot/grub2/grub.cfg)。示例收益:从 5s 降到 1s。printk 时间戳或 initcall_debug 做内核启动剖析,定位初始化瓶颈。systemctl disable <svc> 或 systemctl mask <svc>;按需改为 socket/DBus 激活以减少并行阻塞。Type=oneshot 或 RemainAfterExit=yes;After/Before/Wants/Requires 精确描述依赖,避免“等错依赖”;After=network-online.target 并配合 WantedBy=multi-user.target,实现“网络就绪后再启动”,避免无谓重试与超时。关键命令与配置示例
systemctl list-unit-files --type=service --state=enabled、systemctl disable <svc>、systemctl mask <svc>Type=notify/Type=oneshot,减少起机阻塞。/etc/default/grub:GRUB_TIMEOUT=1,生成配置:grub2-mkconfig -o /boot/efi/EFI/centos/grub.cfgjournalctl --vacuum-time 7d、systemd-tmpfiles --clean/etc/ssh/sshd_config 中设置 UseDNS no、GSSAPIAuthentication no,重启 sshd。注意事项
/etc/default/grub、服务单元文件),变更后用 systemd-analyze 复核耗时,确保优化收益为正。systemd-journald、dbus、networking 等),避免影响系统稳定性与网络可用性。systemd-analyze blame 显示个别服务持续异常长尾,优先检查其依赖、存储/网络就绪状态与日志报错,再决定是重构服务还是调整启动顺序与超时策略。