Files
ubuntumanager/optimize_boot_speed.sh
2025-10-06 19:10:57 +08:00

115 lines
4.0 KiB
Bash
Executable File
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/bin/bash
# 功能精简Ubuntu 24.04启动时间,禁用不必要服务和优化系统设置
# 使用方法sudo bash optimize_boot_speed.sh
# 检查是否以root权限运行
if [ "$(id -u)" -ne 0 ]; then
echo "错误请使用root权限运行此脚本sudo bash $0" >&2
exit 1
fi
# 备份重要配置
backup_configs() {
echo "正在备份系统配置..."
mkdir -p /var/backups/boot-optimization
cp -n /etc/fstab /var/backups/boot-optimization/
systemctl list-unit-files --type=service --state=enabled > /var/backups/boot-optimization/enabled_services_before.txt
}
# 禁用不必要的系统服务
disable_unneeded_services() {
echo "正在禁用不必要的系统服务..."
# 定义要禁用的服务列表(根据服务器用途调整)
local services=(
" ModemManager" # 调制解调器管理(服务器通常不需要)
" avahi-daemon" # 零配置网络(本地网络发现,服务器一般不需要)
" bluetooth" # 蓝牙服务
" cups" # 打印机服务
" cups-browsed" # 打印机浏览服务
" speech-dispatcher" # 语音合成服务
" whoopsie" # 错误报告服务
" apport" # 崩溃报告服务
" apport-autoreport" # 自动崩溃报告
" pppd-dns" # PPP DNS服务拨号上网用
" remote-fs.target" # 远程文件系统自动挂载
" plymouth-quit-wait" # 启动动画等待CLI环境无用
" plymouth-start" # 启动动画CLI环境无用
)
# 禁用服务
for service in "${services[@]}"; do
service=$(echo "$service" | xargs) # 去除空格
if systemctl is-enabled --quiet "$service"; then
echo "禁用服务: $service"
systemctl disable --now "$service" >/dev/null 2>&1
fi
done
}
# 优化系统启动参数
optimize_grub() {
echo "正在优化GRUB启动参数..."
# 备份GRUB配置
cp -n /etc/default/grub /var/backups/boot-optimization/grub.bak
# 添加启动优化参数
sed -i 's/^GRUB_CMDLINE_LINUX_DEFAULT="/GRUB_CMDLINE_LINUX_DEFAULT="quiet splash fastboot noresume /' /etc/default/grub
sed -i 's/^GRUB_TIMEOUT=[0-9]*/GRUB_TIMEOUT=2/' /etc/default/grub # 减少GRUB等待时间
# 更新GRUB配置
update-grub >/dev/null 2>&1
}
# 优化系统服务并行启动
optimize_parallel_boot() {
echo "正在优化服务并行启动..."
# 启用服务并行启动
if ! grep -q "DefaultDependencies=no" /etc/systemd/system.conf; then
echo "DefaultDependencies=no" >> /etc/systemd/system.conf
fi
# 减少超时时间
sed -i 's/^#DefaultTimeoutStartSec=.*/DefaultTimeoutStartSec=10s/' /etc/systemd/system.conf
sed -i 's/^#DefaultTimeoutStopSec=.*/DefaultTimeoutStopSec=5s/' /etc/systemd/system.conf
# 重新加载systemd配置
systemctl daemon-reload
}
# 清理不需要的启动项
cleanup_startup() {
echo "正在清理不必要的启动项..."
# 清理残留的旧内核保留最新2个
echo "清理旧内核..."
dpkg -l 'linux-image-*' | grep '^ii' | sort -V | head -n -2 | awk '{print $2}' | xargs -r apt purge -y >/dev/null 2>&1
# 清理APT缓存
apt clean >/dev/null 2>&1
apt autoremove -y >/dev/null 2>&1
}
# 显示优化结果
show_results() {
echo -e "\n优化完成以下是主要变更"
echo "1. 禁用了不必要的系统服务"
echo "2. 优化了GRUB启动参数等待时间设为2秒"
echo "3. 启用服务并行启动,缩短超时时间"
echo "4. 清理了旧内核和系统缓存"
echo -e "\n建议重启系统以应用所有更改sudo reboot"
echo -e "\n重启后可使用以下命令查看启动时间统计"
echo " systemd-analyze"
echo " systemd-analyze blame | head -n 10"
}
# 主流程执行
backup_configs
disable_unneeded_services
optimize_grub
optimize_parallel_boot
cleanup_startup
show_results