Windows XP Windows 7 Windows 2003 Windows Vista Windows 綜合教學 Linux 系統教學
Windows 10 Windows 8 Windows 2008 Windows NT Windows Server 電腦軟體教學
您现在的位置: Windows教程網 >> Windows教程 >  >> Linux 系統教學 >> 關於 Linux

Ubuntu 磁碟空間不足?清理 /boot 分區釋放空間完整步驟

編輯:關於 Linux

Ubuntu 升級時提示 /boot 空間不足(預設僅 256MB),因舊核心映像檔堆積。本文說明查看已安裝核心、確認當前運行核心、選擇性移除舊核心套件、清理殘留檔案與 /usr/src 目錄的完整操作流程。

1. 查看已安裝核心套件

dpkg --get-selections | grep linux-image
# 輸出範例:
# linux-image-3.13.0-32-generic      install
# linux-image-3.13.0-35-generic      install
# linux-image-3.13.0-36-generic      install
# linux-image-3.13.0-37-generic      install
# linux-image-3.13.0-39-generic      install
# linux-image-extra-3.13.0-32-generic install
# ...

2. 確認當前運行核心(不可刪除)

uname -a
# Linux ubuntu 3.13.0-39-generic #66-Ubuntu SMP ...
# 當前核心:3.13.0-39-generic

3. 選擇性移除舊核心

# 僅保留當前核心與前一版本(回滾備用),刪除其餘
sudo apt-get purge linux-image-3.13.0-32-generic linux-image-extra-3.13.0-32-generic
sudo apt-get purge linux-image-3.13.0-35-generic linux-image-extra-3.13.0-35-generic
sudo apt-get purge linux-image-3.13.0-36-generic linux-image-extra-3.13.0-36-generic
# linux-image-extra 含對應模組,需一併移除

4. 清理殘留配置檔

dpkg -l | grep ^rc | awk '{print $2}' | xargs sudo apt-get purge -y
# 或針對核心:
dpkg -l 'linux-image-*' | grep ^rc | awk '{print $2}' | xargs sudo apt-get purge -y

5. 清理 /usr/src 對應核心原始碼目錄

ls /usr/src
# linux-headers-3.13.0-32  linux-headers-3.13.0-35 ...
sudo rm -rf /usr/src/linux-headers-3.13.0-32 /usr/src/linux-headers-3.13.0-35 ...
# 僅刪除已移除核心對應的 headers 目錄

6. 驗證空間釋放

df -h /boot
# 應顯示可用空間大幅增加

boot 分區空間不足錯誤截圖

dpkg 列出核心套件截圖

uname 確認當前核心截圖

清理殘留檔案截圖

自動化腳本(慎用)

#!/bin/bash
# 保留當前核心與前一版本,自動刪除其餘
CURRENT=$(uname -r | sed 's/-generic//')
PREV=$(dpkg --get-selections | grep 'linux-image-[0-9]' | grep -v deinstall | awk '{print $1}' | sort -V | grep -B1 "$CURRENT" | head -1 | sed 's/linux-image-//')
echo "保留: $CURRENT, $PREV"
dpkg --get-selections | grep 'linux-image-[0-9]' | grep -v deinstall | awk '{print $1}' | while read pkg; do
    ver=$(echo $pkg | sed 's/linux-image-//' | sed 's/-generic//')
    if [[ "$ver" != "$CURRENT" && "$ver" != "$PREV" ]]; then
        echo "移除: $pkg"
        apt-get purge -y "$pkg" "${pkg/image/extra}"
    fi
done
apt-get autoremove -y
dpkg -l | grep ^rc | awk '{print $2}' | xargs apt-get purge -y 2>/dev/null

預防建議

  • 安裝時分配較大 /boot(建議 512MB~1GB)。
  • 定期執行 apt-get autoremove 清理舊核心。
  • 使用 unattended-upgrades 並設定 Remove-Unused-Dependencies "true"。
Copyright © Windows教程網 All Rights Reserved