#!/bin/bash
# ==============================================
# Script to disable ALL automatic updates on Ubuntu 22.04
# Author: Your Name
# Usage: sudo ./disable_ubuntu_auto_updates.sh
# ==============================================
# Check if running as root
if [ "$(id -u)" -ne 0 ]; then
echo "This script must be run as root (use sudo)." >&2
exit 1
fi
# Disable Update-Package-Lists (APT periodic updates)
echo "[1/6] Disabling APT periodic updates..."
sed -i.bak 's/APT::Periodic::Update-Package-Lists "1"/APT::Periodic::Update-Package-Lists "0"/' /etc/apt/apt.conf.d/10periodic 2>/dev/null || echo "10periodic not found or already disabled."
# Disable unattended-upgrades
echo "[2/6] Disabling unattended-upgrades..."
sed -i.bak 's/APT::Periodic::Unattended-Upgrade "1"/APT::Periodic::Unattended-Upgrade "0"/' /etc/apt/apt.conf.d/20auto-upgrades 2>/dev/null || echo "20auto-upgrades not found or already disabled."
# Stop and disable unattended-upgrades service
echo "[3/6] Stopping unattended-upgrades service..."
systemctl stop unattended-upgrades 2>/dev/null
systemctl disable unattended-upgrades 2>/dev/null
# Optional: Remove unattended-upgrades package (uncomment if needed)
# echo "[*] Removing unattended-upgrades package..."
# apt remove -y unattended-upgrades
# Clean APT cache and old files
echo "[4/6] Cleaning APT cache..."
apt autoremove -y
apt clean
apt autoclean
rm -rf /var/cache/apt/*
rm -rf /var/lib/apt/lists/*
rm -rf /var/lib/apt/periodic/*
# Reset update notifications
echo "[5/6] Resetting update notifications..."
rm -f /var/lib/update-notifier/updates-available
# Lock kernel updates (prevent automatic upgrades)
echo "[6/6] Locking kernel updates..."
apt-mark hold linux-generic linux-image-generic linux-headers-generic
echo "=============================================="
echo "Automatic updates have been DISABLED."
echo "To re-enable updates later, run:"
echo " sudo apt-mark unhold linux-generic linux-image-generic linux-headers-generic"
echo " sudo systemctl enable unattended-upgrades"
echo "=============================================="
相关文章:
如何禁止 Ubuntu 22.04 自动更新,删除更新提示和缓存 - sysin | SYStem INside | 软件与技术分享