全部文档
当前文档

暂无内容

如果没有找到您期望的内容,请尝试其他搜索词

文档中心

关于KCE集群中Ubuntu节点关闭自动更新的公告

最近更新时间:2026-02-04 11:03:02

背景说明

结合运维实践与用户反馈,我们发现Ubuntu节点默认内核自动更新会存在潜在风险,可能导致组件兼容性冲突、GPU驱动失效等问题,影响业务正常运行。为此,我们将禁用KCE集群中Ubuntu节点的自动更新,涵盖apt-daily、apt-daily-upgrade及unattended-upgrades服务,从源头规避非预期更新隐患,保障KCE集群中Ubuntu节点运行的稳定性。

解决方案

为避免影响存量节点业务,本次禁用自动更新服务策略仅对增量节点实施,存量节点由用户自主决策。我们将提供禁用自动更新服务、高版本内核包清理及反向启用自动更新服务的配套脚本,满足用户个性化需求。

本文档提供的脚本仅供参考,其功能已在标准测试环境通过验证。考虑到您实际生产环境的多样性,建议您在使用前根据自身的业务环境进行必要的验证与调整。

存量Ubuntu 节点内核自动更新禁用与高版本内核清理

前置说明
  • 操作对象:Ubuntu 系统

  • 核心风险:建议在业务低峰期执行

  • 替换说明:文档中 5.15.0-164-generic 为示例内核版本,需替换为实际需卸载的高版本内核

步骤 1:检查当前运行的内核版本(确认基准版本)

执行以下命令,记录当前正常运行的内核版本(后续需保留此版本,仅清理更高版本):

uname -r

示例输出5.15.0-91-generic(此版本为需保留的基准内核)

步骤 2:检查已安装的内核镜像(判断是否需清理高版本)

执行以下命令,列出系统中已安装的所有内核镜像包:

dpkg --list | grep linux-image
  • 场景 1:仅显示步骤 1 中记录的基准内核(如仅 5.15.0-91-generic)→ 无高版本内核,跳过步骤 4、5,直接执行步骤 3。

  • 场景 2:除基准内核外,还显示其他的更高版本内核(如 5.15.0-164-generic等 )→ 需依次执行步骤 3、4、5。

步骤 3:禁用系统自动更新(核心步骤,避免内核自动升级)

创建并执行以下禁用自动更新的脚本,彻底关闭 Ubuntu 自动更新服务与定时任务:

#!/bin/bash

check_env() {
    [ "$(id -u)" -ne 0 ] && echo "ERROR: Please run this script with root privileges" && exit 1
    [ -f "/etc/os-release" ] && . /etc/os-release
    [ "${ID:-}" != "ubuntu" ] && echo "WARNING: Not an ubuntu system, no need to operate, exit now" && exit 0
}

modify_apt_config() {
    local conf_path=$1
    local conf_name=$2
    
    echo "=== Process the ${conf_name}  file ==="

    [ -f "$conf_path" ] && [ ! -f "${conf_path}.bak" ] && cp -f "$conf_path" "${conf_path}.bak" 
    local config_items=("Update-Package-Lists" "Unattended-Upgrade")
    for item in "${config_items[@]}"; do
            sed -i "s/^APT::Periodic::${item} \".*\";$/APT::Periodic::${item} \"0\";/" "$conf_path"
    done
}

disable_auto_upgrade_trigger() {
    local services=("unattended-upgrades.service" "apt-daily.timer" "apt-daily-upgrade.timer")
    echo "=== Start to disable all auto update services and timers ==="
    for svc in "${services[@]}"; do
        systemctl is-active --quiet "$svc" && systemctl stop "$svc" && echo "Service ${svc} stopped"
        systemctl disable --now "$svc" >/dev/null 2>&1
        systemctl mask "$svc" >/dev/null 2>&1 || true
        echo "Service ${svc} disabled and masked"
    done
}

check_env
modify_apt_config "/etc/apt/apt.conf.d/10periodic" "10periodic"
modify_apt_config "/etc/apt/apt.conf.d/20auto-upgrades" "20auto-upgrades"
disable_auto_upgrade_trigger

echo "Completed: Ubuntu auto update function has been disabled completely"
步骤 4:卸载高版本内核及其依赖(仅场景 2 执行)
  • 替换版本号

    • 将以下命令中的 5.15.0-164 替换为步骤 2 中查到的需卸载的高版本内核号(仅保留数字部分,如 5.15.0-164)。

    • 若步骤 2 中查到多个高版本内核(如同时有 5.15.0-1635.15.0-164),需针对每个高版本内核分别执行一次卸载命令(每次替换对应版本号)。

  • 执行卸载命令

