如何彻底移除Ubuntu 16.04系统中的旧内核?
Hey there, I’ve run into this exact headache a bunch of times with Ubuntu 16.04—those stubborn old kernels hanging around in /boot even after autoremove fails can be super frustrating. Let’s walk through a thorough fix to free up that disk space properly:
Step 1: Lock Down Your Active Kernel
First rule of kernel cleanup: never delete the one you’re currently running. Let’s confirm which kernel that is:
uname -r
Jot down that version (it’ll look like 4.4.0-190-generic)—we’ll keep this one plus any recent backup kernels you want to hold onto.
Step 2: Identify All Installed Kernel Packages
Next, list every kernel-related package on your system to see what’s lingering:
dpkg --list | grep linux-image dpkg --list | grep linux-headers
Look for packages with version numbers lower than your active kernel (and outside your backup set)—these are our targets for removal.
Step 3: Fix Autoremove’s "Blind Spot"
Sometimes autoremove ignores old kernels because they were marked as manually installed. Fix this by marking them as automatic, so apt knows they’re safe to clean up:
sudo apt-mark auto linux-image-<OLD-VERSION>-generic linux-headers-<OLD-VERSION>-generic
Replace <OLD-VERSION> with the actual old kernel number (e.g., 4.4.0-189). Repeat this for every old kernel package pair you want to get rid of.
Step 4: Fully Purge Old Kernels
Use purge instead of regular remove to delete the packages and their leftover config files—this is way more thorough:
sudo apt-get purge linux-headers-<OLD-VERSION>-generic linux-image-<OLD-VERSION>-generic
Run this command for each old kernel pair you identified earlier.
Step 5: Clean Up Stray /boot Files
If you still see old kernel files in /boot (like vmlinuz-<OLD-VERSION> or initrd.img-<OLD-VERSION>), double-check their corresponding packages were purged first. Then delete the stray files manually:
sudo rm /boot/vmlinuz-<OLD-VERSION>-generic /boot/initrd.img-<OLD-VERSION>-generic /boot/config-<OLD-VERSION>-generic /boot/System.map-<OLD-VERSION>-generic
Be extra careful here—don’t touch any files matching your active kernel version!
Step 6: Update GRUB
Finish up by updating your GRUB config to remove references to the deleted kernels:
sudo update-grub
That should wipe out all those leftover kernel files and free up that missing 500MB+ of space in /boot.
内容的提问来源于stack exchange,提问作者user788957

