Blue Flower

Ubuntu 的软件自动更新有点强悍,一个 1 G 多的镜像,自动更新后,体积暴增到 10 G 以上,任何未经许可的软件更新(包括静默下载)都是不可接受的。以下方法可以彻底禁止 Ubuntu 的自动更新。

特别提示:Ubuntu 在安装的时候会联网检查更新,安装最新的更新,请务必断网安装!!!

Ubuntu Server

一般步骤

知识兔
  • 修改配置文件

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    # 关闭 Update-Package-Lists
    sudo sed -i.bak 's/1/0/' /etc/apt/apt.conf.d/10periodic
    # 关闭 unattended-upgrades
    sudo sed -i.bak 's/1/0/' /etc/apt/apt.conf.d/20auto-upgrades
    ## 也可以通过以下命令选择 No
    sudo dpkg-reconfigure unattended-upgrades
    ## 禁用 unattended-upgrades 服务
    sudo systemctl stop unattended-upgrades
    sudo systemctl disable unattended-upgrades
    ## 可选:移除 unattended-upgrades (sysin)
    #sudo apt remove unattended-upgrades
  • 清空 apt 缓存

    1
    2
    3
    4
    5
    6
    # 可选:清空缓存
    sudo apt autoremove #移除不在使用的软件包
    sudo apt clean && sudo apt autoclean #清理下载文件的存档
    sudo rm -rf /var/cache/apt
    sudo rm -rf /var/lib/apt/lists
    sudo rm -rf /var/lib/apt/periodic
  • 重置更新通知(更新提示数字)

    如果执行 sudo apt update 命令后,登录会提示如下:

    1
    2
    3
    257 updates can be installed immediately.
    133 of these updates are security updates.
    To see these additional updates run: apt list --upgradable

    恢复原始状态:

    1
    2
    3
    4
    5
    sudo vi /var/lib/update-notifier/updates-available
    # 第一行是空白

    0 updates can be installed immediately.
    0 of these updates are security updates.

    或者直接删除文件(推荐):

    1
    sudo rm -f /var/lib/update-notifier/updates-available

    删除后提示如下(sudo apt update 后会自动恢复):

    1
    2
    The list of available updates is more than a week old.
    To check for new updates run: sudo apt update

禁用内核更新

知识兔

快速命令

以下一条命令即可禁用内核更新,后面是一些相关命令,仅供查阅。

1
2
3
4
# 禁用内核更新
sudo apt-mark hold linux-generic linux-image-generic linux-headers-generic
# 恢复内核更新
sudo apt-mark unhold linux-generic linux-image-generic linux-headers-generic

可选:在 unattended-upgrades 配置文件中禁用内核更新

1
2
3
4
5
6
7
8
9
sudo vi /etc/apt/apt.conf.d/50unattended-upgrades

找到 Package-Blacklist 字段,加入如下内容

Unattended-Upgrade::Package-Blacklist {
"linux-generic";
"linux-image-generic";
"linux-headers-generic";
};

查看安装的内核

1
2
3
4
5
6
7
8
9
10
11
12
13
dpkg --list | grep linux-
ii binutils-x86-64-linux-gnu 2.34-6ubuntu1.1 amd64 GNU binary utilities, for x86-64-linux-gnu target
ii linux-base 4.5ubuntu3 all Linux image base package
ii linux-firmware 1.187 all Firmware for Linux kernel drivers
ii linux-generic 5.4.0.26.32 amd64 Complete Generic Linux kernel and headers
ii linux-headers-5.4.0-26 5.4.0-26.30 all Header files related to Linux kernel version 5.4.0
ii linux-headers-5.4.0-26-generic 5.4.0-26.30 amd64 Linux kernel headers for version 5.4.0 on 64 bit x86 SMP
ii linux-headers-generic 5.4.0.26.32 amd64 Generic Linux kernel headers
ii linux-image-5.4.0-26-generic 5.4.0-26.30 amd64 Signed kernel image generic
ii linux-image-generic 5.4.0.26.32 amd64 Generic Linux kernel image
ii linux-libc-dev:amd64 5.4.0-81.91 amd64 Linux Kernel Headers for development
ii linux-modules-5.4.0-26-generic 5.4.0-26.30 amd64 Linux kernel extra modules for version 5.4.0 on 64 bit x86 SMP
ii linux-modules-extra-5.4.0-26-generic 5.4.0-26.30 amd64 Linux kernel extra modules for version 5.4.0 on 64 bit x86 SMP

清理多余的内核

1
2
3
4
5
6
sudo apt purge linux-image-x.x.x-x  # x.x.x-x 代表内核版本数字
sudo apt purge linux-headers-x.x.x-x
sudo apt autoremove # 自动删除不在使用的软件包

#卸载完内核后需要执行下列命令更新 grub
sudo update-grub

查看可用的内核更新命令

1
2
3
4
5
6
7
8
9
apt list --upgradable | grep linux-

WARNING: apt does not have a stable CLI interface. Use with caution in scripts.

linux-base/focal-updates 4.5ubuntu3.6 all [upgradable from: 4.5ubuntu3]
linux-firmware/focal-updates 1.187.16 all [upgradable from: 1.187]
linux-generic/focal-updates,focal-security 5.4.0.81.85 amd64 [upgradable from: 5.4.0.26.32]
linux-headers-generic/focal-updates,focal-security 5.4.0.81.85 amd64 [upgradable from: 5.4.0.26.32]
linux-image-generic/focal-updates,focal-security 5.4.0.81.85 amd64 [upgradable from: 5.4.0.26.32]

Ubuntu Desktop

Ubuntu Desktop 与 Server 版的上述配置是一致的。只是增加了额外的更新管理器的图形界面配置。

  • 图形界面配置

    ubuntu-software-updates

    图形界面配置某些选项的储存值为 2,所以修改配置文件多加一句指令为妥:

    (以下两个配置文件在 Server 版默认配置文件为 2 条,Desktop 版默认配置文件为 4 条)

    1
    2
    3
    4
    5
    6
    # 关闭 Update-Package-Lists
    sudo sed -i 's/1/0/' /etc/apt/apt.conf.d/10periodic
    sudo sed -i 's/2/0/' /etc/apt/apt.conf.d/10periodic
    # 关闭 unattended-upgrades
    sudo sed -i 's/1/0/' /etc/apt/apt.conf.d/20auto-upgrades
    sudo sed -i 's/2/0/' /etc/apt/apt.conf.d/20auto-upgrades
  • 可选:删除更新通知程序

    1
    2
    #删除更新通知程序
    sudo apt remove update-notifier

下载仅供下载体验和测试学习,不得商用和正当使用。

下载体验

请输入密码查看内容!

如何获取密码?

 

点击下载