# 卸载内核元包、高版本内核镜像及所有依赖模块
sudo apt remove --purge -y \
  linux-image-generic \
  linux-image-5.15.0-164-generic \
  linux-modules-5.15.0-164-generic \
  linux-modules-extra-5.15.0-164-generic

1. linux-image-generic:内核元包,负责自动拉取最新内核,必须卸载。
2. 若系统无 linux-modules-extra-xxx 包,命令执行时提示 “未安装”可忽略。

步骤 5:修复系统配置并更新引导(仅场景 2 执行)

执行以下命令,清理卸载残留、修复 dpkg/apt 配置,并更新 GRUB 引导(删除高版本内核启动项):

# 修复 dpkg 配置异常
sudo dpkg --configure -a
# 彻底清理未使用的依赖包(含内核残留文件)
sudo apt autoremove --purge -y
# 更新 GRUB 引导,移除高版本内核启动项
sudo update-grub
验证操作

1. 验证自动更新已禁用

# 检查自动更新服务状态(均显示 disabled/masked 即为成功)
systemctl status apt-daily.timer unattended-upgrades apt-daily-upgrade.timer
# 检查自动更新配置(均显示 0 即为成功)
grep -E "Update-Package-Lists|Unattended-Upgrade" /etc/apt/apt.conf.d/20auto-upgrades /etc/apt/apt.conf.d/10periodic

2. 验证高版本内核已卸载

# 仅显示步骤 1 中的基准内核即为成功
dpkg --list | grep linux-image

3. 验证 GRUB 引导

# 查看 GRUB 启动项,仅包含基准内核
grep "menuentry" /boot/grub/grub.cfg 

操作前建议备份 /etc/apt/ 目录,避免配置误改。

手动开启Ubuntu节点的自动更新

若您出于系统安全等考虑需开启Ubuntu节点的自动更新服务,参考脚本如下:

#!/bin/bash

# ===================== 1. 环境校验 =====================
check_env() {
    [ "$(id -u)" -ne 0 ] && echo "ERROR: Please run this script with root privileges" && exit 1
    [ -f "/etc/os-release" ] && . /etc/os-release
    [ "${ID:-}" != "ubuntu" ] && echo "WARNING: Not an ubuntu system, no need to operate, exit now" && exit 0
}

# ===================== 2. 恢复APT自动更新配置 =====================
restore_apt_config() {
    local conf_path=$1
    local conf_name=$2
    local default_values=("Update-Package-Lists" "1" "Unattended-Upgrade" "1") 

    # 优先恢复备份文件(如果存在)
    if [ -f "${conf_path}.bak" ]; then
        cp -f "${conf_path}.bak" "$conf_path"
        echo "Restored ${conf_name} from backup: ${conf_path}.bak"
        return
    fi

    # 无备份时,设置Ubuntu默认值(1=开启自动更新)
    local config_items=("Update-Package-Lists" "Unattended-Upgrade")
    for item in "${config_items[@]}"; do
        sed -i "/^APT::Periodic::${item}/c\APT::Periodic::${item} \"1\";" "$conf_path"
    done
}

# ===================== 3. 恢复自动更新服务/定时器(反向操作) =====================
enable_auto_upgrade_trigger() {
    local services=("unattended-upgrades.service" "apt-daily.timer" "apt-daily-upgrade.timer")
    for svc in "${services[@]}"; do
        systemctl unmask "$svc" >/dev/null 2>&1 || true
        systemctl enable --now "$svc" >/dev/null 2>&1
        if ! systemctl is-active --quiet "$svc"; then
            systemctl start "$svc"
        fi
    done
}

# ===================== 主执行流程 =====================
check_env
restore_apt_config "/etc/apt/apt.conf.d/10periodic" "10periodic"
restore_apt_config "/etc/apt/apt.conf.d/20auto-upgrades" "20auto-upgrades"
enable_auto_upgrade_trigger

echo -e "Completed: Ubuntu auto update function has been enabled completely"

注:
1. 容器自制Ubuntu-22.04 64位 kernel-5.15.0(ID:6e3437da-5ab0-41ca-a747-d1b15184b1f3)镜像已停止维护,存在自动更新风险。
2. 在使用标准镜像 Ubuntu-22.04 64位 kernel-5.15.0 的存量节点池新增节点时,禁用自动更新暂不生效,需手动禁用。

文档导读
纯净模式常规模式

纯净模式

点击可全屏预览文档内容
文档反馈